First sketch by total n00b

Hi. I'm trying to set up my Arduino to cycle a red LED and blue LED through three modes: blue on/red off; red on/blue off; and flashing red then blue. I thought I was getting it, but there is something wrong with my "if" syntax. (There's probably a lot of other things that are wrong as well.) Here's the sketch:

// 3-mode blue and red int ledRed = 9; // assign red LED int ledBlue = 10; int button = 2; // button pin void setup() { pinMode(ledBlue, OUTPUT); pinMode(ledRed, OUTPUT); pinMode(button, INPUT); // button on pin 2 // switch mode digitalWrite(ledBlue, HIGH); } void loop() { // 3 dits for (int x=0; x>3; x++) { int state = digitalRead(button); /* checks for first button press / if (state == HIGH && x=1) { digitalWrite(ledBlue, LOW); // turns the blue LED off digitalWrite(ledRed, HIGH); // turns the red LED on} { int state = digitalRead(button); / checks for second button press / if (state == HIGH && x=2) { // flashes red and blue digitalWrite(ledRed, LOW); digitalWrite(ledBlue, HIGH); delay(150); digitalWrite(ledBlue, LOW); digitalWrite(ledRed, HIGH); delay(150);} { int state = digitalRead(button); / checks for third button press */ if (state == HIGH && x=3) { digitalWrite(ledRed, LOW); digitalWrite(ledBlue, HIGH);// returns to blue on mode, loop repeats }

Any help will be greatly appreciated.

As far as obvious syntax, the ‘for’ loop should be x<2 (for 3 counts 0,1,2) because it loops while that is the condition. The x=1 stuff inside the if() should be x==1 like you have the button state. There might be some other technical problems but you are on the right thought path.

Thank you, savitch! I fixed the "x == 1" lines, but now I get a new error. :'(

// 3-mode blue and red int ledRed = 9; // assign red LED int ledBlue = 10; int button = 2; // button pin void setup() { pinMode(ledBlue, OUTPUT); pinMode(ledRed, OUTPUT); pinMode(button, INPUT); // button on pin 2 // switch mode digitalWrite(ledBlue, HIGH); } void loop() { // 3 dits for (int x=0; x>3; x++) { int state = digitalRead(button); /* checks for first button press / if (state == HIGH && x==1) { digitalWrite(ledBlue, LOW); // turns the blue LED off digitalWrite(ledRed, HIGH); // turns the red LED on} { int state = digitalRead(button); / checks for second button press / if (state == HIGH && x==2) { // flashes red and blue digitalWrite(ledRed, LOW); digitalWrite(ledBlue, HIGH); delay(150); digitalWrite(ledBlue, LOW); digitalWrite(ledRed, HIGH); delay(150);} { int state = digitalRead(button); / checks for third button press */ if (state == HIGH && x==3) { digitalWrite(ledRed, LOW); digitalWrite(ledBlue, HIGH);// returns to blue on mode, loop repeats }

The new error reads:

In function 'void loop()': error: a function-definition is not allowed here before '{' tokenCouldn't determine program size: hardware/tools/avr/bin/avr-size: '/var/folders/wJ/wJM9mjhOHm8PBweLS4eHwE+++TI/-Tmp-/build1133375779792384776.tmp/flash_blue_red.hex': No such file

You are just misplacing some brackets so it thinks you are trying to define something outside of the loop(). It can also be cleaned up and simplified to about half that size.

Hmm. After reading your second response, I realized that I hadn't closed several of the brackets, but when I add the closing brackets, I get the same error message. I also tried moving the first "digitalWrite(ledBlue, HIGH);", but it made no difference. Where am I going wrong? I'm not surprised to hear that the sketch can be cleaned up and made more efficient, but for my first sketch, I'll settle for functional. :P

// 3-mode blue and red
const int ledRed = 9; // assign red LED
const int ledBlue = 10;
const int button = 2; // button pin

