Getting two lights to come on in order

Hi folks! I'm new to Arduino, and my first project is pretty simple. I want to build a box with two lights. When the box is closed once, the first light turns on. When it's closed the second time, the first light stays on and the second light comes on. When it's closed a third time, both lights go out. And I want the process to loop--fourth time, the first light comes on again, fifth time=second light, and so on.

I've figured out how to get the first light to turn on with ButtonStateChange, but I'm not sure how to get the second one to turn on, and I have no idea how to turn them both off. Any ideas?

Show us what you've tried. Post your code between code tags (click the # button to insert tags while composing a post).

At each button state change you increment a variable. Then you use if statements to turn the LEDs on in the order you want them. Finally when your variable reaches the limit, which in your case is three, you set it back to zero. Again using an if statement.

This is in some way a special case of my "multiple modes" experiment http://blog.blinkenlight.net/experiments/counting-resets/multiple-modes/. You might want to read the introduction "counting resets" to understand what is going on.

Jack, I haven't tried anything yet--heck, I don't even have the kit yet--but here's what I'm looking at. Grumpy_Mike, that makes sense. In fact, I was thinking about changing it from two lights down to one, and having the light blink the second time the box was open and closed. I'm not sure where the if-then statement for, say, "if the button is pressed a second time, then make the light blink" would go in this code. Maybe I'm overthinking this, but can you put an if-then statement in an if-then statement? And where would the variable go? Sorry, but I'm really a noob at this...

/* State change detection (edge detection)

Often, you don't need to know the state of a digital input all the time, but you just need to know when the input changes from one state to another. For example, you want to know when a button goes from OFF to ON. This is called state change detection, or edge detection.

This example shows how to detect when a button or button changes from off to on and on to off.

The circuit: * pushbutton attached to pin 2 from +5V * 10K resistor attached to pin 2 from ground * LED attached from pin 13 to ground (or use the built-in LED on most Arduino boards)

created 27 Sep 2005 modified 30 Aug 2011 by Tom Igoe

This example code is in the public domain.

http://arduino.cc/en/Tutorial/ButtonStateChange

*/

// this constant won't change: const int buttonPin = 2; // the pin that the pushbutton is attached to const int ledPin = 13; // the pin that the LED is attached to

// Variables will change: int buttonPushCounter = 0; // counter for the number of button presses int buttonState = 0; // current state of the button int lastButtonState = 0; // previous state of the button

void setup() { // initialize the button pin as a input: pinMode(buttonPin, INPUT); // initialize the LED as an output: pinMode(ledPin, OUTPUT); // initialize serial communication: Serial.begin(9600); }

void loop() { // read the pushbutton input pin: buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state if (buttonState != lastButtonState) { // if the state has changed, increment the counter if (buttonState == HIGH) { // if the current state is HIGH then the button // wend from off to on: buttonPushCounter++; Serial.println("on"); Serial.print("number of button pushes: "); Serial.println(buttonPushCounter); } else { // if the current state is LOW then the button // wend from on to off: Serial.println("off"); } } // save the current state as the last state, //for next time through the loop lastButtonState = buttonState;

// turns on the LED every four button pushes by // checking the modulo of the button push counter. // the modulo function gives you the remainder of // the division of two numbers: if (buttonPushCounter % 4 == 0) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); }

}

And here's another question: why is it that in the ButtonStateChange coding, it turns the LED on every four pushes? Why not every other push?

And here's another question: why is it that in the ButtonStateChange coding, it turns the LED on every four pushes? Why not every other push?

if (buttonPushCounter % 4 == 0)

The [u]modulo operator[/u]

BTW - Your code will look better if you use code tags [ /code] (without the space before the '/'), or click the # button in the editor.

OK, here's what I've come up with so far. There was a "void loop" command right after (buttonPushCounter % 8 == 0), which I took out; my logic was that I didn't want the loop to void so that the counter could continue to track the number of pushes. I was going to make the last command a "else-if" one, but since it was the last command and the only time the light would go off, I left it as just "else". What do you guys think?

thanks for the / code help, too.

*/

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
 // initialize the button pin as a input:
 pinMode(buttonPin, INPUT);
 // initialize the LED as an output:
 pinMode(ledPin, OUTPUT);
 // initialize serial communication:
 Serial.begin(9600);
}


