How to assign the speed rate of stepper motor ?

I’m doing a program which using one push button to count the speed mode of stepper motor, which means when we press once push button, the stepper motor rotate in speed mode 1, press 2nd time in 2nd speed mode , press 3rd time work in 3rd speed mode and this will repeat again from the beginning 1st speed mode.
however, i try to use 3 leds to indicate which motor speed mode going to be used but it failed.

below is my coding of trial.
please help ^^ TQ

sketch_jun30a.ino (564 Bytes)

I see only 2 LEDs.

Maybe seeing what your program is really doing will help:

byte leds[] = {8, 12};
byte speedchoicePin = 2;

void setup() {
  for (int i = 0; i < 2; i++)
  {
    pinMode(leds[i], OUTPUT);
  }
  pinMode(speedchoicePin, INPUT);
}

void loop()
{
  for (int speedc = 0; speedc < 2; speedc++) {
    if (digitalRead(speedchoicePin) == HIGH)
    {
      digitalWrite(leds[speedc], HIGH);
    }
  }
}

however, i try to use 3 leds to indicate which motor speed mode going to be used but it failed.

No, it did not fail. The code did something. You expected it to do something. If what it did was not what you expected, your expectations are wrong.

But, we can’t help with your expectations because you haven’t told us what the code actually does or what you expected it to do.

Do NOT post your code as an attachment when it can be posted inline:

int led1 = 8;
int led2 = 12;
int speedchoice = 2;
int speedc = 0;


void setup() {
  // put your setup code here, to run once:
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);

  pinMode(speedchoice,INPUT);
}

void loop() {
  for(int speedc=0; speedc<2;speedc++){
    if (speedc == 0){
          if (digitalRead(speedchoice) == HIGH){
            digitalWrite(led1,HIGH);
          }
     }
    else if (speedc == 1){
      if (digitalRead(speedchoice) == HIGH){
              digitalWrite(led2,HIGH);
            }
      }
     else{
      break;
     }
    }
}

It is silly to use a for loop when, for every iteration of the loop, you expect it to do something different.

The only comments in your code are useless ones. Get rid of the useless ones and put comments in to describe what you are thinking the code is supposed to do.

I’m so sorry for faulties i made. please be patient with me cause im a newbie with programming.
i’m try to use those leds to indicate the speed we choose, but when i run through the program it failed to light up one by one. is it means i should seperate the loops i’ve written?

int led1 = 8; //initialize pin led 1
int led2 = 12; //initialize pin led 2
int speedchoice = 2; //initialise pin to choose the different speed mode
int speedc = 0; //initialize the count for speed choice

void setup() {
// put your setup code here, to run once:
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);

pinMode(speedchoice,INPUT);
}

void loop() {
for(int speedc=0; speedc<2;speedc++){ //for the count of speed choice increase one by one when the speedchoice pin == high
if (speedc == 0){
if (digitalRead(speedchoice) == HIGH){ //if speed choice is high, for speed mode 1
digitalWrite(led1,HIGH); //light up led 1
}
}
else if (speedc == 1){ //if spped choice is high for speed mode 2
if (digitalRead(speedchoice) == HIGH){ //light up led 2
digitalWrite(led2,HIGH);
}
}
else{
break; //expect the previous count is save and looping again.
}
}
}

Please use the code button </> so your code looks like the following and is easy to copy to a text editor. See How to use the Forum

int led1 = 8;             //initialize pin led 1
int led2 = 12;      //initialize pin led 2
int speedchoice = 2;  //initialise pin to choose the different speed mode
int speedc = 0;      //initialize the count for speed choice


void setup() {
  // put your setup code here, to run once:
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);

  pinMode(speedchoice,INPUT);
}

void loop() {
  for(int speedc=0; speedc<2;speedc++){           //for the count of speed choice increase one by one when the speedchoice pin == high
    if (speedc == 0){   
          if (digitalRead(speedchoice) == HIGH){    //if speed choice is high, for speed mode 1
            digitalWrite(led1,HIGH);                //light up led 1
          }
     }
    else if (speedc == 1){                          //if spped choice is high for speed mode 2
      if (digitalRead(speedchoice) == HIGH){        //light up led 2
              digitalWrite(led2,HIGH);
            }
      }
     else{
      break;                                        //expect the previous count is save and looping again.
     }
    }
}

…R

You are not going about the problem the correct way. Your code should be something like this pseudo code

if ButtonPressed
   increase speedc
   if speedc > 1
      speedc = 0 // assuming it should only be 0 or 1

if speedc == 0
    turn off LED2
    light LED1

if speedc == 1
   turn off LED1
   light LED2

Have a look at Planning and Implementing a Program

...R

thanks for your help and advices~ i'm now clear of how to write it clearer ^^