Frequency of loop, actualTime - storedTime

Hello
I'm using arduino for driving stepper motor with L298N shield. I managed to code some functions like fullstep, halfstep etc. I used switch to move between functions but after couple changes of case it brakes. i can still pick desired case but if statement with (actualTime - storedTime >= predkosc) doesnt seem to work anymore. Any suggestions ?

#include <LiquidCrystal.h>
#include <Bounce2.h>

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 6, 7, 8, 9);

int stepCount = 0;         // number of steps the motor has taken

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
int buttonPin=10;
int buttonNew;
int buttonOld=0;
int LCDState=0;
int dt=500;
int buttonPin2=13;
int controloption;
int count=0;
unsigned int predkosc;
int pot=A1;
const int Step1Pin=6;
const int Step2Pin=7;
const int Step3Pin=8;
const int Step4Pin=9;
int actualTime = 0;
int storedTime=0;
int stepNo=0;
int czujnik=0;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Bounce debouncer = Bounce();
void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  // Po konfiguracji przycisku, ustawienie działania funkcji Bounce :
  debouncer.attach(buttonPin);
  debouncer.interval(25); // interwał w ms
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.  
}
void loop()
{ 
  
  czujnik=analogRead(pot);
  predkosc = map(czujnik, 1, 1000, -10, 1000);
  Serial.println(predkosc);
  int button1_last = 0;
  int button1_state = digitalRead(buttonPin);
  int button2state=digitalRead(buttonPin2);
  if (button1_state != button1_last){//debouncer2
    delay(50);
    button1_state = digitalRead(buttonPin);
  }
  while (button1_state != button1_last)
  { 
    count++;
    storedTime=actualTime;
    button1_last = button1_state;
    delay(50);
    if (count >= 5) {
       count = 1;
    }
  }
 switch (count) {
     case 1: 
     
      lcd.print("sterowanie");
      lcd.println("pelnokrokowe");
   actualTime=millis();
   if (actualTime - storedTime >= predkosc) {
      if (stepNo == 1) {
        digitalWrite(Step1Pin, HIGH);
        digitalWrite(Step2Pin, LOW);
        digitalWrite(Step3Pin, LOW);
        digitalWrite(Step4Pin, HIGH);
      } if (stepNo == 2) {
        digitalWrite(Step1Pin, HIGH);
        digitalWrite(Step2Pin, LOW);
        digitalWrite(Step3Pin, HIGH);
        digitalWrite(Step4Pin, LOW);       
      } if (stepNo == 3) {
        digitalWrite(Step1Pin, LOW);
        digitalWrite(Step2Pin, HIGH);  
        digitalWrite(Step3Pin, HIGH);
        digitalWrite(Step4Pin, LOW);        
      } if (stepNo == 4) {
        digitalWrite(Step1Pin, LOW);
        digitalWrite(Step2Pin, HIGH);
        digitalWrite(Step3Pin, LOW);
        digitalWrite(Step4Pin, HIGH);
        stepNo = 0;
      }
       stepNo++;
      storedTime = actualTime;
    }
   
  if (button2state==0){
  lcd.clear();
  lcd.print("zalaczono funkcje");
   delay(1000);}
        break;
     case 2:       
  lcd.clear();
    lcd.print("sterowanie");
     lcd.println("falowe");
    actualTime=millis();
    if (actualTime - storedTime >= predkosc) {
      if (stepNo == 1) {
        digitalWrite(Step1Pin, LOW); //B+
        digitalWrite(Step2Pin, LOW);//B-
        digitalWrite(Step3Pin, HIGH);//A+
        digitalWrite(Step4Pin, LOW);//A-
      } if (stepNo == 2) {
        digitalWrite(Step1Pin, HIGH);
        digitalWrite(Step2Pin, LOW);
        digitalWrite(Step3Pin, LOW);
        digitalWrite(Step4Pin, LOW);
        
      } if (stepNo == 3) {
        digitalWrite(Step1Pin, LOW);
        digitalWrite(Step2Pin, LOW);  
        digitalWrite(Step3Pin, LOW);
        digitalWrite(Step4Pin, HIGH);
          
      } if (stepNo == 4) {
        digitalWrite(Step1Pin, LOW);
        digitalWrite(Step2Pin, HIGH);
        digitalWrite(Step3Pin, LOW);
        digitalWrite(Step4Pin, LOW);
        stepNo = 0;
      }
       stepNo++;
      storedTime = actualTime;
    }
           break;
     case 3:
        lcd.clear();
      lcd.print("silnik prawo"); 
        if (button2state==0){
  lcd.clear();
  lcd.print("zalaczono funkcje");
   //delay(1);
    myStepper.step(1);
  stepCount++;
  }
        break;
        case 4:
        lcd.clear();
  lcd.print("sterowanie"); 
  lcd.println("polkrokowe");
   actualTime=millis();
         if (actualTime - storedTime >= predkosc) 
     { 
      
      if (stepNo == 1) {              // A+B+  A-B+  A-B-  A+B-
        digitalWrite(Step1Pin, LOW); //B+           A+0  A+B+  0B+  A-B+  A-0  A-B- 0B- A+B- 
        digitalWrite(Step2Pin, LOW); //B-
        digitalWrite(Step3Pin, LOW);// A-
        digitalWrite(Step4Pin, HIGH); //A+
     
      } if (stepNo == 2) {
        digitalWrite(Step1Pin, HIGH);
        digitalWrite(Step2Pin, LOW);
        digitalWrite(Step3Pin, LOW);
        digitalWrite(Step4Pin, HIGH);
        
      } if (stepNo == 3) { 
        digitalWrite(Step1Pin, HIGH);
        digitalWrite(Step2Pin, LOW);  
        digitalWrite(Step3Pin, LOW);
        digitalWrite(Step4Pin, LOW);
          
      } if (stepNo == 4) { 
        digitalWrite(Step1Pin, HIGH);
        digitalWrite(Step2Pin, LOW);
        digitalWrite(Step3Pin, HIGH);
        digitalWrite(Step4Pin, LOW);
    
      }
      if (stepNo == 5) { 
        digitalWrite(Step1Pin, LOW);
        digitalWrite(Step2Pin, LOW);
        digitalWrite(Step3Pin, HIGH);
        digitalWrite(Step4Pin, LOW);
        
      }
       if (stepNo == 6) { 
        digitalWrite(Step1Pin, LOW);
        digitalWrite(Step2Pin, HIGH);
        digitalWrite(Step3Pin, HIGH);
        digitalWrite(Step4Pin, LOW);
    
      }
       if (stepNo == 7) { 
        digitalWrite(Step1Pin, LOW);
        digitalWrite(Step2Pin, HIGH);
        digitalWrite(Step3Pin, LOW);
        digitalWrite(Step4Pin, LOW);
    
      }
       if (stepNo == 8) { 
        digitalWrite(Step1Pin, LOW);
        digitalWrite(Step2Pin, HIGH);
        digitalWrite(Step3Pin, LOW);
        digitalWrite(Step4Pin, HIGH);
        
        stepNo = 0;
      }
       stepNo++;
      storedTime = actualTime;
    }
   
    break;
    
  }
}

