Show Posts
Pages: [1]
1  Using Arduino / LEDs and Multiplexing / Re: Getting two lights to come on in order on: May 23, 2012, 11:17:12 am
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.
2  Using Arduino / LEDs and Multiplexing / Re: Getting two lights to come on in order on: May 22, 2012, 08:17:14 pm
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 smiley

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.
3  Using Arduino / LEDs and Multiplexing / Re: Getting two lights to come on in order on: May 22, 2012, 06:39:30 pm
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.
4  Using Arduino / LEDs and Multiplexing / Re: Getting two lights to come on in order on: May 22, 2012, 05:51:21 pm
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.

Code:
*/

// 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);
 }

}
5  Using Arduino / LEDs and Multiplexing / Re: Getting two lights to come on in order on: May 22, 2012, 05:02:29 pm
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?
6  Using Arduino / LEDs and Multiplexing / Re: Getting two lights to come on in order on: May 22, 2012, 04:39:17 pm
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);
  }
 
}
7  Using Arduino / LEDs and Multiplexing / Getting two lights to come on in order on: May 21, 2012, 05:07:08 pm
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?
Pages: [1]