Problem with 'buttons' was not declared in this scope

Hi!

We are doing a school project trying to get a machine state working. We have some states and different kinds of button states. We are running into a problem in the BACKWARD case (the last one). There we can’t get through the line “if(buttons == NONE){” which we find very strange cause it works for the two cases before that… Does anyone know what the problem could be?

The code is below, I have commented the line the error occurs…

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

#define CE_PIN   7 //byta?
#define CSN_PIN 8

const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio


enum State_enum {STOP, FORWARD, BACKWARD};
enum Buttons_enum {NONE, BUTTON_FORWARD, BUTTON_BACKWARD};

int BUTTON_FORWARD_pin = 4;
int BUTTON_BACKWARD_pin = 2;

int high_FORWARD = 1;
int high_BACKWARD = 2;
int high_STOP = 3;

void setup(){
  radio.begin();
  radio.openWritingPipe(pipe);
  pinMode (BUTTON_FORWARD_pin, INPUT);
  pinMode (BUTTON_BACKWARD_pin, INPUT);

}

void loop()
{

if ((digitalRead(BUTTON_FORWARD) == HIGH) && (digitalRead(BUTTON_BACKWARD) == LOW)){
  buttons = FORWARD
}
if ((digitalRead(BUTTON_FORWARD) == LOW) && (digitalRead(BUTTON_BACKWARD) == HIGH)){
  buttons = BACKWARD
}
if ((digitalRead(BUTTON_FORWARD) == LOW) && (digitalRead(BUTTON_BACKWARD) == LOW)){
  buttons = NONE
}

  switch(state)
  {
    case STOP:
      if(buttons == NONE){
        radio.write(high_STOP, sizeof(high_STOP));
        state = STOP; 
      }
       else if(buttons == FORWARD){
        radio.write(high_FORWARD, sizeof(high_FORWARD));
        state = FORWARD; 
      }
       else{
        radio.write(high_BACKWARD, sizeof(high_BACKWARD));
        state = BACKWARD; 
      }
      break;

    case FORWARD:
      if(buttons == NONE){
        radio.write(high_FORWARD, sizeof(high_FORWARD));
        state = FORWARD; 
      }
       else if(buttons == FORWARD){
        radio.write(high_FORWARD, sizeof(high_FORWARD));
        state = FORWARD; 
      }
       else{
        radio.write(high_STOP, sizeof(high_STOP));
        state = STOP; 
      }
      break;

    case BACKWARD:
   //Here comes the error message   if(buttons == NONE){
        radio.write(high_BACKWARD, sizeof(high_BACKWARD));
        state = BACKWARD; 
      }
       else if(buttons == FORWARD){
        radio.write(high_STOP, sizeof(high_STOP));
        state = STOP; 
      }
       else{
        radio.write(high_BACKWARD, sizeof(high_BACKWARD));
        state = BACKWARD; 
      }      
      break;
  }
}

Thanks! :slight_smile:

You're missing semi-colons. You have 'else without if'. You have an additional }.

I do not see a variable named buttons, of any type, declared anywhere.
I do not see a variable named state, of any type, declared anywhere.

Buttons and State variables are declared as enumerations at the top. And it works for the two first cases...

sterretje:
You're missing semi-colons. You have 'else without if'. You have an additional }.

Where would we be missing semi-colons? The cases before the line are almost identical and they work, they even contain the exact same line that has problems and still works...

It's not in the cases; e.g.

  if ((digitalRead(BUTTON_FORWARD) == HIGH) && (digitalRead(BUTTON_BACKWARD) == LOW)) {
    buttons = FORWARD
  }

And in the same part, to my knowledge enumerations start at 0 unless otherwise instructed. So BUTTON_FORWARD equals 1 and BUTTON_BACKWARD equals 2; those are NOT the pins.

Raxu:
Buttons and State variables are declared as enumerations at the top. And it works for the two first cases...

buttons is not the same as Buttons_enum. You will need to create a variable of type Buttons_enum called buttons.

Buttons and State variables are declared as enumerations at the top. And it works for the two first cases...

Where? Copy and paste the exact lines in your reply.

sterretje:
It's not in the cases; e.g.

  if ((digitalRead(BUTTON_FORWARD) == HIGH) && (digitalRead(BUTTON_BACKWARD) == LOW)) {

buttons = FORWARD
  }




And in the same part, to my knowledge enumerations start at 0 unless otherwise instructed. So *BUTTON_FORWARD* equals 1 and *BUTTON_BACKWARD* equals 2; those are NOT the pins.

*buttons* is not the same as *Buttons_enum*. You will need to create a variable of type *Buttons_enum* called *buttons*.

Ok thank you! This was helpful, we'll try and create a variable of type Buttons_enum.

