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?
-
3Where is the rest of your code?Majenko– Majenko2017年04月28日 17:26:35 +00:00Commented Apr 28, 2017 at 17:26
-
1There is an end bracket too many. Indent the code correctly and you will find it.Mikael Patel– Mikael Patel2017年04月28日 21:20:14 +00:00Commented 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.user3629249– user36292492017年04月29日 01:45:02 +00:00Commented Apr 29, 2017 at 1:45
-
@Mikael Patel thanks! I can't believe I missed that!Aditya J.– Aditya J.2017年04月30日 00:29:36 +00:00Commented Apr 30, 2017 at 0:29
1 Answer 1
the following code
- is properly indented
- is missing the declarations of the variables for the 'button states' and the 'prior button states'
- has additional text about a format error
- is properly indented
- 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;
}
lang-c