I have a project where I have to control a DC motor. In this code the purpose is that every time you press a button the direction of the motor changes.
I only get the error that L1 (and L2,R1 and R2) are not declared in this scope. the place in the loop. How do I define the variables so that the code works?
int Motorforward = 10; // MotorDirectionPin set to pin10
int Motorbackward = 11; // MotorDirectionPin set to pin11
const int button1Pin = 2; //button control set to pin 2
int state = L1;
int state = L2;
int state = R1;
int state = R2;
int button1State; // current reading from the input pin
int previous = LOW; //the previous reading from the input pin
char input;
void setup(){
pinMode(button1Pin, INPUT); //set pin as input
Serial.begin(9600);
pinMode(Motorforward, OUTPUT); // sets the pin as output
pinMode(Motorbackward, OUTPUT);
}
void loop(){
button1State = digitalRead(button1Pin);
input = button1State;
switch(state){
case L1:
if (state == L1){
if (button1State == HIGH){
state == L2;
}
}
break;
case L2:
if (state == L2){
if (button1State == LOW){
state == R1;
}
}
break;
case R1:
if (state == R1){
if (button1State == HIGH){
state == R2;
}
}
break;
case R2:
if (state == R2){
if (button1State == LOW){
state == L1;
}
}
break;
}
state
variables do you want?! And it is quite correct. At no point do you ever declare those symbols.state
variables do you want? You define the same variable 4 times, each time with a different assignment, and an assignment to a variable or macro that you haven't defined at that.