Compare commits

..

3 Commits

Author SHA1 Message Date
sieja
913a6d1f15 Uusnięcie zbędnego kodu
Przywrócenie SpeedTrend
Przywrócenie migania światła stop
2025-06-15 17:14:20 +02:00
sieja
ccd190ec40 Merge branch 'master' of https://gitea.kaczor.it/sieja/Arduino 2025-06-15 16:20:23 +02:00
sieja
f3cc292bf8 Poprawa odczytu napięcia
UStawienie przycisku

Przesuniecie funkcji na góre kodu
2025-06-15 16:13:32 +02:00

View File

@@ -1,21 +1,12 @@
// #include <avr/eeprom.h>
#include <ESP32Servo.h> // model servo: DS3218 PRO #include <ESP32Servo.h> // model servo: DS3218 PRO
// nie włącza serovo
// nie podaje napięcia z baterii
#include <SPI.h> #include <SPI.h>
#include <Wire.h> #include <Wire.h>
#include <time.h> #include <time.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#define Version "2.1.0" #define Version "2.2.0"
////2DO: ////2DO:
//diagnostyka i/lub przeciwdziałanie skokom predkosci
//dlaczego wskaznik odnosi sie do poprawnego biegu a w tym czasie bieg jest zly? bo czas ponizej 2s?
//zmienic system usypiania dodac bezwzglednie czas 3 min, i po czeku z przyciskiem ponowny odczyt magnesu po 0,5sec
// menu do zmiany zakresu predkosci biegów // menu do zmiany zakresu predkosci biegów
// menu do zmiany zakresu kątów biegów, obwodu koła, ilosci magnesow // menu do zmiany zakresu kątów biegów, obwodu koła, ilosci magnesow
@@ -30,10 +21,10 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define ServoPin 19 //on board: D19 #define ServoPin 19 //on board: D19
#define BrakingLight 13 //on board: D13 BrakingLight #define BrakingLight 13 //on board: D13 BrakingLight
#define PinLED 25 //on board: D25 ORANGE loop signal #define PinLED 25 //on board: D25 ORANGE loop signal
#define Btn1 27 //on board: D27 Button1 #define VoltInptPin 27 //on board: D27
#define Btn2 33 //on board: D33 Button2 #define Btn2 33 //on board: D33 Button2
// #define VoltInptPin 23 //on board: D23 Battery Voltage // #define VoltInptPin 23 //on board: D23 Battery Voltage prawdopodobnie uszkodzone ADC
#define ServoMaxAngle 130 #define ServoMaxAngle 130
#define MaxAngle 179 #define MaxAngle 179
@@ -49,8 +40,8 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Servo myservo; Servo myservo;
//SPEED //SPEED
double readSignalTime_5;
double readSignalTime_4; double readSignalTime_4;
double readSignalTime_3; double readSignalTime_3;
double readSignalTime_2; double readSignalTime_2;
@@ -70,20 +61,8 @@ unsigned long millissSpd = millis();
unsigned long lastMillisSpd = millis(); unsigned long lastMillisSpd = millis();
unsigned long lastLastMillisSpd = millis(); unsigned long lastLastMillisSpd = millis();
unsigned long loopTime = millis(); unsigned long loopTime = millis();
//speed validation
double speed4Gear = 0.0;
double speed4Gear_1 = 0.0;
double speed4Gear_2 = 0.0;
double speed4Gear_3 = 0.0;
double speed4Geat_estimated = 0.0;
double speedDiff_1 = 0.0;
double speedDiff_2 = 0.0;
double speedDiff_3 = 0.0;
int avgWeight_2 = 2;
int avgWeight_3 = 1;
//GEAR //GEAR
int currentGear = 1; int currentGear = 1;
int previousGear = 1;
int calculatedGear = 1; int calculatedGear = 1;
//Przedziały dia biegów //Przedziały dia biegów
float spdRange1and2 = 7.5; float spdRange1and2 = 7.5;
@@ -95,11 +74,10 @@ float spdRange6and7 = 29.9;
float spdRange7and8 = 36.5; float spdRange7and8 = 36.5;
double calcTimeDiff = 0.0; double calcTimeDiff = 0.0;
double lastGearCalc = millis(); double lastGearCalc = millis();
double changeDelayMs = 2000.0; double changeDelayMs = 1000.0;
double accelerationShift = 1.0; double accelerationShift = 1.0;
int displGear = 9 - currentGear; int displGear = 9 - currentGear;
float currentGearRangeLower = 0; float currentGearRangeLower = 0;
float currentGearRangeMiddle = 3.0;
float currentGearRangeUpper = 7.5; float currentGearRangeUpper = 7.5;
//SERVO //SERVO
int pos = 0; int pos = 0;
@@ -109,16 +87,11 @@ int servoCurrPos = ServoMaxAngle;
int gearBarHeight = 0; int gearBarHeight = 0;
int gearBarPosition = 0; int gearBarPosition = 0;
float speedForBar = 0; float speedForBar = 0;
//DST unsigned int pointerVisibility = 1;
int totalDistMemLocation = 60;
int loop_cnt = 0;
double totalDist = 0.0;
unsigned int totalDistReaded = 0;
unsigned int totalDistWrited = 0;
//BATTERY //BATTERY
float referenceVoltage = 5.1; float referenceVoltage = 3.3;
int maxADCValue = 1023; int maxADCValue = 4095;
float voltageDividerRatio = 3.0; float voltageDividerRatio = 5.7;
int adcBattVoltValue = 0; int adcBattVoltValue = 0;
float inputVoltage = 0.0; float inputVoltage = 0.0;
float measuredVoltage = 0.0; float measuredVoltage = 0.0;
@@ -126,8 +99,6 @@ int voltBarHeight = 0;
int voltBarPosition = 0; int voltBarPosition = 0;
//oth //oth
int BrakingLightSwitch; int BrakingLightSwitch;
int ups = 0;
int downs = 0;
int run_hrs = 0; int run_hrs = 0;
int run_mins = 0; int run_mins = 0;
void setup() { void setup() {
@@ -135,14 +106,7 @@ void setup() {
digitalWrite(ServoSwitch, HIGH); digitalWrite(ServoSwitch, HIGH);
myservo.attach(ServoPin); // attaches the servo on pin 4 to the servo object myservo.attach(ServoPin); // attaches the servo on pin 4 to the servo object
setPosition(8); setPosition(8);
//
// Serial.begin(9600);
// //DIPLAY settings
// if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
// Serial.println(F("SSD1306 allocation failed"));
// for (;;); // Don't proceed, loop forever
// }
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.clearDisplay(); display.clearDisplay();
display.setTextColor(WHITE); display.setTextColor(WHITE);
@@ -158,10 +122,10 @@ void setup() {
pinMode(PinInSpeed, INPUT); pinMode(PinInSpeed, INPUT);
// pinMode(PinInterrupt, OUTPUT); // pinMode(PinInterrupt, OUTPUT);
// digitalWrite(PinInterrupt, HIGH); // digitalWrite(PinInterrupt, HIGH);
pinMode(Btn1, INPUT); pinMode(VoltInptPin, INPUT);
pinMode(Btn2, INPUT); pinMode(Btn2, INPUT);
// pinMode(VoltInptPin, INPUT);
//OUTPUT //OUTPUT
pinMode(PinLED, OUTPUT); pinMode(PinLED, OUTPUT);
pinMode(BrakingLight, OUTPUT); pinMode(BrakingLight, OUTPUT);
@@ -195,151 +159,100 @@ void setup() {
myservo.write(servoCurrPos); myservo.write(servoCurrPos);
delay(15); delay(15);
} }
//MEMORY
// eeprom_read_block(&totalDistReaded, totalDistMemLocation, 2);
// totalDist = float(totalDistReaded);
} }
//########################################### LOOP ############################################################
//########################################### LOOP ############################################################
void loop() {
loopTime = millis(); void setPosition(int currentGear) {
display.clearDisplay(); pos = 180 - round((currentGear - 1) * (ServoMaxAngle / (MaxGear - 1) ));
// //DIAG Btn1 if (pos >= 180) {
// display.setTextSize(1); pos = MaxAngle;
// display.setCursor(0, 30); }
// display.println("Btn1:");
// display.setCursor(30, 20);
// display.println(digitalRead(Btn1));
// display.setCursor(30, 30);
// display.println(analogRead(Btn1));
// display.setCursor(55, 30);
display.setTextSize(3); if (pos <= 0) {
pos = MinAngle;
//################################################ }
//SPEED if (sleepMode == 1 && calcSpeedMain > 0.0) {
sleepSpd = millis() - millissSpd; digitalWrite(ServoSwitch, HIGH);
if (sleepSpd >= 1000.0) { //podaj zerową prędkość jeśli nie było odcztu od 1,5 s for (servoCurrPos = myservo.read(); servoCurrPos <= 180; servoCurrPos++) {
speed4Gear = 0.0; myservo.write(servoCurrPos);
speed4Gear_2 = 0.0; delay(4);
raw_speed = 0.0;
//przejście w tryb uśpienia za pomocą odpowiedniego ustawienia koła i magnesu
if ((digitalRead(PinInSpeed) == LOW)||(digitalRead(Btn2) == HIGH)|| (sleepSpd >= LongTimeToSleep)) {
display.fillCircle(75, 10, 10, SSD1306_WHITE);
if (sleepSpd >= TimeToSleep) {
prepareTurnOff();
}
}
} }
sleepMode = 0;
if (speed4Gear > 40 || isinf(speed4Gear)) {
speed4Gear = speed4Gear_3;
} }
calcSpeed(); myservo.write(pos);
calcGear();
displGear = 9 - currentGear;
setPosition(currentGear);
//duzy font
//GEAR
display.setCursor(8, 0);
display.println("G:");
display.setCursor(40, 0);
display.print(currentGear);
//SPEED_TREND
display.setCursor(75, 00);
if (speedTrend <= -1 ) {
display.write(31);
} else {
if (speedTrend >= 1) {
display.write(30);
} else {
display.println("-");
}
}
//SPEED
display.setCursor(8, 40);
display.println("S:");
display.setCursor(40, 40);
display.println(calcSpeedMain, 1);
//GearBar
currentGearRangeMiddle = (currentGearRangeLower + currentGearRangeUpper) / 2;
display.fillRect(115, 32, 7, 1, SSD1306_WHITE);
display.fillRect(115, 0, 7, 1, SSD1306_WHITE);
display.fillRect(115, 63, 7, 1, SSD1306_WHITE);
display.setTextSize(2);
gearBarHeight = 64 - int(((calcSpeedMain - currentGearRangeLower) / (currentGearRangeUpper - currentGearRangeLower)) * 64) - 5;
display.setCursor(115, gearBarHeight);
display.write(16);
display.setTextSize(3);
//VOLT_BAR
// adcBattVoltValue = analogRead(VoltInptPin);
// measuredVoltage = (adcBattVoltValue * referenceVoltage) / maxADCValue;
// inputVoltage = measuredVoltage * voltageDividerRatio;
voltBarHeight = int(((inputVoltage-9)/3)*64); //odjemowanie 9 bo to minimalne napiecie, podział przez 3 bo zakladam max napiecie 12.0V a nie 12.6V
voltBarPosition = 64 - voltBarHeight;
display.fillRect(0, voltBarPosition, 1, voltBarHeight, SSD1306_WHITE);
//########################################## ZAPIS DO WYŚWIETLACZA ######################################################
display.display();
loopTime = millis();
if ((digitalRead(Btn2) == HIGH)) {
digitalWrite(PinLED, HIGH);
}else {
digitalWrite(PinLED, LOW);
}
//wstrzymanie pętli by odczyty były co 0,5s
for (; (millis() - loopTime) < 300 ;) {
delay(10);
}
if (speedTrend == -1 && speed4Gear > 0.0 ) {
if (BrakingLightSwitch == 1) {
digitalWrite(BrakingLight, HIGH);
BrakingLightSwitch = 0;
} else {
digitalWrite(BrakingLight, LOW);
BrakingLightSwitch = 1;
}
} else {
digitalWrite(BrakingLight, LOW);
}
// //TTL DST
// // totalDist = 0;
// loop_cnt = loop_cnt +1;
// if (loop_cnt >= 20 //&& totalDistWrited != totalDist
// ){
// totalDistWrited = int(totalDist);
// // eeprom_write_block(&totalDistWrited,totalDistMemLocation,2);
// loop_cnt = 0;
// }
} }
//########################################### LOOP ############################################################
//########################################### LOOP ############################################################
void readSpeed() { void readSpeed() {
lastLastMillisSpd = lastMillisSpd; lastLastMillisSpd = lastMillisSpd;
lastMillisSpd = millissSpd; lastMillisSpd = millissSpd;
millissSpd = millis(); millissSpd = millis();
readSignalTime_5 = readSignalTime_4;
readSignalTime_4 = readSignalTime_3;
readSignalTime_3 = readSignalTime_2; readSignalTime_3 = readSignalTime_2;
readSignalTime_2 = readSignalTime_1; readSignalTime_2 = readSignalTime_1;
readSignalTime_1 = double(millissSpd - lastLastMillisSpd) / 1000; readSignalTime_1 = double(millissSpd - lastLastMillisSpd) / 1000;
readSignalTimeAvg = (readSignalTime_1 + readSignalTime_2 + readSignalTime_3 )/3; readSignalTimeAvg = (readSignalTime_1 + readSignalTime_2 + readSignalTime_3 )/3;
raw_speed = (((2 * Pi) / readSignalTimeAvg * ((WheelCircumference) / (Pi)) * ms2kmh)) / MagnetsCnt; raw_speed = (((2 * Pi) / readSignalTimeAvg * ((WheelCircumference) / (Pi)) * ms2kmh)) / MagnetsCnt;
}
void prepareTurnOff() {
sleepMode = 1;
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Przygotwywanie...");
display.display();
for (servoCurrPos = myservo.read(); servoCurrPos >= 60; servoCurrPos--) {
myservo.write(servoCurrPos);
delay(15);
}
for (; 1500 < (millis() - lastMillisSpd);) { // zmiana z 1000 na 1500 w 1.13.19
digitalWrite(ServoSwitch, LOW);
digitalWrite(ServoPin, LOW);
//INFO
display.clearDisplay();
display.setTextSize(1);
display.setCursor(5, 30);
display.println("Mozna teraz");
display.setCursor(5, 38);
display.println("bezpiecznie wylaczyc");
display.setCursor(5, 46);
display.println("komputer.");
display.setCursor(5, 0);
display.write(31);
display.setCursor(10, 0);
display.println("+");
display.setCursor(15, 0);
display.write(30);
//Version
display.setCursor(5, 13);
display.println("V: ");
display.setCursor(15, 13);
display.println(Version);
//RUN TIME
run_mins = floor((millis() / 1000) / 60);
run_hrs = floor(run_mins / 60);
run_mins = run_mins - (run_hrs * 60);
display.setCursor(65, 54);
display.println("T:");
display.setCursor(80, 54);
display.println(run_hrs);
display.setCursor(88, 54);
display.println(":");
display.setCursor(93, 54);
display.println(run_mins);
adcBattVoltValue = analogRead(VoltInptPin);
measuredVoltage = (adcBattVoltValue * referenceVoltage) / maxADCValue;
inputVoltage = measuredVoltage * voltageDividerRatio;
voltBarHeight = int(((inputVoltage-9)/3)*64); //odjemowanie 9 bo to minimalne napiecie, podział przez 3 bo zakladam max napiecie 12.0V a nie 12.6V
voltBarPosition = 64 - voltBarHeight;
display.fillRect(0, voltBarPosition, 2, voltBarHeight, SSD1306_WHITE);
display.display();
delay(500);
}
} }
void calcSpeed(){ void calcSpeed(){
@@ -349,6 +262,17 @@ void calcSpeed(){
calcSpeedAvg = (calcSpeed1 + calcSpeed2 + calcSpeed3)/3; calcSpeedAvg = (calcSpeed1 + calcSpeed2 + calcSpeed3)/3;
calcSpeedMain = calcSpeedAvg; calcSpeedMain = calcSpeedAvg;
if(abs(raw_speed - calcSpeed2) >= changeDelayMs) {
if ((raw_speed - calcSpeed2) < 0) {
speedTrend = -1;
} else {
speedTrend = 1;
}
}else {
speedTrend = 0;
}
} }
void calcGear() { void calcGear() {
accelerationShift = 1; accelerationShift = 1;
@@ -390,95 +314,126 @@ void calcGear() {
}; };
calcTimeDiff = millis() - lastGearCalc; calcTimeDiff = millis() - lastGearCalc;
// previousGear = currentGear; if (calcTimeDiff < 2000 && abs((calculatedGear-currentGear))<=1){
currentGear = calculatedGear; currentGear = currentGear;
lastGearCalc = millis(); pointerVisibility = 0;
} else {
currentGear = calculatedGear;
lastGearCalc = millis();
pointerVisibility = 1;
}
} }
void setPosition(int currentGear) {
pos = 180 - round((currentGear - 1) * (ServoMaxAngle / (MaxGear - 1) ));
if (pos >= 180) {
pos = MaxAngle;
}
if (pos <= 0) {
pos = MinAngle;
}
if (sleepMode == 1 && calcSpeedMain > 0.0) {
digitalWrite(ServoSwitch, HIGH);
for (servoCurrPos = myservo.read(); servoCurrPos <= 180; servoCurrPos++) {
myservo.write(servoCurrPos);
delay(4);
}
sleepMode = 0;
}
myservo.write(pos);
}
void prepareTurnOff() { //########################################### LOOP ############################################################
sleepMode = 1; //########################################### LOOP ############################################################
void loop() {
loopTime = millis();
display.clearDisplay(); display.clearDisplay();
display.setTextSize(1); // //DIAG Btn1
display.setCursor(0, 0); // display.setTextSize(1);
display.println("Przygotwywanie..."); // display.setCursor(0, 30);
display.display(); // display.println("Btn1:");
for (servoCurrPos = myservo.read(); servoCurrPos >= 60; servoCurrPos--) { // display.setCursor(30, 20);
myservo.write(servoCurrPos); // display.println(digitalRead(Btn1));
delay(15); // display.setCursor(30, 30);
// display.println(analogRead(Btn1));
// display.setCursor(55, 30);
display.setTextSize(3);
//################################################
//SPEED
sleepSpd = millis() - millissSpd;
if (sleepSpd >= 1000.0) { //podaj zerową prędkość jeśli nie było odcztu od 1,5 s
raw_speed = 0.0;
} }
//Na czas diagnostyki //przejście w tryb uśpienia za przuycisku lub czasu
// eeprom_write_block(&totalDist,totalDistMemLocation,2); if ((digitalRead(Btn2) == HIGH)|| (sleepSpd >= LongTimeToSleep)) {
// totalDistWrited = totalDist; prepareTurnOff();
// loop_cnt = 0; }
//
for (; 1500 < (millis() - lastMillisSpd);) { // zmiana z 1000 na 1500 w 1.13.19 calcSpeed();
digitalWrite(ServoSwitch, LOW); calcGear();
digitalWrite(ServoPin, LOW); displGear = 9 - currentGear;
//INFO setPosition(currentGear);
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 30); //duzy font
display.println("Mozna teraz"); //GEAR
display.setCursor(0, 38); display.setCursor(8, 0);
display.println("bezpiecznie wylaczyc"); display.println("G:");
display.setCursor(0, 46); display.setCursor(40, 0);
display.println("komputer."); display.print(currentGear);
display.setCursor(0, 0); //SPEED_TREND
display.setCursor(75, 00);
if (speedTrend <= -1 ) {
display.write(31); display.write(31);
display.setCursor(5, 0); } else {
display.println("+"); if (speedTrend >= 1) {
display.setCursor(10, 0); display.write(30);
display.write(30); } else {
display.setCursor(20, 0); display.println("-");
display.println(downs + ups); }
//Version
display.setCursor(0, 13);
display.println("V: ");
display.setCursor(10, 13);
display.println(Version);
//DST
display.setCursor(45, 0);
display.println("Spins: ");
display.setCursor(80, 0);
display.println(totalDistWrited);
display.setCursor(53, 13);
display.println("KM: ");
display.setCursor(90, 13);
display.println((totalDistWrited*WheelCircumference)/1000);
//RUN TIME
run_mins = floor((millis() / 1000) / 60);
run_hrs = floor(run_mins / 60);
run_mins = run_mins - (run_hrs * 60);
display.setCursor(60, 54);
display.println("T:");
display.setCursor(75, 54);
display.println(run_hrs);
display.setCursor(83, 54);
display.println(":");
display.setCursor(88, 54);
display.println(run_mins);
display.display();
delay(500);
} }
//SPEED
display.setCursor(8, 40);
display.println("S:");
display.setCursor(40, 40);
display.println(calcSpeedMain, 1);
//GearBar
if (pointerVisibility = 1){
display.fillRect(115, 32, 7, 1, SSD1306_WHITE);
display.fillRect(115, 0, 7, 1, SSD1306_WHITE);
display.fillRect(115, 63, 7, 1, SSD1306_WHITE);
display.setTextSize(2);
gearBarHeight = 64 - int(((calcSpeedMain - currentGearRangeLower) / (currentGearRangeUpper - currentGearRangeLower)) * 64) - 5;
display.setCursor(115, gearBarHeight);
display.write(16);
display.setTextSize(3);
}
//VOLT_BAR
adcBattVoltValue = analogRead(VoltInptPin);
measuredVoltage = (adcBattVoltValue * referenceVoltage) / maxADCValue;
inputVoltage = measuredVoltage * voltageDividerRatio;
voltBarHeight = int(((inputVoltage-9)/3)*64); //odjemowanie 9 bo to minimalne napiecie, podział przez 3 bo zakladam max napiecie 12.0V a nie 12.6V
voltBarPosition = 64 - voltBarHeight;
display.fillRect(0, voltBarPosition, 2, voltBarHeight, SSD1306_WHITE);
//########################################## ZAPIS DO WYŚWIETLACZA ######################################################
display.display();
loopTime = millis();
if ((digitalRead(Btn2) == HIGH)) {
digitalWrite(PinLED, HIGH);
}else {
digitalWrite(PinLED, LOW);
}
//wstrzymanie pętli by odczyty były co 0,5s
for (; (millis() - loopTime) < 300 ;) {
delay(10);
}
if (speedTrend == -1 ) {
if (BrakingLightSwitch == 1) {
digitalWrite(BrakingLight, HIGH);
BrakingLightSwitch = 0;
} else {
digitalWrite(BrakingLight, LOW);
BrakingLightSwitch = 1;
}
} else {
digitalWrite(BrakingLight, LOW);
}
} }
//########################################### LOOP ############################################################
//########################################### LOOP ############################################################