void loop() {
 // read the pushbutton input pin:
 buttonState = digitalRead(buttonPin);

 // compare the buttonState to its previous state
 if (buttonState != lastButtonState) {
   // if the state has changed, increment the counter
   if (buttonState == HIGH) {
     // if the current state is HIGH then the button
     // wend from off to on:
     buttonPushCounter++;
     Serial.println("on");
     Serial.print("number of button pushes:  ");
     Serial.println(buttonPushCounter);
   }
   else {
     // if the current state is LOW then the button
     // went from on to off:
     Serial.println("off");
   }
 }
 // save the current state as the last state,
 //for next time through the loop
 lastButtonState = buttonState;


 // turns on the LED every four button pushes by
 // checking the modulo of the button push counter.
 // the modulo function gives you the remainder of
 // the division of two numbers:
 if (buttonPushCounter % 4 == 0) {
   digitalWrite(ledPin, HIGH);
 }
else if (buttonPushCounter % 8 == 0) {
 digitalWrite(ledPin, HIGH);   // sets the LED on
 delay(1000);                  // waits for a second
 digitalWrite(ledPin, LOW);    // sets the LED off
 delay(1000);                  // waits for a second
}
else (buttonPushCounter % 12 == 0) {
  digitalWrite(ledPin, LOW);
 }

}

walkie74: Jack, I haven't tried anything yet--heck, I don't even have the kit yet--

@walkie74, what kit do you not have. I've been assuming you must have an Arduino and the LEDs, button, etc.?

Jack, I'm purely in the research stage. I have no equipment yet--I wanted to make sure I could pull this off before I bought the parts.

walkie74: Jack, I'm purely in the research stage. I have no equipment yet--I wanted to make sure I could pull this off before I bought the parts.

This should be very straightforward. Jump in with both feet! I'd be pretty frustrated writing code without a way to test it!

Do you have a particular kit in mind?

A suggestion, and a question.

For dealing with the button, use a library. There are two parts to this project, one is the logic to run the lights, and the other is handling the button, debouncing it, etc. You'll no doubt learn more about libraries once you get your stuff, but basically they are just pre-packaged code (classes, actually) that make it easier to interface with some other hardware (could be a sensor, or buttons, various integrated circuits, etc., OTOH libraries don't have to be hardware-oriented, they could for example provide mathematical functions of some sort.)

Buttons seem simple, but can be deceiving; properly handling a button can be a little tricky. So best not to mix that logic in with the other. Keep it compartmentalized and out of the way. That way you can deal with the button at a more abstract level, i.e. just as a button without having to worry about debouncing, etc., and concentrate on what is really the main logic of the project. Of course, once the code is working with a button library, it'd be an educational exercise to then try handling the button on your own.

There are lots of button libraries around, in the libraries section, in the playground and probably a lot of other places. As ever, GIYF. I wrote my own button library, you're welcome to it if you're interested.

Question on the lights, what kind did you have in mind? We might have just assumed simple LEDs on you there.

Haha it's funny you mention that--for me, the frustration would be buying the parts and then realizing there was no way to write the code :)

I'm looking at the Kit Workshop Base from this site. It's an awful lot of parts, but I know I'd get everything I needed. I might build one myself, once I figure out how much everything might be--and I'm sure I didn't miss anything.

That looks like a pretty nice kit. I know some of the kits out there also come with manuals of experiments (many are available online), if that's something that might appeal to you. Can be a good intro to the various capabilities of the Arduino. I hope you have a great time!

There was a "void loop" command right after (buttonPushCounter % 8 == 0), which I took out; my logic was that I didn't want the loop to void so that the counter could continue to track the number of pushes.

No no no.

Loop is the name of the function, you must have a loop function. void is telling the compiler that this function returns no variable

I think you need to start trying to compile what you write.

Grumpy_Mike, you're right--I think I read that earlier too. This was my first try at modifying code in over 30 years (the last time was with my VIC20, so there you are). OK, so "void loop" goes back in.

Jack, I found the Toggle button library. I think that might be what I'm looking for. I'm guessing I'd just paste it into my code? I'd love to see your library as well--it'd be great to compare.

My button library is here: https://github.com/JChristensen/Button

Arduino libraries are installed in the sketchbook\libraries directory. Specific instructions for installation and use are at the link above. The library also contains several example sketches. A #include statement is used to reference a library in a sketch.

Let me know how it works for you!