Please help !
I have state machine programmed but the switch is not declared.
When i set it to zero. it didn't do anything.
enum State_enum {IDLE, FORWARD, BACK};
void motors_idle();
void motors_forward();
void motors_back();
int Sensor_Home = 10;
int Sensor_Button = 3;
int Distance = 0;
int Stepping = false;
int flag = 0;
void setup(){
Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
pinMode(Sensor_Home, INPUT);
pinMode(Sensor_Button,INPUT);
}
void loop()
{
switch(state)
{
case IDLE:
if(Sensor_Home == LOW){
motors_idle();
state = FORWARD;
}
else{
state = BACK;
}
break;
case FORWARD:
if(Sensor_Button == LOW && Sensor_Home == LOW){
motors_forward();
state = BACK;
}
break;
case BACK:
if(Sensor_Home == HIGH){
motors_back();
state = IDLE;
}
break;
}
}
void motors_idle()
{
//code for idle motor
if (Sensor_Home == LOW)
{
digitalWrite(8,LOW);
}
}
void motors_forward()
{
//when SENSOR_PRESS trigger
//motor move back til SENSOR_HOME pressed & motor forward until SENSOR_HOME unpressed
//drive motor forward to X-distance & return (LOOP CYCLE)
while (flag = 0)
{
if (Sensor_Button == LOW && Stepping == false) //SENSOR_PRESS triggered
{
Stepping = true;
}
while (Stepping == true)
{
//code for motor go to SENSOR_HOME
//then before code foor motor forward x-distance and return
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
Distance = Distance + 1; // record this step
// Check to see if we are at the end of our move
if (Distance == 9600) //Input Distance
{
// We are! Reverse direction (invert DIR signal)
if (digitalRead(8) == HIGH)
{
digitalWrite(8, LOW);
}
else
{
digitalWrite(8, HIGH);
flag = 1;
break;
}
delay(500);
}
}
}
}
void motors_back()
{
while(flag == 0)
{
if (Sensor_Home == HIGH && Stepping == false)
{
Stepping == true;
}
while(Stepping == true)
{
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
if (Sensor_Home == LOW) //LIMIT SWITCH ACTIVATED
{
// RUN MOTOR BACK TOWARD LIMIT SWITCH & PRESSED
// REVERSE DIRECTION & MOTOR FORWARD UNTIL LIMIT SWITCH UNPRESSED & STOP
digitalWrite(8,LOW);
Stepping == false;
flag = 1;
break;
}
}
}
}