#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 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, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x04, 0xff, 0xe4, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x04, 0x7f, 0xc4, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x04, 0x7f, 0xc4, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x04, 0x7f, 0xc4, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x04, 0x3f, 0x84, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x04, 0x3f, 0x84, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x04, 0x3f, 0x84, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x04, 0x1f, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x04, 0x1f, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x18, 0x00, 0x3c, 0x04, 0x1f, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x18, 0x00, 0x3c, 0x04, 0x0e, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x18, 0x00, 0x3c, 0x06, 0x0e, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x18, 0x00, 0x3c, 0x06, 0x0e, 0x0c, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x06, 0x04, 0x0c, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x07, 0x04, 0x1c, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x07, 0x04, 0x1c, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x07, 0x84, 0x3c, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x07, 0x84, 0x3c, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x07, 0xc4, 0x7c, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x07, 0xc4, 0x7c, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x07, 0xc4, 0x7c, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x07, 0xe4, 0xfc, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xe0, 0x3c, 0x07, 0xe4, 0xfc, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x60, 0x00, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x60, 0x00, 0x00, 0x07, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0xf0, 0x00, 0x00, 0x07, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0xf0, 0x00, 0x00, 0x0f, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0xf0, 0x00, 0x00, 0x0f, 0x80, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0xf8, 0x00, 0x00, 0x0f, 0x80, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x01, 0xf8, 0x00, 0x00, 0x1f, 0x80, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x01, 0xf8, 0x00, 0x00, 0x1f, 0xc0, 0x0c, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x17, 0xff, 0xff, 0xfe, 0x7f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xff, 0xc4, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0xff, 0xff, 0xf8, 0x0f, 0xff, 0xff, 0x84, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x7f, 0xff, 0xe0, 0x07, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x3f, 0xff, 0xc0, 0x01, 0xff, 0xfc, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x0f, 0xff, 0x80, 0x00, 0xff, 0xf8, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x07, 0xff, 0x00, 0x00, 0x7f, 0xf0, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x0f, 0xff, 0x00, 0x00, 0x7f, 0xf0, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x0f, 0xff, 0x00, 0x00, 0xff, 0xf8, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x0f, 0xff, 0x80, 0x00, 0xff, 0xf8, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0xdf, 0x80, 0x00, 0xfd, 0xf8, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1f, 0x0f, 0x80, 0x01, 0xf8, 0xfc, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x1e, 0x07, 0xc0, 0x01, 0xe0, 0x3c, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x3c, 0x01, 0xc0, 0x01, 0xc0, 0x1c, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x30, 0x00, 0xc0, 0x03, 0x00, 0x0e, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x20, 0x00, 0x60, 0x02, 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x17, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
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;
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(); //Mod1
display.setTextSize(2); //Mod1
display.setTextColor(SSD1306_WHITE); //Mod1
//display.setCursor(0, 0); //original
//display.println("TEG"); //original
//display.display(); //original
//delay(2000); //original
display.clearDisplay();
display.drawBitmap(0, 0, myBitmap, 128, 64, WHITE);
display.display();
// display.setTextSize(2);
// display.setTextColor(SSD1306_WHITE);
// display.setCursor(30, 10); //modifikasi
// display.println("Speed"); //modifikasi
// display.setCursor(20, 30); //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);
display.setCursor(5, 10);
// display.print("SPEED:");
display.setTextSize(4);
display.println(speed);
display.setTextSize(1);
display.setCursor(100, 42);
display.print("Km/h");
display.setTextSize(1,1);
display.setCursor(0, 55);
display.print("Max Speed: ");
display.println(maxspeed);
//display.setCursor(0, 55);
//display.print("RPM: ");
//display.println(rpm);
if(maxspeed>speed){
maxspeed=0;
}
if(speed >100){
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(3);
display.print("DANGER!");
display.setCursor(0,45);
display.setTextSize(1);
display.print("It's not");
display.setCursor(0,55);
display.setTextSize(1);
display.print("Fast & Furious!");
}
display.display();
}
}