Variable does'n change value

Hi, I’m sorry for my English.
I’m doing a program to control the camera slider, I need to control the stepper motor. At each step, I need change variable stepnum to the next step number. But when the program runs, the variable remains the same without change.

Sorry in the code are words in Czech language.

int stepnum = 1;
/* directions
 *  1 = right
 *  2 = left
 *  
 *  power
 *  0 = no power
 *  1 = max power
 *  
 */
#include <QC3Control.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
QC3Control quickCharge(12, 13); //qc pin 12 = data+, pin 13 = data-
LiquidCrystal_I2C lcd(0x27,16,2);

//deklarace piny a stale promenné

const int EndButtonRight = 9; //koncak pravej
const int EndButtonLeft = 10; //koncak levej
const int MotorPINA1 = 3;
const int MotorPINA2 = 11;
const int MotorPINB1 = A1;
const int MotorPINB2 = A0;
const int DataPlusPIN = 12;
const int DataMinusPIN = 13;


//deklarace promennych

int Speed = MaxSpeed;
int LastEndButtonLeft;
int LastEndButtonRight;
int Direction = 1;
int stepdelay;

//////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////PŘÍKAZY///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//motorWrite
void motorWrite(){
  stepdelay = stepdelay + 1;
  if(Direction == 1){
    if(stepdelay >= Speed){
      
      if(stepnum == 1){
        stepnum = 2;
        digitalWrite(MotorPINA1, HIGH);
        digitalWrite(MotorPINA2, LOW);
        digitalWrite(MotorPINB1, LOW);
        digitalWrite(MotorPINB2, LOW);
        stepdelay = 0;
      }
      if(stepnum == 2){
        digitalWrite(MotorPINA1, LOW);
        digitalWrite(MotorPINA2, LOW);
        digitalWrite(MotorPINB1, HIGH);
        digitalWrite(MotorPINB2, LOW);
        stepnum = 3;
        stepdelay = 0;
      }
      if(stepnum == 3){
        digitalWrite(MotorPINA1, LOW);
        digitalWrite(MotorPINA2, HIGH);
        digitalWrite(MotorPINB1, LOW);
        digitalWrite(MotorPINB2, LOW);
        stepnum = 4;
        stepdelay = 0;
      }
      if(stepnum == 4){
        digitalWrite(MotorPINA1, LOW);
        digitalWrite(MotorPINA2, LOW);
        digitalWrite(MotorPINB1, LOW);
        digitalWrite(MotorPINB2, HIGH);
        stepnum = 1;
        stepdelay = 0;
      }
    }
  }
  if (Direction == 2){
    if (stepdelay >= Speed){
      if (stepnum == 1){
        digitalWrite(MotorPINA1, HIGH);
        digitalWrite(MotorPINA2, LOW);
        digitalWrite(MotorPINB1, LOW);
        digitalWrite(MotorPINB2, LOW);
        stepnum = 4 ;
        stepdelay = 0;
      }
      if (stepnum == 2){
        digitalWrite(MotorPINA1, LOW);
        digitalWrite(MotorPINA2, LOW);
        digitalWrite(MotorPINB1, HIGH);
        digitalWrite(MotorPINB2, LOW);
        stepnum = 1 ;
        stepdelay = 0;
      }
      if (stepnum == 3){
        digitalWrite(MotorPINA1, LOW);
        digitalWrite(MotorPINA2, HIGH);
        digitalWrite(MotorPINB1, LOW);
        digitalWrite(MotorPINB2, LOW);
        stepnum = 2 ;
        stepdelay = 0;
      }
      if (kolo == 4){
        digitalWrite(MotorPINA1, LOW);
        digitalWrite(MotorPINA2, LOW);
        digitalWrite(MotorPINB1, LOW);
        digitalWrite(MotorPINB2, HIGH);
        stepnum = 3 ;
        stepdelay = 0;
      }
    }
  }
}


//nastaveni vstupu a vystupu
void setup() {
  quickCharge.set12V();
  TCCR2B = TCCR2B & 0b11111000 | 0x03;
  pinMode(EndButtonRight, INPUT);
  pinMode(EndButtonLeft, INPUT);
  pinMode(MotorPINA1, OUTPUT);
  pinMode(MotorPINA2, OUTPUT);
  pinMode(MotorPINB1, OUTPUT);
  pinMode(MotorPINB2, OUTPUT);



  digitalWrite(MotorPINA1, LOW);
  digitalWrite(MotorPINA2, LOW);
  digitalWrite(MotorPINB1, LOW);
  digitalWrite(MotorPINB2, LOW);
 
}

void loop() {
  int CurrentEndButtonLeft = digitalRead(EndButtonLeft);
  int CurrentEndButtonRight = digitalRead(EndButtonRight);


  
  //funkce rychlosti a smeru
  if (CurrentEndButtonLeft != HIGH && CurrentEndButtonRight != HIGH){
    motorWrite();
  }
  
  LastEndButtonLeft = CurrentEndButtonLeft;
  LastEndButtonRight = CurrentEndButtonRight;
}

This is short version of my code because full code have up to 9000 characters

Look at this section of code carefully

      if (stepnum == 1)
      {
        stepnum = 2;
        digitalWrite(MotorPINA1, HIGH);
        digitalWrite(MotorPINA2, LOW);
        digitalWrite(MotorPINB1, LOW);
        digitalWrite(MotorPINB2, LOW);
        stepdelay = 0;
      }
      if (stepnum == 2)
      {
        digitalWrite(MotorPINA1, LOW);
        digitalWrite(MotorPINA2, LOW);
        digitalWrite(MotorPINB1, HIGH);
        digitalWrite(MotorPINB2, LOW);
        stepnum = 3;
        stepdelay = 0;
      }
      if (stepnum == 3)

If stepnum is equal to 1 it will be set to to 2 but then the second if will be true and stepnum will be set to 3 and the third if will be true and so on

Do you see a problem ?

hint: use if else if else if else ...

UKHeliBob:
Look at this section of code carefully

      if (stepnum == 1)

{
        stepnum = 2;
        digitalWrite(MotorPINA1, HIGH);
        digitalWrite(MotorPINA2, LOW);
        digitalWrite(MotorPINB1, LOW);
        digitalWrite(MotorPINB2, LOW);
        stepdelay = 0;
      }
      if (stepnum == 2)
      {
        digitalWrite(MotorPINA1, LOW);
        digitalWrite(MotorPINA2, LOW);
        digitalWrite(MotorPINB1, HIGH);
        digitalWrite(MotorPINB2, LOW);
        stepnum = 3;
        stepdelay = 0;
      }
      if (stepnum == 3)




If stepnum is equal to 1 it will be set to to 2 but then the second if will be true and stepnum will be set to 3 and the third if will be true and so on

Do you see a problem ?

Yes, you are god Věry thanks to you

Reordering the ifs (test highest value first in your scheme) would work also. :wink:

Indeed - but At the cost of unnecessary tests once a value is matched

Whandall:
Reordering the ifs (test highest value first in your scheme) would work also. :wink:

Except for stepnum == 4 which would also trigger stepnum == 1.