Please edit your opening post, select all code and click the </> button to apply code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

Great you posted you code, not so great that you did not use code tags. Why not edit your code and put your code in code tags.

Sorry, It's my first topic. Thank you for advice. All done.

1 Like

not really sure what the specific problem is. i simulated your code and cleaned up some things.

the button processing: made INPUT_PULLUP, moved button1_last outside of loop() and simplified the logic (no need for "Bounce").

i moved reading millis() to the top of loop()

you include Stepper, but don't use it's functions. those sequences seem redundant

consider

#define MyHW
#ifdef MyHW
class Stepper {
  public:
    Stepper (int z, int a, int b, int c, int d)  {
        pinMode (a, OUTPUT);
        pinMode (b, OUTPUT);
        pinMode (c, OUTPUT);
        pinMode (d, OUTPUT);
    };

    void step (int a)   { };
};

const int Step1Pin=10;
const int Step2Pin=11;
const int Step3Pin=12;
const int Step4Pin=13;

class LiquidCrystal {
  public:
    LiquidCrystal (int rs, int en, int d4, int d5, int d6, int d7)  { };

    void begin   (int a, int b)   { };
    void clear   (void)           { };
#if 0
    void print   (const char *s)  { Serial.print (s); };
    void println (const char *s)  { Serial.println (s); };
#else
    void print   (const char *s)  { };
    void println (const char *s)  { };
#endif
};

