Need help with Stepper Motor code!

Hi guys, i want to adjust my motor’s speed with serial monitor. And i want to spin my motor one time whenever i change the speed with serial monitor. But whenever i write any number such as 3,4,7,9 my speed doesnt change and serial moniter gives me same sumbers(52,49,51). I think these numbers are Can you guys help me to get through this. Thank you in advance.

#include <Wire.h>

int BLACK=2;
int BROWN=4;
int ORANGE=6;
int YELLOW=8;


void setup(){
  pinMode(BLACK,OUTPUT);
  pinMode(BROWN,OUTPUT);
  pinMode(ORANGE,OUTPUT);
  pinMode(YELLOW,OUTPUT);
  Serial.begin(9600);
}

void step1(){
  digitalWrite(BLACK,LOW);
  digitalWrite(ORANGE,HIGH);
  digitalWrite(BROWN,HIGH);
  digitalWrite(YELLOW,LOW);
}

void step2(){
  digitalWrite(BLACK,LOW);
  digitalWrite(ORANGE,LOW);
  digitalWrite(BROWN,HIGH);
  digitalWrite(YELLOW,HIGH);
}

void step3(){
  digitalWrite(BLACK,HIGH);
  digitalWrite(ORANGE,LOW);
  digitalWrite(BROWN,LOW);
  digitalWrite(YELLOW,HIGH);
}

void step4(){
  digitalWrite(BLACK,HIGH);
  digitalWrite(ORANGE,HIGH);
  digitalWrite(BROWN,LOW);
  digitalWrite(YELLOW,LOW);
}

void Stop(){
  digitalWrite(BLACK,LOW);
  digitalWrite(ORANGE,LOW);
  digitalWrite(BROWN,LOW);
  digitalWrite(YELLOW,LOW);
}


void forward(int s){
  
  step1();
  delay(s);
  step2();
  delay(s);
  step3();
  delay(s);
  step4();
  delay(s);
}

void loop(){
if (Serial.available()) {
int c = Serial.read();
Serial.println(c);
forward(c);  
}
}

Bciceksoy: if (Serial.available()) {

Try changing to this?

if (Serial.available() > 0) {

It will also print the same number because you are telling it to with Serial.println(c);

Still same. It gives me the keyboard number of key. When i press 1 key from keypad it gives 49, for 2 50 for 3 51.

OK, I'm no expert by any means, but instead of Serial.print(c); use Serial.write(c); will correct the 49,50,51 issue

I'm gonna have to get out my stepper but try this:

void forward(int s){
  
  if (Serial.available() > 0) {
  int s = Serial.read();
  }
  step1();
  delay(s);
  step2();
  delay(s);
  step3();
  delay(s);
  step4();
  delay(s);
}

void loop(){
if (Serial.available()) {
int s = Serial.read();
Serial.write(s);
forward(s);  
}

Guys thank you for your help but it is still same, the numbers are normal now but the speed is still same. :(

Bump

Why are you reading serial in loop() and again in forward() - it will give two completely different results.

Passing "s" to forward() is no use if you immediately ignore it and get a new value for s.

...R

Once you know that there is a byte to read, read the serial port, and store the data in a char, NOT an int.

Then, convert the char to a number by subtracting '0' form the digit you sent ('6' - '0' = 6).

Finally, ask your self if you can really tell the difference between a 1 millisecond pause between steps and a 9 millisecond pause.

When you are done pondering that, are you sending anything after the digit, like a carriage return and/or line feed?

Last, but by no means least, you aren't really expecting the Arduino to power a stepper motor, are you? Even the smallest ones require more power than the Arduino can provide.