AccelStepper + Countdown timer..... Here we go again

Hello everyone

I am having a mental breakdown with this.
I have a sketch where i need to rotate a stepper at a certen speed for a certen amount of time and for that i am using AccelStepper.h with a Countdown timer.

My 1st teste with just the stepper i can run it prefectly and at the speed i need.

/* Example sketch to control a stepper motor with L298N motor driver, Arduino UNO and AccelStepper.h library. Contiuous rotation. More info: https://www.makerguides.com */
// Include the AccelStepper library:
#include <AccelStepper.h>

// Define the AccelStepper interface type:
#define MotorInterfaceType 4

// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(MotorInterfaceType, 8, 9, 10, 11);
void setup() {
  // Set the maximum speed in steps per second:
  stepper.setMaxSpeed(1000);
}


void loop() {
  // Set the speed of the motor in steps per second:
  stepper.setSpeed(500);
  // Step the motor with constant speed as set by setSpeed():
  stepper.runSpeed();
}

but all goes terrebly wrong when i add the same code to my countdown and the stepper stops working properly and it mover verry slowly and not responding to the speed value at all

So you showed up the code that works, but you didn't show us the code that doesn't work? How are we to help you?

/***********************************************************
Curing/Washing Box by Xmodpt
Last update @ 15/04/2020  Current Ver: 1.04

****** Version Control *******
1.00 - Countdown tested and working with relay 
1.01 - Added menu with 3 options
1.03 - Change to main menu, for 2 options and added a sub-menu for each of the 1st menu options
1.04 - Added EndStop Buzzer (using PIN7)
***********************************************************/
#include <Arduino.h>
/****** U8G2 LIB *********/
#include <U8g2lib.h>
/***** U8G2 settings *****/
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
// Include the AccelStepper library:
#include <AccelStepper.h>
// Define the AccelStepper interface type:
#define MotorInterfaceType 4
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(MotorInterfaceType, 8, 9, 10, 11);

/**** U8G2 Display settings *****/
//U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

/********* Timer Settings ******************/

// 10000ul =   10 sec
// 30250ul =   30 sec
// 60500ul =   1 min
// 301500ul =  5 min
// 600000ul = 10 min
// 900000ul = 15 min
long int arrayTimer [4] = {301500ul, 600000ul, 900000ul, 10000ul}; // set of values 
#define pinRele 6  // Relay Pin Set


char
    szString[20];
byte
    mins, secs;
       
unsigned long
    timeTemp,
    timeNow,
    timeStart,
    timeElapsed,
    timeLeft,
    COUNTDOWN_TIME ;   

// End of constructor list

const char *string_list =
  "Curing\n"
  "Washing\n"
  "\n"
  ".:: About ::.";

const char *string_times =
  "05 min\n"
  "10 min\n"
  "15 min\n"
  "\n"
  ".:: Back ::.";
int buzzer = 7;
uint8_t current_selection = 1;
uint8_t current_selection_times = 1;



void setup(void) {
// ************** STEPPER SETTINGS ****************** 
     stepper.setMaxSpeed(1000);
     //stepper.move(800);  
     stepper.setSpeed(500); 

    pinMode(buzzer,OUTPUT);

// ************** RELAY SETTINGS ********************
     pinMode(pinRele, OUTPUT);
     digitalWrite(pinRele, HIGH);
// ************** Rotary encoder or buttons *********
     u8g2.begin(/*Select=*/ A2, /*Right/Next=*/ A4, /*Left/Prev=*/ A5, /*Up=*/ A0, /*Down=*/ A1, /*Home/Cancel=*/ A3); 
// ************** Font Type config for menu *********
     u8g2.setFont(u8g2_font_6x12_tr);
     u8g2.clearDisplay();
    // Serial.begin(9600);
}


