1

I have several integers, all of which are declared and set to zero in setup. However, in a method, I can only modify the value of half of those integers. The method is to read several buttons and return which of them have been pressed (precondition is that no more than one buttons are pressed):

 int readButtons(){
 rbuttonState = digitalRead(right);
 lbuttonState = digitalRead(left);
 sbuttonState = digitalRead(sel);
 obuttonState = digitalRead(off);
 nbuttonState = digitalRead(snooze);
 int ret = -1;
 if (rbuttonState != lastrButtonState){
 if (rbuttonState == HIGH) {
 ret = right;
 }
 }
 if (lbuttonState != lastlButtonState){
 if (lbuttonState == HIGH) {
 ret = left;
 }
 }
 if (sbuttonState != lastsButtonState){
 if (sbuttonState == HIGH) {
 ret = sel;
 }
 }
 if (obuttonState != lastoButtonState){
 if (obuttonState == HIGH) {
 ret = off;
 }
 }
 if (nbuttonState != lastnButtonState){
 if (nbuttonState == HIGH) {
 ret = snooze;
 }
 }
 }
 lastrButtonState = rbuttonState;
 lastlButtonState = lbuttonState;
 lastsButtonState = sbuttonState;
 lastoButtonState = obuttonState;
 lastnButtonState = nbuttonState;
 return ret;
}

Yet I have the errors that

Arduino: 1.6.12 (Mac OS X), Board: "Arduino Nano, ATmega328"
Nixie_Clock_0:65: error: 'lastrButtonState' does not name a type
 lastrButtonState = rbuttonState;
 ^
Nixie_Clock_0:66: error: 'lastlButtonState' does not name a type
 lastlButtonState = lbuttonState;
 ^
Nixie_Clock_0:67: error: 'lastsButtonState' does not name a type
 lastsButtonState = sbuttonState;
 ^
Nixie_Clock_0:68: error: 'lastoButtonState' does not name a type
 lastoButtonState = obuttonState;
 ^
Nixie_Clock_0:69: error: 'lastnButtonState' does not name a type
 lastnButtonState = nbuttonState;
 ^
Nixie_Clock_0:70: error: expected unqualified-id before 'return'
 return ret;
 ^
Nixie_Clock_0:71: error: expected declaration before '}' token
 }
 ^
exit status 1
'lastrButtonState' does not name a type
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

(The last line of code is line 72). I am at a complete loss. Any help?

asked Apr 28, 2017 at 17:18
4
  • 3
    Where is the rest of your code? Commented Apr 28, 2017 at 17:26
  • 1
    There is an end bracket too many. Indent the code correctly and you will find it. Commented Apr 28, 2017 at 21:20
  • the posted code does not contain 70 some lines of code. It only contains 40 lines of code. Post the actual code. Commented Apr 29, 2017 at 1:45
  • @Mikael Patel thanks! I can't believe I missed that! Commented Apr 30, 2017 at 0:29

1 Answer 1

0

the following code

  1. is properly indented
  2. is missing the declarations of the variables for the 'button states' and the 'prior button states'
  3. has additional text about a format error
  4. is properly indented
  5. has the code blocks properly separated by a single blank line

and now the code

int readButtons()
{
 rbuttonState = digitalRead(right);
 lbuttonState = digitalRead(left);
 sbuttonState = digitalRead(sel);
 obuttonState = digitalRead(off);
 nbuttonState = digitalRead(snooze);
 int ret = -1;
 if (rbuttonState != lastrButtonState)
 {
 if (rbuttonState == HIGH)
 {
 ret = right;
 }
 }
 if (lbuttonState != lastlButtonState)
 {
 if (lbuttonState == HIGH)
 {
 ret = left;
 }
 }
 if (sbuttonState != lastsButtonState)
 {
 if (sbuttonState == HIGH)
 {
 ret = sel;
 }
 }
 if (obuttonState != lastoButtonState)
 {
 if (obuttonState == HIGH)
 {
 ret = off;
 }
 }
 if (nbuttonState != lastnButtonState)
 {
 if (nbuttonState == HIGH)
 {
 ret = snooze;
 }
 }
} << extranious closing brace ends the actual function
 lastrButtonState = rbuttonState;
 lastlButtonState = lbuttonState;
 lastsButtonState = sbuttonState;
 lastoButtonState = obuttonState;
 lastnButtonState = nbuttonState;
 return ret;
}
answered Apr 29, 2017 at 1:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.