#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int outpin = 2;
int alertpin = 14;
float maxSpeedLimit = 16;
int potpin = 15;
int speedval;
float motorRPM = 1000.0; // Motor's RPM
float wheelDiameter = 0.1; // Wheel diameter in meters (adjust as needed)
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
void testfillrect(void) {
display.clearDisplay();
for(int16_t i=0; i<display.height()/2; i+=3) {
// The INVERSE color is used so rectangles alternate white/black
display.fillRect(i, i, display.width()-i*2, display.height()-i*2, WHITE);
display.display(); // Update screen with each newly-drawn rectangle
delay(1);
}
delay(2000);
}
void displaySpeedInfo(float speedKMH) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
// Change text color based on speed
if (speedKMH <= 4) {
display.setTextColor(YELLOW);
} else if (speedKMH <= 8) {
display.setTextColor(RED);
} else if (speedKMH <= 12) {
display.setTextColor(GREEN);
} else {
display.setTextColor(CYAN);
}
display.setCursor(0,0); // Start at top-left corner
display.println(F("EBike Controller"));
display.println("");display.println("");
// Change text size based on speed
if (speedKMH <= 4) {
display.setTextSize(1);
} else if (speedKMH <= 8) {
display.setTextSize(1.5);
} else if (speedKMH <= 12) {
display.setTextSize(2);
} else {
display.setTextSize(2.5);
}
display.print("Speed: ");
display.println("");
display.print(speedKMH, 1); // Display speed with one decimal place
display.print(" km/h");
display.display();
}
void setup() {
pinMode(outpin, OUTPUT);
pinMode(alertpin, OUTPUT);
Wire.begin();
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
// Draw a single pixel in white
display.drawPixel(10, 10, WHITE);
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(2); // Adjust text size as needed
display.setTextColor(WHITE);
testfillrect();
}
void loop() {
speedval = analogRead(potpin);
if (speedval >= 0 && speedval <= 50) {speedval = 0;}
speedval = map(speedval, 0, 1024, 0, 255);
Serial.println("");Serial.print("speedval: ");Serial.println(speedval);Serial.println("");
analogWrite(outpin, speedval);
// Calculate speed in km/h
float speedKMH = (speedval / 255.0) * (motorRPM * wheelDiameter * 3.14159265359 * 60.0) / 1000.0;
// display
displaySpeedInfo(speedKMH);
// maxSpeedLimit condition
if (speedKMH >= maxSpeedLimit ) {digitalWrite(alertpin, HIGH);}
else {digitalWrite(alertpin, LOW);}
delay(20);
}
// ---------------------------------------------------------------------------
// #include <Wire.h>
// #include <Adafruit_GFX.h>
// #include <Adafruit_SSD1306.h>
// #define SCREEN_WIDTH 128
// #define SCREEN_HEIGHT 64
// #define OLED_RESET -1
// #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
// Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// int outpin = 2;
// int alertpin = 14;
// float maxSpeedLimit = 16;
// int potpin = 15;
// int speedval;
// float motorRPM = 1000.0; // Motor's RPM
// float wheelDiameter = 0.1; // Wheel diameter in meters (adjust as needed)
// void testfillrect(void) {
// display.clearDisplay();
// for(int16_t i=0; i<display.height()/2; i+=3) {
// // The INVERSE color is used so rectangles alternate white/black
// display.fillRect(i, i, display.width()-i*2, display.height()-i*2, SSD1306_INVERSE);
// display.display(); // Update screen with each newly-drawn rectangle
// delay(1);
// }
// delay(2000);
// }
// void displaySpeedInfo(float speedKMH) {
// display.clearDisplay();
// display.setTextSize(1); // Normal 1:1 pixel scale
// display.setTextColor(SSD1306_WHITE); // Draw white text
// display.setCursor(0,0); // Start at top-left corner
// display.println(F("EBike Controller"));
// display.println("");display.println("");
// // Draw 2X-scale text
// display.setTextSize(2);
// display.setTextColor(SSD1306_WHITE);
// display.print("Speed: ");
// display.println("");
// display.print(speedKMH, 1); // Display speed with one decimal place
// display.print(" km/h");
// display.display();
// }
// void setup() {
// pinMode(outpin, OUTPUT);
// pinMode(alertpin, OUTPUT);
// Wire.begin();
// Serial.begin(9600);
// // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
// if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
// Serial.println(F("SSD1306 allocation failed"));
// for(;;); // Don't proceed, loop forever
// }
// display.display();
// delay(2000); // Pause for 2 seconds
// // Clear the buffer
// display.clearDisplay();
// // Draw a single pixel in white
// display.drawPixel(10, 10, SSD1306_WHITE);
// // Show the display buffer on the screen. You MUST call display() after
// // drawing commands to make them visible on screen!
// display.display();
// delay(2000);
// display.clearDisplay();
// display.setTextSize(2); // Adjust text size as needed
// display.setTextColor(SSD1306_WHITE);
// testfillrect();
// }
// void loop() {
// speedval = analogRead(potpin);
// if (speedval >= 0 && speedval <= 50) {speedval = 0;}
// speedval = map(speedval, 0, 1024, 0, 255);
// Serial.println("");Serial.print("speedval: ");Serial.println(speedval);Serial.println("");
// analogWrite(outpin, speedval);
// // Calculate speed in km/h
// float speedKMH = (speedval / 255.0) * (motorRPM * wheelDiameter * 3.14159265359 * 60.0) / 1000.0;
// // display
// displaySpeedInfo(speedKMH);
// // maxSpeedLimit condition
// if (speedKMH >= maxSpeedLimit ) {digitalWrite(alertpin, HIGH);}
// else {digitalWrite(alertpin, LOW);}
// delay(20);
// }