1

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;
}
3
  • How many state variables do you want?! And it is quite correct. At no point do you ever declare those symbols.
    – Majenko
    Mar 15, 2019 at 13:22
  • Do you mean the different states the motor can be in? in that case 4 states in which case 1 and 2 and case 3 and 4 give the same result. Mar 15, 2019 at 13:25
  • No, I mean how many 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.
    – Majenko
    Mar 15, 2019 at 13:33

2 Answers 2

0

I have the feeling you need only one state which can have 4 values: L1, L2, R1 or R2.

For this, use an enum:

enum EState { L1, L2, R1, R2 };

Now create one state variable of that type and give it an initial value.

EState state = L1;

Some other problems I see at first sight:

case L1:
  if (state == L1){
  if (button1State == HIGH){
      state == L2;
  }
}
break;

In this statement, when there is case L1, it means the code following ( until the break) is only handled when the state (because of switch(state) is L1, so you can remove the check if (state == L1). What is left is:

case L1:
  if (button1State == HIGH)
  {
    state == L2;
  }
  break;

Than you use state == L2 to set a new state. However, the == only compares two values, if you want to assign it, you have to use a single =, thus the code should be:

case L1:
  if (button1State == HIGH)
  {
    state = L2;
  }
  break;

Of course you have to do this also for the other case statements.

Tip: align your code (make the { and } aligned under each other; this will make your code readability much better.

0

You have not defined L1, L2, R1, or R2. You're also redefining the int variable 'state' four times.

Try replacing:

int state = L1;
int state = L2;
int state = R1;
int state = R2;

With something like:

#define L1  1
#define L2  2
#define R1  3
#define R2  4
int state = R2;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.