diff --git a/code_polisseur/code_polisseur.ino b/code_polisseur/code_polisseur.ino index 83e38a6..86c372e 100644 --- a/code_polisseur/code_polisseur.ino +++ b/code_polisseur/code_polisseur.ino @@ -47,7 +47,8 @@ RotaryEncoder encoder(ROT_ENC_1, ROT_ENC_2); AccelStepper stepper(1, STEPPER_STEP, STEPPER_DIRECTION); -EveryTimer timer; +EveryTimer cntdwn; +EveryTimer calc_dspl; //Global variables @@ -74,42 +75,42 @@ static bool currBut = true; byte mode = 0; void countdown_callback() { - if (current_rpm != 0) { + if (current_rpm != 0) { // if speed = 0 => pause countdown countdown--; if (countdown < 0) { countdown = 0; - } - hours = countdown / 3600; - minutes = (countdown / 60) % 60; - seconds = countdown % 60; - - // Update data to be displayed - if (mode == 0) { // mode 0 = display speed - digitalWrite(SEVSEG_COLON, false); - sevseg.setNumber(current_rpm); - } - else if (mode == 1) { // mode 1 = display countdown - //Blink colon each seconds - if (seconds % 2) { - digitalWrite(SEVSEG_COLON, true); - } - else { - digitalWrite(SEVSEG_COLON, false); - } - //Display HH:MM or MM:SS if there is less than 1h left - if (hours) { - sevseg.setNumber(100*hours+minutes); - } else { - sevseg.setNumber(100*minutes+seconds); - } } - Serial.println(countdown); + recalculate_HMS(); + //Serial.println(countdown); } else { Serial.println("Pause"); } } +void calculate_display_callback() { + // Update data to be displayed + if (mode == 0) { // mode 0 = display speed + digitalWrite(SEVSEG_COLON, false); + sevseg.setNumber(current_rpm); + } + else if (mode == 1) { // mode 1 = display countdown + //Blink colon each seconds + if (countdown % 2) { + digitalWrite(SEVSEG_COLON, true); + } + else { + digitalWrite(SEVSEG_COLON, false); + } + //Display HH:MM or MM:SS if there is less than 1h left + if (hours) { + sevseg.setNumber(100*hours+minutes); + } else { + sevseg.setNumber(100*minutes+seconds); + } + } +} + void update_encoder() { static long currVal = 0; int multiplier = 10; @@ -117,36 +118,42 @@ void update_encoder() { encoder.tick(); long newVal = encoder.getPosition(); + int dir = encoder.getDirection(); if (currVal != newVal) { currVal = newVal; - if (mode == 0) { + if (mode == 0) { // Mode 0 : speed setting if (current_rpm >= 500) { - multiplier = 100; + multiplier = 100; // default : 1 encoder step = +/- 10 step/min + // if speed > 500 : encoder step = +/- 100 step/min } - new_rpm += multiplier * encoder.getDirection(); - if (new_rpm < STEPPER_MIN_SPEED) { + new_rpm += multiplier * dir; + if (new_rpm < STEPPER_MIN_SPEED) { new_rpm = STEPPER_MIN_SPEED; } - if (new_rpm > STEPPER_MAX_SPEED) { + if (new_rpm > STEPPER_MAX_SPEED) { new_rpm = STEPPER_MAX_SPEED; } } - if (mode == 1) { - multiplier = 60; + if (mode == 1) { //mode 1 : countdown setting + multiplier = 60; // default : 1 encoder step = +/- 1min + // if countdown > 1h : encoder step = +/- 1h if (countdown >= 3600) { multiplier = 600; } - countdown += multiplier * encoder.getDirection(); + countdown += multiplier * dir; if (countdown < 0) { countdown = 0; } + + // Max: 86400s = 1 day if (countdown >= 86400) { countdown = 86399; } + recalculate_HMS(); } Serial.print("RPM: "); - Serial.print(current_rpm); + Serial.print(new_rpm); Serial.print(" | Countdown: "); Serial.println(countdown); } @@ -174,8 +181,10 @@ void update_encoder() { } void update_stepper() { + + //Update speend setting and ENABLE pin if new speed value if (new_rpm != current_rpm) { - new_rpm = current_rpm; + current_rpm = new_rpm; stepper.setSpeed(current_rpm); // Enable/Disable motor if (current_rpm == 0) { @@ -186,12 +195,23 @@ void update_stepper() { stepper.enableOutputs(); Serial.println("Stepper : On"); } - - //if Countdown = 0 => pause (no move but keep speed setting) - if (countdown != 0 and current_rpm != 0) { - stepper.runSpeed(); - } } + //if Countdown = 0 => pause (no move but keep speed setting) + if (countdown != 0 and current_rpm != 0) { + //Serial.println("Run motor !"); + stepper.runSpeed(); + } +} + +void recalculate_HMS() { + minutes = (countdown / 60) % 60; + if (countdown >= 3600) { + hours = countdown / 3600; + } + else { + hours = 0; + seconds = countdown % 60; + } } void setup() @@ -210,20 +230,21 @@ void setup() stepper.setAcceleration(STEPPER_ACCELERATION); stepper.setSpeed(0); - timer.Every(1000, countdown_callback); + cntdwn.Every(1000, countdown_callback); + calc_dspl.Every(SEVSEG_REFRESH, calculate_display_callback); } -void loop() -{ +void loop() { update_encoder(); - - //Update countdown - timer.Update(); //Make the motor move update_stepper(); + //Update countdown + cntdwn.Update(); + //refresh the 7-segment display + calc_dspl.Update(); sevseg.refreshDisplay(); }