#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels // tambahan
#define SCREEN_HEIGHT 64 // OLED display height, in pixels //tambahan
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //tambahan
//Adafruit_SSD1306 display(OLED_RESET); // original
const float pi = 3.14159;
const float wheel = 24.0; // Wheel diameter in mm
float speed = 0;
float maxspeed = 0;
float sementara = 0;
const int IR_PIN = 2; // IR sensor input pin
volatile unsigned int counter = 0; // Counter variable for revolutions
unsigned long previousMillis = 0; // Variable to store previous time
unsigned int rpm = 0; // Variable to store RPM value
//unsigned int speed = 0;
const unsigned char myBitmap [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0c, 0x00, 0x03, 0x83, 0x80, 0x60, 0x0f, 0x8f, 0xff, 0xf0, 0xfe, 0x00, 0xfc, 0x00, 0x00,
0x00, 0x0f, 0x00, 0x03, 0xe7, 0xe0, 0x78, 0x1f, 0xff, 0xff, 0xf1, 0xfe, 0x03, 0xff, 0x00, 0x00,
0x00, 0x0f, 0x00, 0x03, 0xe7, 0xe0, 0x78, 0x3f, 0xff, 0xff, 0x01, 0xfe, 0x07, 0xff, 0x80, 0x00,
0x00, 0x0f, 0x01, 0x83, 0xc7, 0xf0, 0xf8, 0xff, 0xff, 0x7e, 0x01, 0xfe, 0x03, 0xe3, 0xc0, 0x00,
0x00, 0x07, 0x81, 0xe3, 0xc7, 0xf0, 0xf9, 0xff, 0xc0, 0x7e, 0x03, 0xff, 0x01, 0xe1, 0xc0, 0x00,
0x00, 0x07, 0x87, 0xe3, 0xcf, 0xf8, 0xf9, 0xfe, 0x00, 0x7c, 0x03, 0xff, 0x01, 0xe3, 0xc0, 0x00,
0x00, 0x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 0x3c, 0x07, 0x8f, 0x8f, 0xc0, 0x00,
0x00, 0x07, 0xe0, 0x0f, 0x00, 0x07, 0x81, 0xfe, 0x00, 0xf0, 0x3c, 0x07, 0x87, 0x07, 0xc0, 0x00,
0x00, 0x01, 0x80, 0x0f, 0x00, 0x07, 0x00, 0xfc, 0x00, 0xf0, 0x18, 0x03, 0x81, 0x03, 0xe0, 0x00,
0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x70, 0x00, 0xf0, 0x00, 0x03, 0x80, 0x01, 0xf0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x03, 0xc0, 0x00, 0xf0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x30, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0xf9, 0x81, 0xfe, 0x3f, 0x3f, 0xef, 0xc1, 0xf8, 0x70, 0xcc, 0x3f, 0x9f, 0xc0, 0x00,
0x00, 0x03, 0x01, 0x81, 0x80, 0x60, 0x86, 0x0c, 0xe3, 0x8c, 0x78, 0xcc, 0x60, 0x98, 0x00, 0x00,
0x00, 0x03, 0x01, 0x81, 0x80, 0x60, 0x06, 0x0c, 0x63, 0x06, 0x78, 0xcc, 0x60, 0x30, 0x00, 0x00,
0x00, 0x03, 0x01, 0x81, 0x80, 0xc0, 0x06, 0x0c, 0x62, 0x06, 0x6c, 0xcc, 0xc0, 0x18, 0x00, 0x00,
0x00, 0x03, 0xf9, 0x81, 0xfc, 0xc0, 0x06, 0x0c, 0xc6, 0x06, 0x6c, 0xcc, 0xc0, 0x1f, 0x00, 0x00,
0x00, 0x03, 0x01, 0x81, 0x80, 0xc0, 0x06, 0x0f, 0xc6, 0x06, 0x66, 0xcc, 0xc0, 0x03, 0xc0, 0x00,
0x00, 0x03, 0x01, 0x81, 0x80, 0xc0, 0x06, 0x0c, 0xc3, 0x06, 0x66, 0xcc, 0xc0, 0x00, 0xc0, 0x00,
0x00, 0x03, 0x01, 0x81, 0x80, 0x60, 0x06, 0x0c, 0x63, 0x06, 0x63, 0xcc, 0x60, 0x00, 0xc0, 0x00,
0x00, 0x03, 0x01, 0x81, 0x80, 0x70, 0x86, 0x0c, 0x33, 0x8c, 0x63, 0xcc, 0x71, 0x90, 0xc0, 0x00,
0x00, 0x03, 0xf9, 0xfd, 0xfe, 0x1f, 0x06, 0x0c, 0x30, 0xf8, 0x61, 0xcc, 0x1f, 0x1f, 0x80, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
} ;
void IRinterrupt() {
counter++;
}
void setup() {
pinMode(IR_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(IR_PIN), IRinterrupt, FALLING);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
//display.setCursor(0, 0);
//display.println("TEG");
//display.display();
//delay(2000);
display.clearDisplay();
display.drawBitmap(0, 0, myBitmap, 128, 64, WHITE);
//display.setTextSize(2);
//display.setTextColor(SSD1306_WHITE);
//display.setCursor(30, 15); //modifikasi
//display.println("Speed"); //modifikasi
//display.setCursor(20, 35); //modifikasi
//display.println("Checker"); //modifikasi
display.display();
delay(1000);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000) {
detachInterrupt(digitalPinToInterrupt(IR_PIN));
rpm = (counter / 2) * 60; // Calculate RPM
speed = pi*wheel*counter*0.0036 ; // Calculate speed
counter = 0;
if(maxspeed<=speed){
maxspeed=0;
maxspeed=maxspeed+speed;
sementara=maxspeed;
}
//maxspeed= speed+1;
attachInterrupt(digitalPinToInterrupt(IR_PIN), IRinterrupt, FALLING);
previousMillis = currentMillis;
display.clearDisplay();
display.setTextSize(1,2);
display.setCursor(0, 5);
display.print("SPEED: ");
display.println(speed);
display.setCursor(100, 5);
display.print("Km/h ");
display.setCursor(0, 25);
display.print("Max Speed: ");
display.println(maxspeed);
display.setCursor(0, 45);
display.print("RPM : ");
display.println(rpm);
if(maxspeed>speed){
maxspeed=0;
}
display.display();
}
}