void loop(void) {

    u8g2.setFont(u8g2_font_6x12_tr);
    current_selection = u8g2.userInterfaceSelectionList(
    "Curing & Washing OS",
    current_selection,
    string_list);
  //****************** CURING MENU *****************  
  if ( current_selection == 1 ) {
    u8g2.setFont(u8g2_font_6x12_tr);
    current_selection_times = u8g2.userInterfaceSelectionList(
    "Curing & Washing OS",
    current_selection,
    string_times);
            if ( current_selection_times == 1 ) { //5min selection Curing UV + Stepper 10rpm    
              timeStart = millis();
              mins = 1;
              secs = 1;
              
              COUNTDOWN_TIME = arrayTimer [3]; // Set for 5 min
              
                while (mins > 0 || secs > 0){
               DoCountdown();                
               relay_action();
               //stepper_motor(); // <-------------------------- this is my problem
                } 
                buzzer_alarm();
                
            }
            if ( current_selection == 1 && current_selection_times == 2 ) { //10min selection Curing UV + Stepper 10rpm   
              timeStart = millis();
              mins = 1;
              secs = 1;

              COUNTDOWN_TIME = arrayTimer [1]; // Set for 10 min
                while (mins > 0 || secs > 0){
                DoCountdown();                
                relay_action();
                } 
                buzzer_alarm();
            }
              if ( current_selection_times == 3 ) { //15min selection Curing UV + Stepper 10rpm      
          timeStart = millis();
              mins = 1;
              secs = 1;

              COUNTDOWN_TIME = arrayTimer [2]; // Set for 15 min
                while (mins > 0 || secs > 0){
                DoCountdown();                
                relay_action();
                } 
                buzzer_alarm();
            }
    
  }
  //****************** CURING END ******************
  //**************** WASHING  MENU *****************
  if ( current_selection == 2 ) {
        u8g2.setFont(u8g2_font_6x12_tr);
    current_selection_times = u8g2.userInterfaceSelectionList(
    "Curing & Washing OS",
    current_selection,
    string_times);
 
               if ( current_selection_times == 1 ) { // 5min selection Washing Stepper 100rpm    
              timeStart = millis();
              mins = 1;
              secs = 1;
     
              COUNTDOWN_TIME = arrayTimer [0]; // Set for 5 min
                while (mins > 0 || secs > 0){
                DoCountdown();                
                relay_action();
                } 
                buzzer_alarm();
            }
              if ( current_selection_times == 2 ) { // 10min selection Washing Stepper 100rpm     
                timeStart = millis();
                mins = 1;
                secs = 1;
                COUNTDOWN_TIME = arrayTimer [1]; // Set for 10 min
                  while (mins > 0 || secs > 0){
                DoCountdown();                
                relay_action();
                } 
                buzzer_alarm();             
             } 
           if ( current_selection_times == 3 ) { //15min selection Washing Stepper 100rpm      
              timeStart = millis();
              mins = 1;
              secs = 1;
              COUNTDOWN_TIME = arrayTimer [2]; // Set for 15 min
                while (mins > 0 || secs > 0){
                DoCountdown();                
                relay_action();
                } 
                buzzer_alarm();
             }
         }
  //****************** WASHING MENU END *****************
  
  if ( current_selection == 4 ) {
    u8g2.userInterfaceMessage(
  "About:",
  "Curing & Washing OS",
  "design by Xmodpt",
  "ver: 1.04");
    }
}
void DoCountdown()
{
  
   //u8g2.clearBuffer();
    static unsigned long
        lastTimeNow = 0;
    static byte
        lastsecs = 1;
       
    timeNow = millis();
    timeElapsed = timeNow - timeStart;
   
    if( mins == 0 && secs == 0 )
        return;
       
    timeLeft = COUNTDOWN_TIME - timeElapsed;

    mins = (byte)(timeLeft / 60000ul);
    timeTemp = timeLeft - (mins * 60000);
    secs = (byte)(timeTemp / 1000ul);
    timeTemp = timeTemp - (secs * 1000ul);

    if( mins == 0 && secs == 0 )
    {
        buzzer_alarm();
        buzzer_alarm();
        buzzer_alarm();
        u8g2.setFont(u8g2_font_helvB24_tf );
        u8g2.clearDisplay();
        u8g2.setCursor( 20, 45 );
        sprintf( szString, "DONE" );
        u8g2.print( szString );
   //     Serial.println( szString );
       // delay (2000);
    
    }
    else if( secs != lastsecs )
    {
        u8g2.setFont(u8g2_font_helvB24_tf );
        lastsecs = secs;
        u8g2.setCursor( 20,45 );
        sprintf( szString, "%02d:%02d", mins, secs );
   //     Serial.println( szString );
        u8g2.clearBuffer();
        u8g2.print( szString );
        
     }
   
   u8g2.sendBuffer();
  
}
//****** VOIDS ********//

//********** AccelStepper **************
void stepper_motor() {  
   if ( mins == 0 && secs == 0 ) { 
    
       stepper.stop();
      
   } else {
 
       stepper.runSpeed();
   }
}
//**************************************

void relay_action(){
  
  if ( mins == 0 && secs == 0 ) { 
    
      digitalWrite(pinRele, HIGH);
      
      
   } else {
       digitalWrite(pinRele, LOW);
       stepper.runSpeed();
   }

}

void buzzer_alarm() {
    // Sounds the buzzer at the frequency relative to the note C in Hz
    tone(buzzer,5000, 500);    
    // Waits some time to turn off
    delay(1100);
    //Turns the buzzer off
    noTone(buzzer); 
    // Sounds the buzzer at the frequency relative to the note D in Hz   
   return;
}

can anyone cast any light in this issue ?

Note:
i have tested without the display lib and just print to Serial and it’s the same.
originaly AccelStepper uses PINs 2,3,4,5 but it was sujested to use 8,9,10,11 to take advantage of
arduinos Timer1

Kind Regards to you all

blh64:
So you showed up the code that works, but you didn't show us the code that doesn't work? How are we to help you?

Sorry m8 i had to wait 5 min to ba able to make a new post

any hint :frowning:

Your timing code seems to be horribly complicated. Why not just use millis() or, if you prefer, create a variable that you increment every second (using millis). Then just measure each action as number of seconds - for example

if (secondsVal - startTime >= numberOfSecondsForThisAction) {
   // do something
}

…R

PS … if you must refer to people as “mate” please have the kindness to spell it out properly.

Hello Robin2

Thank you for your hint.

In relation to my spelling i am really sorry if i have ofended you. it was never the intent.

Regards