diff --git a/LeonardoProMicro/AutomatedGearShifter/AutomatedGearShifter.ino b/LeonardoProMicro/AutomatedGearShifter/AutomatedGearShifter.ino index 281cadd..e48f22a 100644 --- a/LeonardoProMicro/AutomatedGearShifter/AutomatedGearShifter.ino +++ b/LeonardoProMicro/AutomatedGearShifter/AutomatedGearShifter.ino @@ -1,4 +1,4 @@ - //#include +#include #include // model servo: DS3218 PRO #include #include @@ -6,8 +6,7 @@ #include #include -#define Version "1.11.3.1" -//ostatnia zmiana: zmiana na PCB, dostrajanie +#define Version "1.12.10" ////2DO: #define SCREEN_WIDTH 128 @@ -15,7 +14,8 @@ #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); -#define PinInSpeed 1 //on board: RXI +#define PinInterrupt 0 //on board: RX deklaracja pod stabilizacje TX +#define PinInSpeed 1 //on board: TX #define ServoSwitch 4 //on board: 4 #define ServoPin 8 //on board: 8 #define BrakingLight 9 //on board: 9 BrakingLight @@ -84,9 +84,11 @@ int downs = 0; int run_hrs = 0; int run_mins = 0; //DST -int totalDist = 0; -int totalDistReaded = 0; - +int totalDistMemLocation = 60; +int loop_cnt = 0; +unsigned int totalDist = 0; +unsigned int totalDistReaded = 0; +unsigned int totalDistWrited = 0; void setup() { //SERVO @@ -113,7 +115,8 @@ void setup() { display.display(); delay(500); //INPUT - pinMode(PinInSpeed, INPUT_PULLUP); + pinMode(PinInSpeed, INPUT); + pinMode(PinInterrupt, INPUT_PULLUP); pinMode(Btn1, INPUT); //OUTPUT pinMode(PinLED, OUTPUT); @@ -122,11 +125,6 @@ void setup() { //Interrupts attachInterrupt(digitalPinToInterrupt(PinInSpeed), calcSpeed, FALLING); - - // powolne ustawianie pozycji servo po włączeniu - // delay(750); - // setPosition(8); - display.clearDisplay(); delay(550); setPosition(7); @@ -153,9 +151,8 @@ void setup() { delay(30); } //MEMORY - eeprom_read_block(&totalDistReaded, 0, 2); + eeprom_read_block(&totalDistReaded, totalDistMemLocation, 2); totalDist = totalDistReaded; - } @@ -251,8 +248,15 @@ void loop() { } else { digitalWrite(BrakingLight, LOW); } - // //TTL DST - // eeprom_write_block(&totalDist,0,2); + //TTL DST +// totalDist = 0; + loop_cnt = loop_cnt +1; + if (loop_cnt >= 10 && totalDistWrited != totalDist ){ + eeprom_write_block(&totalDist,totalDistMemLocation,2); + totalDistWrited = totalDist; + loop_cnt = 0; + } + } @@ -266,11 +270,11 @@ void calcSpeed() { speed_last = speed; speed = (((2 * Pi) / sigleTimeSpd * ((WheelCircumference) / (Pi)) * ms2kmh)) / MagnetsCnt; // diag - // if (((speed_last_3 * 1.5) > speed) && speed > 10.0 && speedTrend > 0) { //zabezpieczenie przed losowymi sygnałami magesu - // speed = speed_last_3; - // } - // speed = (speed + speed_last)/2; - // totalDist = totalDist + 1; + if (((speed_last_3 * 1.5) > speed) && speed > 10.0 && speedTrend > 0) { //zabezpieczenie przed losowymi sygnałami magesu + speed = speed_last_3; + } + speed = (speed + speed_last)/2; + totalDist = totalDist + 1; } void calcGear() { @@ -401,7 +405,7 @@ void prepareTurnOff() { for (; 1000 < (millis() - lastMillisSpd);) { digitalWrite(ServoSwitch, LOW); digitalWrite(ServoPin, LOW); - // myservo.detach(ServoPin); + //INFO display.clearDisplay(); display.setTextSize(1); display.setCursor(0, 30); @@ -418,22 +422,34 @@ void prepareTurnOff() { 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 + eeprom_write_block(&totalDist,totalDistMemLocation,2); + totalDistWrited = totalDist; + loop_cnt = 0; + display.setCursor(45, 0); display.println("Spins: "); - display.setCursor(60, 0); - display.println(totalDist);; - display.setCursor(70, 0); - + 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(64, 54); + display.setCursor(60, 54); display.println("T:"); display.setCursor(75, 54); display.println(run_hrs); - display.setCursor(80, 54); + display.setCursor(83, 54); display.println(":"); - display.setCursor(85, 54); + display.setCursor(88, 54); display.println(run_mins); display.display(); delay(500);