void setup() 
{
    pinMode(ledBlue, OUTPUT);
    pinMode(ledRed, OUTPUT);
    pinMode(button, INPUT); // button on pin 2
    digitalWrite(ledBlue, HIGH);
    // what about red?
}
void loop() 
{
    for (int x=0; x<3; x++) {
        int state = digitalRead(button);
        /* checks for first button press */
        if (state == HIGH && x==0) {
           digitalWrite(ledBlue, LOW); // turns the blue LED off
           digitalWrite(ledRed, HIGH); // turns the red LED on}
        }

        state = digitalRead(button);
        /* checks for second button press */
        if (state == HIGH && x==1) {
            // flashes red and blue
            digitalWrite(ledRed, LOW);
            digitalWrite(ledBlue, HIGH);
            delay(150);
            digitalWrite(ledBlue, LOW);
            digitalWrite(ledRed, HIGH);
            delay(150);
        }
        state = digitalRead(button);
        /* checks for third button press */
        if (state == HIGH && x==2) {
            digitalWrite(ledRed, LOW);
            digitalWrite(ledBlue, HIGH);// returns to blue on mode, loop repeats
        }
    }
}

looks about right.
Consistent indentation helps.

It's not going to do what you want in its current state. Get rid of the 'for' loop completely.

Define the 'int state;' for the button at the top with the rest then just do a state=digitalRead(button); in the loop.

But the led writes inside an if() testing for x==1 and the other states.

Then have an

if(state==1){delay(300);x++;}//delay for button debounce to advance at every button press. X will advance then the leds will do what is inside their if(x==1) statements.

At the end add if(x>2)x=0; so it resets and starts over. x starts at 0 so 3 states would start at if(x==0)

With this flow it will loop forever and switch mode every button press.

Thanks, AWOL, and thanks again, savitch. It's about 2:30 a.m. here in Japan, so I'll sleep on it and try again tomorrow.

In the light of morning, I'm still in the dark. I tried writing it this way:

// 3-mode blue and red const int ledRed = 9; // assign red LED const int ledBlue = 10; const int button = 2; // button pin int state = digitalRead(button);

void setup() { pinMode(ledBlue, OUTPUT); pinMode(ledRed, OUTPUT); pinMode(button, INPUT); // button on pin 2 } void loop() {state=digitalRead(button); if (state == 1) {delay(300);x++}//delay for button debounce { if(x==0) { digitalWrite(ledRed, LOW); digitalWrite(ledBlue, HIGH);} if(x==1) { digitalWrite(ledBlue, LOW); digitalWrite(ledRed, HIGH);} if(x==2) { digitalWrite(ledRed, LOW); digitalWrite(ledBlue, HIGH); delay(150); digitalWrite(ledBlue, LOW); digitalWrite(ledRed, HIGH); delay(150);} if (x>2)x=0;} }

And I got

In function 'void loop()': error: 'x' was not declared in this scope

:-/

I did it! Thanks for the tips, savitch! You have a great sense for pointing a n00b in the right direction without spoon-feeding him/her. :) Thanks to your advice I was able to actually understand what the problems were and how to resolve them.

// 3-mode blue and red const int ledRed = 9; // asign red LED to pin 9 const int ledBlue = 10; // asign blue LED to pin 10 const int button = 2; // asign tactile switch to pin 2 int state = digitalRead(button); // define the state of the switch int x = 0; // a number that tells us where we are in the cycle

void setup() // setting things up { pinMode(ledBlue, OUTPUT); // this means pin 10 will be outputting pinMode(ledRed, OUTPUT); // this means pin 9 will be outputting pinMode(button, INPUT); // this means pin 2 will be taking input from the switch } void loop() // starting loop {state=digitalRead(button); if (state == 1) {delay(300);x++;}// checks to see if switch is pressed, delay for button debounce { if(x==0) { // first mode digitalWrite(ledRed, LOW); // makes sure red is turned off digitalWrite(ledBlue, HIGH);} // turns on blue if(x==1) { // second mode digitalWrite(ledBlue, LOW); // turns off blue digitalWrite(ledRed, HIGH);} // turns on red if(x==2) { // third mode, flashing digitalWrite(ledRed, LOW); // turns off red digitalWrite(ledBlue, HIGH); // turns on blue delay(150); // waits 150 milliseconds digitalWrite(ledBlue, LOW); // turns off blue digitalWrite(ledRed, HIGH); // turns on red delay(150);} // waits 150 milliseconds if (x>2)x=0;} // if switch is pressed again, variable x is reset to 0 and the cycle begins again }