diff --git a/ESP32/ESP_AutomatedGearShifter/ESP_AutomatedGearShifter.ino b/ESP32/ESP_AutomatedGearShifter/ESP_AutomatedGearShifter.ino index 34e8822..acec2bc 100644 --- a/ESP32/ESP_AutomatedGearShifter/ESP_AutomatedGearShifter.ino +++ b/ESP32/ESP_AutomatedGearShifter/ESP_AutomatedGearShifter.ino @@ -1,21 +1,12 @@ -// #include #include // model servo: DS3218 PRO - -// nie włącza serovo -// nie podaje napięcia z baterii - #include #include #include #include #include -#define Version "2.1.0" +#define Version "2.1.4" ////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 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 BrakingLight 13 //on board: D13 BrakingLight #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 VoltInptPin 23 //on board: D23 Battery Voltage +// #define VoltInptPin 23 //on board: D23 Battery Voltage prawdopodobnie uszkodzone ADC #define ServoMaxAngle 130 #define MaxAngle 179 @@ -51,6 +42,7 @@ Servo myservo; //SPEED +double readSignalTime_5; double readSignalTime_4; double readSignalTime_3; double readSignalTime_2; @@ -116,9 +108,9 @@ double totalDist = 0.0; unsigned int totalDistReaded = 0; unsigned int totalDistWrited = 0; //BATTERY -float referenceVoltage = 5.1; -int maxADCValue = 1023; -float voltageDividerRatio = 3.0; +float referenceVoltage = 3.3; +int maxADCValue = 4095; +float voltageDividerRatio = 5.7; int adcBattVoltValue = 0; float inputVoltage = 0.0; float measuredVoltage = 0.0; @@ -158,10 +150,10 @@ void setup() { pinMode(PinInSpeed, INPUT); // pinMode(PinInterrupt, OUTPUT); // digitalWrite(PinInterrupt, HIGH); - pinMode(Btn1, INPUT); + pinMode(VoltInptPin, INPUT); pinMode(Btn2, INPUT); - // pinMode(VoltInptPin, INPUT); + //OUTPUT pinMode(PinLED, OUTPUT); pinMode(BrakingLight, OUTPUT); @@ -201,138 +193,33 @@ void setup() { // totalDist = float(totalDistReaded); } -//########################################### LOOP ############################################################ -//########################################### LOOP ############################################################ -void loop() { - loopTime = millis(); - display.clearDisplay(); -// //DIAG Btn1 -// display.setTextSize(1); -// 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); +void setPosition(int currentGear) { + pos = 180 - round((currentGear - 1) * (ServoMaxAngle / (MaxGear - 1) )); + if (pos >= 180) { + pos = MaxAngle; + } - 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 - speed4Gear = 0.0; - speed4Gear_2 = 0.0; - 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(); - } - } + 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); } - - if (speed4Gear > 40 || isinf(speed4Gear)) { - speed4Gear = speed4Gear_3; + sleepMode = 0; } - calcSpeed(); - 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; -// } + myservo.write(pos); } -//########################################### LOOP ############################################################ -//########################################### LOOP ############################################################ + void readSpeed() { lastLastMillisSpd = lastMillisSpd; lastMillisSpd = millissSpd; millissSpd = millis(); + readSignalTime_5 = readSignalTime_4; + readSignalTime_4 = readSignalTime_3; readSignalTime_3 = readSignalTime_2; readSignalTime_2 = readSignalTime_1; readSignalTime_1 = double(millissSpd - lastLastMillisSpd) / 1000; @@ -342,6 +229,81 @@ void readSpeed() { } + +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); + } +//Na czas diagnostyki +// eeprom_write_block(&totalDist,totalDistMemLocation,2); +// totalDistWrited = totalDist; +// loop_cnt = 0; +// + 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); + display.setCursor(25, 0); + display.println(downs + ups); + //Version + display.setCursor(5, 13); + display.println("V: "); + display.setCursor(15, 13); + display.println(Version); + //DST + display.setCursor(50, 0); + display.println("Spins: "); + display.setCursor(85, 0); + display.println(totalDistWrited); + display.setCursor(58, 13); + display.println("KM: "); + display.setCursor(95, 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(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(){ calcSpeed3 = calcSpeed2; calcSpeed2 = calcSpeed1; @@ -396,89 +358,129 @@ void calcGear() { } -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() { - sleepMode = 1; +//########################################### LOOP ############################################################ +//########################################### LOOP ############################################################ +void loop() { + + loopTime = millis(); 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); +// //DIAG Btn1 +// display.setTextSize(1); +// 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); + + //################################################ + //SPEED + sleepSpd = millis() - millissSpd; + if (sleepSpd >= 1000.0) { //podaj zerową prędkość jeśli nie było odcztu od 1,5 s + speed4Gear = 0.0; + speed4Gear_2 = 0.0; + raw_speed = 0.0; } -//Na czas diagnostyki -// eeprom_write_block(&totalDist,totalDistMemLocation,2); -// totalDistWrited = totalDist; -// loop_cnt = 0; -// - 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(0, 30); - display.println("Mozna teraz"); - display.setCursor(0, 38); - display.println("bezpiecznie wylaczyc"); - display.setCursor(0, 46); - display.println("komputer."); - display.setCursor(0, 0); + //przejście w tryb uśpienia za przuycisku lub czasu + if ((digitalRead(Btn2) == HIGH)|| (sleepSpd >= LongTimeToSleep)) { + prepareTurnOff(); + } + + if (speed4Gear > 40 || isinf(speed4Gear)) { + speed4Gear = speed4Gear_3; + } + calcSpeed(); + 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); - display.setCursor(5, 0); - display.println("+"); - display.setCursor(10, 0); - display.write(30); - display.setCursor(20, 0); - 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); + } 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, 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 && 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 ############################################################