class Bounce {
  public:
    Bounce ()   { };
    void attach   (int a)  { };
    void interval (int a)  { };
};

byte pot        = A0;
byte buttonPin  = A1;
byte buttonPin2 = A2;

// ---------------------------------------------------------
#else
#include <LiquidCrystal.h>
#include <Bounce2.h>
#include <Stepper.h>

const int Step1Pin=6;
const int Step2Pin=7;
const int Step3Pin=8;
const int Step4Pin=9;

int pot=A1;
int buttonPin=10;
int buttonPin2=13;
#endif

char s [80];

// -----------------------------------------------------------------------------
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, Step1Pin, Step2Pin, Step3Pin, Step4Pin);
int stepCount = 0; // number of steps the motor has taken

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
int buttonNew;
int buttonOld=0;
int LCDState=0;
int dt=500;
int controloption;
int count=0;
unsigned long predkosc;

unsigned long actualTime = 0;
unsigned long storedTime=0;
int stepNo=0;
int czujnik=0;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Bounce debouncer = Bounce();

void setup() {
    Serial.begin(9600);

    pinMode(buttonPin,  INPUT_PULLUP);
    pinMode(buttonPin2, INPUT_PULLUP);
    // Po konfiguracji przycisku, ustawienie dziaania funkcji Bounce :
    debouncer.attach(buttonPin);
    debouncer.interval(25); // interwa w ms
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
    // Print a message to the LCD.
}

int but1_last = HIGH;

