K, so as a beginning programmer i was trying to create a code for making a simple arduino reaction game and i've been going nuts trying to figure out where my problem is in the code. Every time i think i found the issue , there's only more errors showing up. Can someone tell me how to fix it please ?
// 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 ) ;
}
}
Ok now for your problems... The first obvious problem is your "for" loops, that's not how you write a for loop.
//instead of this:
for (int i=0 ; i pinMode( led_pins[i], OUTPUT ) ;
}
// do this:
for (int i=0 ; i < 3; i++) //i from 0 to 2 (3 being the size of led_pins array)
{
pinMode( led_pins[i], OUTPUT ) ;
}
Once you fix your for loops, you may have a problem of braces. Every opening brace "{" must be closed by a "}" (actually it's the opposite, you have too much "}" and need to add "{", and I recommend using the Allman style indentation).
Thanks,it helped solve some of the errors but im still getting tons of errors. I'm really new to this so I'm really stuck.
sketch_apr28h:30: error: expected constructor, destructor, or type conversion before '(' token
sketch_apr28h.ino: In function 'void loop()':
sketch_apr28h:40: error: expected ;' before 'digitalWrite' sketch_apr28h:40: error: expected )' before ';' token
sketch_apr28h:43: error: expected ;' before 'button_state' sketch_apr28h:43: error: expected )' before ';' token
sketch_apr28h:53: error: name lookup of 'i' changed for new ISO 'for' scoping
sketch_apr28h:40: error: using obsolete binding at 'i'
sketch_apr28h:63: error: break statement not within loop or switch
sketch_apr28h.ino: At global scope:
sketch_apr28h:69: error: expected constructor, destructor, or type conversion before '(' token
sketch_apr28h:70: error: expected declaration before '}' token
// 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 < 3; i++) //i from 0 to 2 (3 being the size of led_pins array)
{
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 ) ;
}
}
You fixed only one for loop, I see more loops in there... And you fixed it badly, remember the number of "{" must match the number of "}".
Here for exemple:
void setup(){
for (int i=0 ; i < 3; i++) //i from 0 to 2 (3 being the size of led_pins array)
{
pinMode( led_pins[i], OUTPUT ) ;
pinMode( button_pin, INPUT ) ;
}
you have 2 "{" but only one "}". That's why indentation is important. Also I'm not sure you want to set pinMode of the button_pin 3 times (since it looks like in the for loop). Fixed:
void setup()
{
for (int i=0 ; i < 3; i++) //i from 0 to 2 (3 being the size of led_pins array)
{
pinMode( led_pins[i], OUTPUT ) ;
}
pinMode( button_pin, INPUT ) ;
}
But since you have a single line under the loop, you aren't forced to put it inside { }, so you could do:
void setup()
{
for (int i=0 ; i < 3; i++) //i from 0 to 2 (3 being the size of led_pins array)
pinMode( led_pins[i], OUTPUT ) ;
pinMode( button_pin, INPUT ) ;
}
Understand?
Now fix your other for loops, carefully check for "{" and "}" and use the Auto Format in the Tools menu of the Arduino IDE. It should fix most errors
for ( int t=0 ; t button_state = digitalRead( button_pin ) ;
What about this part, im getting an error?
boolean ON = 1 ;
boolean OFF = 0 ;
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 < 3; i++) //i from 0 to 2 (3 being the size of led_pins array)
pinMode( led_pins[i], OUTPUT ) ;
pinMode( button_pin, INPUT ) ;
}
void loop()
{
boolean button_state = OFF ;
for (int i=0 ; i < 3; i++) //i from 0 to 2 (3 being the size of led_pins array)
pinMode( led_pins[i], OUTPUT ) ;
for ( int t=0 ; t button_state = digitalRead( button_pin ) ;
if ( button_state == ON ){
delay( push_pause ) ;
button_state = OFF ;
if ( i == success_led )
colorswitch_delay -= time_change ;
else
colorswitch_delay += time_change ;
break ;
}
delay( 5 ) ;
digitalWrite( led_pins[i], LOW ) ;
}
the errors:
program_part_1_game.ino: In function 'void loop()':
program_part_1_game:40: error: expected ;' before 'button_state' program_part_1_game:40: error: expected )' before ';' token
program_part_1_game:48: error: name lookup of 'i' changed for new ISO 'for' scoping
program_part_1_game:36: error: using obsolete binding at 'i'
program_part_1_game:54: error: break statement not within loop or switch
You MUST put braces around parts of code where they are needed. As you are a beginner, I suggest you forget about what I said earlier about the optional braces, and just use them everywhere...
I have no idea what is that "t" value, only you know! Suppose you want to loop 10 times...
void loop()
{
boolean button_state = OFF ;
for (int i=0 ; i < 3; i++) //i from 0 to 2 (3 being the size of led_pins array)
{
for ( int t=0 ; t < 10; t++ )
{
button_state = digitalRead( button_pin ) ;
if ( button_state == ON )
{
delay( push_pause ) ;
button_state = OFF ;
if ( i == success_led )
{
colorswitch_delay -= time_change ;
}
else
{
colorswitch_delay += time_change ;
}
break ;
}
}
delay( 5 ) ;
digitalWrite( led_pins[i], LOW ) ;
}
}
Please study the changes carefully, it's the only way for you to learn
That will maybe compile without errors, but I have no idea if it will work at all, or will work the way YOU want it.
And to be honest, you better start a new project by yourself, start small by looking at the examples that comes with the Arduino IDE (in menu File -> Examples -> Basics), instead of copy/pasting code that you have no clue about