Our button pin variables are named "BUTTON_FORWARD_pin" and "BUTTON_BACKWARD_pin".

Raxu:
Where would we be missing semi-colons? The cases before the line are almost identical and they work, they even contain the exact same line that has problems and still works...

you should put //comments at the end of a line of code, so someone might not be mistakingly think that you tried to compile it like that.

We have now created variables of types Buttons_enum and State_enum but we’re still getting errors… We are very noobs on Arduinos so it’s difficult to get forward. We are still getting error messages that “‘buttons’ was not declared in this scope” as well as new error messages like "cannot convert ‘State_enum’ to ‘Buttons_enum’ in assignment.

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

#define CE_PIN   7 //byta?
#define CSN_PIN 8

const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

enum State_enum {STOP, FORWARD, BACKWARD}; //Create enum for wheel states
enum Buttons_enum {NONE, BUTTON_FORWARD, BUTTON_BACKWARD}; //Create enum for button configs

Buttons_enum buttons = NONE; //assign variable to enum
State_enum state = STOP; //assign variable to enum

int BUTTON_FORWARD_pin = 4;
int BUTTON_BACKWARD_pin = 2;

int high_FORWARD = 1;
int high_BACKWARD = 2;
int high_STOP = 3;

void setup(){
  
  radio.begin();
  radio.openWritingPipe(pipe);
  pinMode (BUTTON_FORWARD_pin, INPUT);
  pinMode (BUTTON_BACKWARD_pin, INPUT);

}

void loop()
{
  
//check which buttons are pressed and assign correct state

  if ((digitalRead(BUTTON_FORWARD_pin) == HIGH) && (digitalRead(BUTTON_BACKWARD_pin) == LOW)){
  buttons = FORWARD;
  }
  if ((digitalRead(BUTTON_FORWARD_pin) == LOW) && (digitalRead(BUTTON_BACKWARD_pin) == HIGH)){
  buttons = BACKWARD;
  }
  if ((digitalRead(BUTTON_FORWARD_pin) == LOW) && (digitalRead(BUTTON_BACKWARD_pin) == LOW)){
  buttons = NONE;
  }

  switch(state) //switch between states and send correct radio message
  {
    case STOP:
      if(buttons == NONE){
        radio.write(high_STOP, sizeof(high_STOP));
        state = STOP;
      }
       else if(buttons == FORWARD){
        radio.write(high_FORWARD, sizeof(high_FORWARD));
        state = FORWARD; 
      }
       else{
        radio.write(high_BACKWARD, sizeof(high_BACKWARD));
        state = BACKWARD; 
      }
      break;

    case FORWARD:
      if(buttons == NONE){
        radio.write(high_FORWARD, sizeof(high_FORWARD));
        state = FORWARD; 
      }
       else if(buttons == FORWARD){
        radio.write(high_FORWARD, sizeof(high_FORWARD));
        state = FORWARD; 
      }
       else{
        radio.write(high_STOP, sizeof(high_STOP));
        state = STOP; 
      }
      break;

    case BACKWARD:
      if(buttons == NONE){
        radio.write(high_BACKWARD, sizeof(high_BACKWARD));
        state = BACKWARD; 
      }
       else if(buttons == FORWARD){
        radio.write(high_STOP, sizeof(high_STOP));
        state = STOP; 
      }
       else{
        radio.write(high_BACKWARD, sizeof(high_BACKWARD));
        state = BACKWARD; 
      }      
      break;
  }
}

We just can’t seem to find what the problem is and would appreciate greatly if anyone can help us! :frowning:

"'buttons' was not declared in this post"

post the actual messages, not what you think you remember.

AWOL:
post the actual messages, not what you think you remember.

Oh sorry! A typo... The message we are getting is the one in this thread title: "'buttons was not declared in this scope"

And where do you think "buttons" is declared?

AWOL:
And where do you think "buttons" is declared?

Here maybe?

enum State_enum {STOP, FORWARD, BACKWARD}; //Create enum for wheel states
enum Buttons_enum {NONE, BUTTON_FORWARD, BUTTON_BACKWARD}; //Create enum for button configs

Buttons_enum buttons = NONE; //assign variable to enum
State_enum state = STOP; //assign variable to enum

But that might be wrong?

Post the actual error messages.
All of them.

We finally got it working, the mistake was that we mixed up the names within the two enums... So the code is compiling now!

AWOL:
Post the actual error messages.
All of them.

Is there a way to access message history to find all messages? :frowning:

No, recreate the faulty code, compile and post it here 8)

You might not have fixed another issue. You have a number of lines like

radio.write(high_FORWARD, sizeof(high_FORWARD));

high_FORWARD is an int, radio.write() takes a pointer to a byte. You need to use the address of high_FORWARD.