void loop()
{
    actualTime = millis();
    czujnik  = analogRead(pot);
    predkosc = map(czujnik, 1, 1000, -10, 1000);
 // Serial.println(predkosc);

    int but1          = digitalRead(buttonPin);
    int button2state  = digitalRead(buttonPin2);

#if 0
    if (button1_state != button1_last){//debouncer2
        delay(50);
        button1_state = digitalRead(buttonPin);
    }

    while (button1_state != button1_last)
    {
        count++;
        storedTime=actualTime;
        button1_last = button1_state;
        delay(50);
        if (count >= 5) {
            count = 1;
        }
    }
#else
    if (but1_last != but1)  {
        but1_last = but1;
        delay (10);         // debounce

        if (LOW == but1)  {
            storedTime = actualTime;
            count = (count+1) % 6;
        }
    }
#endif

    sprintf (s, "cnt %3d, stepNo %3d, %8ld, %8ld, %8ld",
        count, stepNo, actualTime, storedTime, predkosc);
    Serial.println (s);

    switch (count) {
    case 1:
        lcd.print("sterowanie");
        lcd.println("pelnokrokowe");
        if ( (actualTime - storedTime) >= predkosc) {
            if (stepNo == 1) {
                digitalWrite(Step1Pin, HIGH);
                digitalWrite(Step2Pin, LOW);
                digitalWrite(Step3Pin, LOW);
                digitalWrite(Step4Pin, HIGH);
            }
            if (stepNo == 2) {
                digitalWrite(Step1Pin, HIGH);
                digitalWrite(Step2Pin, LOW);
                digitalWrite(Step3Pin, HIGH);
                digitalWrite(Step4Pin, LOW);
            }
            if (stepNo == 3) {
                digitalWrite(Step1Pin, LOW);
                digitalWrite(Step2Pin, HIGH);
                digitalWrite(Step3Pin, HIGH);
                digitalWrite(Step4Pin, LOW);
            }
            if (stepNo == 4) {
                digitalWrite(Step1Pin, LOW);
                digitalWrite(Step2Pin, HIGH);
                digitalWrite(Step3Pin, LOW);
                digitalWrite(Step4Pin, HIGH);
                stepNo = 0;
            }
            stepNo++;
            storedTime = actualTime;
        }
        if (button2state==0){
            lcd.clear();
            lcd.print("zalaczono funkcje");
            delay(1000);
        }
        break;

    case 2:
        lcd.clear();
        lcd.print("sterowanie");
        lcd.println("falowe");
        if (actualTime - storedTime >= predkosc) {
            if (stepNo == 1) {
                digitalWrite(Step1Pin, LOW); //B+
                digitalWrite(Step2Pin, LOW);//B-
                digitalWrite(Step3Pin, HIGH);//A+
                digitalWrite(Step4Pin, LOW);//A-
            }
            if (stepNo == 2) {
                digitalWrite(Step1Pin, HIGH);
                digitalWrite(Step2Pin, LOW);
                digitalWrite(Step3Pin, LOW);
                digitalWrite(Step4Pin, LOW);
            }
            if (stepNo == 3) {
                digitalWrite(Step1Pin, LOW);
                digitalWrite(Step2Pin, LOW);
                digitalWrite(Step3Pin, LOW);
                digitalWrite(Step4Pin, HIGH);
            }
            if (stepNo == 4) {
                digitalWrite(Step1Pin, LOW);
                digitalWrite(Step2Pin, HIGH);
                digitalWrite(Step3Pin, LOW);
                digitalWrite(Step4Pin, LOW);
                stepNo = 0;
            }
            stepNo++;
            storedTime = actualTime;
        }
        break;

    case 3:
        lcd.clear();
        lcd.print("silnik prawo");
        if (button2state==0){
            lcd.clear();
            lcd.print("zalaczono funkcje");
            //delay(1);
            myStepper.step(1);
            stepCount++;
        }
        break;

    case 4:
        lcd.clear();
        lcd.print("sterowanie");
        lcd.println("polkrokowe");
        if (actualTime - storedTime >= predkosc)
        {
            if (stepNo == 1) {              // A+B+  A-B+  A-B-  A+B-
                digitalWrite(Step1Pin, LOW); //B+           A+0  A+B+  0B+  A-B+  A-0  A-B- 0B- A+B-
                digitalWrite(Step2Pin, LOW); //B-
                digitalWrite(Step3Pin, LOW);// A-
                digitalWrite(Step4Pin, HIGH); //A+
            }
            if (stepNo == 2) {
                digitalWrite(Step1Pin, HIGH);
                digitalWrite(Step2Pin, LOW);
                digitalWrite(Step3Pin, LOW);
                digitalWrite(Step4Pin, HIGH);
            }
            if (stepNo == 3) {
                digitalWrite(Step1Pin, HIGH);
                digitalWrite(Step2Pin, LOW);
                digitalWrite(Step3Pin, LOW);
                digitalWrite(Step4Pin, LOW);
            }
            if (stepNo == 4) {
                digitalWrite(Step1Pin, HIGH);
                digitalWrite(Step2Pin, LOW);
                digitalWrite(Step3Pin, HIGH);
                digitalWrite(Step4Pin, LOW);
            }
            if (stepNo == 5) {
                digitalWrite(Step1Pin, LOW);
                digitalWrite(Step2Pin, LOW);
                digitalWrite(Step3Pin, HIGH);
                digitalWrite(Step4Pin, LOW);
            }
            if (stepNo == 6) {
                digitalWrite(Step1Pin, LOW);
                digitalWrite(Step2Pin, HIGH);
                digitalWrite(Step3Pin, HIGH);
                digitalWrite(Step4Pin, LOW);
            }
            if (stepNo == 7) {
                digitalWrite(Step1Pin, LOW);
                digitalWrite(Step2Pin, HIGH);
                digitalWrite(Step3Pin, LOW);
                digitalWrite(Step4Pin, LOW);
            }
            if (stepNo == 8) {
                digitalWrite(Step1Pin, LOW);
                digitalWrite(Step2Pin, HIGH);
                digitalWrite(Step3Pin, LOW);
                digitalWrite(Step4Pin, HIGH);
                stepNo = 0;
            }
            stepNo++;
            storedTime = actualTime;
        }
        break;
    }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.