I don't immediately see any reason why the reverse() function won't be called.
I suspect the problem is that nowhere in the reverse() function do you use the value passed to it and stored in the variable v.
Separately, I can't see any reason to have separate functions for fwd and rev when the only difference is the setting of the direction pin. Why not have a single moveMotor() function that takes two parameters direction and number of steps?
Separately (again) it is more usual to write a FOR loop() like this
Separately, I can't see any reason to have separate functions for fwd and rev when the only difference is the setting of the direction pin. Why not have a single moveMotor() function that takes two parameters direction and number of steps?
yes i have to do some more stuff so i need to have two functions.
i have one other problem.
#include<Stepper.h>
int ms1 = 10;
int ms2 = 9;
int ms3 = 8;
int stepin = 6;
int dirnpin = 7;
int steps;
int reset = 4;
int k;
int value;
int stepstomoveback;
int fstepvalue;
void setup()
{
pinMode(reset,OUTPUT);
digitalWrite(reset,HIGH);
pinMode(ms1,OUTPUT);
pinMode(ms2,OUTPUT);
pinMode(ms3,OUTPUT);
pinMode(stepin,OUTPUT);
pinMode(dirnpin,OUTPUT);
Serial.begin(9600);
Serial.println("enter number of times u want to call the function");
}
void loop() {
digitalWrite(ms1,HIGH);
digitalWrite(ms2,HIGH);
digitalWrite(ms3,HIGH);
while(Serial.available()>0)
{
int number = Serial.parseInt();
Serial.println(number);
value = 100*number;
for(int k =0;k<=number;)
{ forward(100);
Serial.println(1);
delay(10);
k=k+1;
}
reverse(value);
}
}
int forward(int steps)
{
for(int j = 0;j<steps;j++)
{
digitalWrite(stepin,LOW);
digitalWrite(dirnpin,HIGH);
delay(5);
digitalWrite(stepin,HIGH);
digitalWrite(dirnpin,LOW);
delay(5);
}
Serial.println("forward spin");
}
int reverse(int steps)
{
for(int j = 0;j<steps;j++)
{
digitalWrite(stepin,LOW);
digitalWrite(dirnpin,HIGH);
delay(5);
digitalWrite(stepin,HIGH);
digitalWrite(dirnpin,LOW);
delay(5);
Serial.println("reverse spin");
}
}
here after i enter the value the reverse() function runs,i know why it happens.
is there any way to make the forward function run first and then make the reverse function run.
where can i put the condition for it?
Your code is very difficult to read. Please use the Auto Format tool to indent it consistently. That makes it much easier to see how the different parts relate to each other.
The reverse() function probably runs first when Serial.parseInt() produces a zero.
IMHO the program needs a complete re-write. Separate the business of getting serial data from the business of moving the motors. Receive the data (if there is any) and save it in a variable. Then check the value of the variable before deciding what to do with the motors. The code in loop() might be something like this