Arduino 问题:“'i' 未在此范围内声明”
Arduino problems:" 'i' was not declared in this scope"
这个为 arduino 反应游戏制作的代码有很多问题。我不确定我是否已经声明过 i 两次或其他什么。 Arduino 告诉我问题出在这里:"if ( i == success_led )"。
// Public Domain 2012 by A.Coster
// some constants for readability, using booleans to save memory
boolean ON = 1 ;
boolean OFF = 0 ;
// Arduino pin setup - there are 3 pins used for LEDs,
// 1 for push-button. The pin at index success_led is
// the one the user should try to hit.
byte led_pins[3] = {8,9,10} ;
byte button_pin = 2 ;
byte success_led = 1 ; // refers to pin 9
// time_change is the number of milliseconds to add upon
// failure or subtract upon success.
// colorswitch_delay is the amount of time that the LED stays
// on. This value has time_change added to/subtracted from it.
// So that the user knows which LED was lit up when they hit
// the button, push_pause defines the number of milliseconds
// that the LED will stay on after the button is pressed.
unsigned int time_change = 50 ;
unsigned int colorswitch_delay = 500 ;
unsigned int push_pause = 2000 ;
void setup(){
for (int i=0 ; i ;) ;
pinMode( led_pins[i], OUTPUT ) ;
pinMode( button_pin, INPUT ) ;
}
void loop(){
// need to keep track of button presses
// so set the button state to 0 and overwrite
// later upon button press
boolean button_state = OFF ;
// sequentially turn on each LED
for ( int i=0 ; i digitalWrite( led_pins[i], HIGH ) ;
// Check for a button press every 5 milliseconds
for ( int t=0 ; t button_state = digitalRead( button_pin ) ;
// if the button has been pressed, stop
// switching LEDs so the user knows, and
// then check if it was the success_led
if ( button_state == ON ){
delay( push_pause ) ;
// set the button state back to off
button_state = OFF ;
if ( i == success_led )
// if the user hit the right LED,
// make it more challenging by
// reducing the delay between LED
// switches.
colorswitch_delay -= time_change ;
else
// if the user was wrong, make it
// easier by increasing the delay.
colorswitch_delay += time_change ;
break ;
}
delay( 5 ) ;
}
// and turn it off before looping through
// to turn the next LED on.
digitalWrite( led_pins[i], LOW ) ;
}
}
您在每个 for
循环后放置了分号。由于在循环的控制语句中声明的结果变量 i
(和其他一些变量)在这些语句之外不存在。例如
void setup(){
for (int i=0 ; i ;) ;
^^^
pinMode( led_pins[i], OUTPUT ) ;
^^^???
//...
或
void loop(){
// need to keep track of button presses
// so set the button state to 0 and overwrite
// later upon button press
boolean button_state = OFF ;
// sequentially turn on each LED
for ( int i=0 ; i digitalWrite( led_pins[i], HIGH ) ;
^^^
// Check for a button press every 5 milliseconds
for ( int t=0 ; t button_state = digitalRead( button_pin ) ;
^^^
//...
这个为 arduino 反应游戏制作的代码有很多问题。我不确定我是否已经声明过 i 两次或其他什么。 Arduino 告诉我问题出在这里:"if ( i == success_led )"。
// Public Domain 2012 by A.Coster
// some constants for readability, using booleans to save memory
boolean ON = 1 ;
boolean OFF = 0 ;
// Arduino pin setup - there are 3 pins used for LEDs,
// 1 for push-button. The pin at index success_led is
// the one the user should try to hit.
byte led_pins[3] = {8,9,10} ;
byte button_pin = 2 ;
byte success_led = 1 ; // refers to pin 9
// time_change is the number of milliseconds to add upon
// failure or subtract upon success.
// colorswitch_delay is the amount of time that the LED stays
// on. This value has time_change added to/subtracted from it.
// So that the user knows which LED was lit up when they hit
// the button, push_pause defines the number of milliseconds
// that the LED will stay on after the button is pressed.
unsigned int time_change = 50 ;
unsigned int colorswitch_delay = 500 ;
unsigned int push_pause = 2000 ;
void setup(){
for (int i=0 ; i ;) ;
pinMode( led_pins[i], OUTPUT ) ;
pinMode( button_pin, INPUT ) ;
}
void loop(){
// need to keep track of button presses
// so set the button state to 0 and overwrite
// later upon button press
boolean button_state = OFF ;
// sequentially turn on each LED
for ( int i=0 ; i digitalWrite( led_pins[i], HIGH ) ;
// Check for a button press every 5 milliseconds
for ( int t=0 ; t button_state = digitalRead( button_pin ) ;
// if the button has been pressed, stop
// switching LEDs so the user knows, and
// then check if it was the success_led
if ( button_state == ON ){
delay( push_pause ) ;
// set the button state back to off
button_state = OFF ;
if ( i == success_led )
// if the user hit the right LED,
// make it more challenging by
// reducing the delay between LED
// switches.
colorswitch_delay -= time_change ;
else
// if the user was wrong, make it
// easier by increasing the delay.
colorswitch_delay += time_change ;
break ;
}
delay( 5 ) ;
}
// and turn it off before looping through
// to turn the next LED on.
digitalWrite( led_pins[i], LOW ) ;
}
}
您在每个 for
循环后放置了分号。由于在循环的控制语句中声明的结果变量 i
(和其他一些变量)在这些语句之外不存在。例如
void setup(){
for (int i=0 ; i ;) ;
^^^
pinMode( led_pins[i], OUTPUT ) ;
^^^???
//...
或
void loop(){
// need to keep track of button presses
// so set the button state to 0 and overwrite
// later upon button press
boolean button_state = OFF ;
// sequentially turn on each LED
for ( int i=0 ; i digitalWrite( led_pins[i], HIGH ) ;
^^^
// Check for a button press every 5 milliseconds
for ( int t=0 ; t button_state = digitalRead( button_pin ) ;
^^^
//...