Hey, this is my first project post so don't expect to be blown away... :![]()
I built a little foot controlled LED switcher that uses three cheap flux leds, and two momentary switches.
Here's a video of it in action:
Here's the code:
buttonA counts to 5 and resets, buttonB resets to 0.
/*
#########################################
***BASED partly on this code
http://arduino.cc/en/Tutorial/ButtonStateChange
for counting presses with one button****
#########################################
*/
// Constants
const int buttonPinA = 2; // the pin that the pushbutton is attached to
const int buttonPinB = 3; // the pin that the pushbutton is attached to
const int ledPinR = 8; // the pin that the LED is attached to
const int ledPinB= 9; // the pin that the LED is attached to
const int ledPinG = 10; // the pin that the LED is attached to
// Variables
int buttonPushCounterA = 0; // counter for the number of button presses
int buttonStateA = 0; // current state of the button
int lastButtonStateA = 0; // previous state of the button
int buttonPushCounterB = 0; // counter for the number of button presses
int buttonStateB = 0; // current state of the button
int lastButtonStateB = 0; // previous state of the button
void setup() {
// initialize the pins:
pinMode(buttonPinA, INPUT); // initialize buttonA
pinMode(buttonPinB, INPUT); // initialize buttonB
pinMode(ledPinR, OUTPUT); //red led
pinMode(ledPinB, OUTPUT); //blue led
pinMode(ledPinG, OUTPUT); //green led
Serial.begin(9600);
}
void loop() {
// read the pushbutton pin:
buttonStateA = digitalRead(buttonPinA);
buttonStateB = digitalRead(buttonPinB);
// compare the buttonState to its previous state
if (buttonStateB != lastButtonStateB)
{
// if the state has changed, increment the counter
if (buttonStateB == HIGH)
{
buttonPushCounterA = 0;
}//sets PushCounterA to zero if pressed, resets leds
}//ends buttonB reset loop
// compare the buttonState to its previous state
if (buttonStateA != lastButtonStateA) {
if (buttonStateA == HIGH) {
buttonPushCounterA++;
if (buttonPushCounterA == 5)//resets counter every 5 presses
{
buttonPushCounterA =0;
}//ends if
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounterA, DEC);
} //ONLY FOR SERIAL COMMUNICATION
else {
// if the current state is LOW then the button
// ends else
Serial.println("off"); //ONLY USED FOR SERIAL COMMUNICATION
}//ends else
// save the current state as the last state,
//for next time through the loop
lastButtonStateA = buttonStateA;
}//ends if
//THIS WHOLE BLOCK SWITCHES THE LEDS
//BASED ON WHAT NUMBER THE COUNTER
//IS AT.
if (buttonPushCounterA == 0)
{
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinG, LOW);
}//ALL LEDS OFF
else if (buttonPushCounterA == 1){
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinG, HIGH);
}//GREEN
else if (buttonPushCounterA == 2){
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinB, HIGH);
digitalWrite(ledPinG, LOW);
delay(100);
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinG, LOW);
delay(100);
}//BLUE FLASH
else if (buttonPushCounterA == 3){
digitalWrite(ledPinR, HIGH);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinG, HIGH);
delay(100);
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinG, LOW);
delay(100);
}//YELLOW FLASH
else if (buttonPushCounterA == 4){
digitalWrite(ledPinR, HIGH);
digitalWrite(ledPinB, LOW);
digitalWrite(ledPinG, LOW);
delay(50);
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinB, HIGH);
digitalWrite(ledPinG, LOW);
delay(50);
}//RED BLUE FLASH
}//ends void
I'd like to add a third button so I measure the time in-between its presses. That way I could have a tap tempo for the strobe. I'm in a band and we do a lot of weird experimental jams, so this could be a way to signal changes. Any ideas for measuring time between presses?