Sunday, April 8, 2012

Week 13 - Arduino Buttons!

Hey this week had lots of fun playing around with push buttons on the Arduino. The original program was written differently and needed to be corrected as the LED was on from the start and pushing the button turned it off.

After fixing this I continued with the kit's notes and updated it to use two buttons - one to turn it on and one to turn it off. Then there was an addition to make the LED fade on and off, which was cool.

Then I wanted to change it to get more acquainted with both the programming and the elements involved. After playing around with the kit's final updates, I realized that you had to hold either button down for the LED to fade through it entire range on or off. So for starters, I changed the program to no longer hold the button down to fade through its entirety. One click and the LED faded through the whole range on or off. Clicking throughout the fade was controlled so that no button click was allowed until the fade was done.

Then I realized I could keep clicking the same button over and over again and it would repeat the the fade routine. So I modified it again to have an onOff state where it detected whether it was already on or off and that clicking the same button again would have no effect on the LED.






Here's the code with different stages commented out:
/*
Button
This example code is in the public domain.
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 3; // the number of the pushbutton pin
const int buttonPin2 = 2; // the number of the pushbutton pin
const int ledPin = 9; // the number of the LED pin
int value = 0;
boolean onOff = false; // button state
boolean fadeIt = false; // button state

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}

void loop(){
// fixed original example - naiJenn - used ARDX code to fix original
/*
if (digitalRead(buttonPin2) == HIGH) { // button unpresed state = HIGH
digitalWrite(ledPin, LOW);
} else if (digitalRead(buttonPin2) == LOW) { // button presed state = LOW
digitalWrite(ledPin, HIGH);
}
*/
// Use one button to turn on and one to turn off - ARDX
/*
if (digitalRead(buttonPin1) == LOW) { // button presed state = LOW
digitalWrite(ledPin, LOW);
} else if (digitalRead(buttonPin2) == LOW) { // button presed state = LOW
digitalWrite(ledPin, HIGH);
}
*/

//Now fade on/off while button pressed - ARDX
/*
if (digitalRead(buttonPin1) == LOW) { // button presed state = LOW
value--;
} else if (digitalRead(buttonPin2) == LOW) { // button presed state = LOW
value++;
}
value = constrain(value, 0 , 255);
analogWrite(ledPin, value);
delay(10);
}
*/

// naiJenn update -Fade on/off with only clicking button once - light will fade on/off through entire range
// also don't interrupt - once button is clicked - buttons won't respond till the LED is done fading through range
// also cant repeat the same button repetitively - it now holds an onOff state so once LED is on only the Off button will work and visa versa
if (!fadeIt) {
if ((digitalRead(buttonPin1) == LOW) && (onOff == true)) { // button presed state = LOW
fadeIt = true;
for (int i = 255; i >= 0; i--) {
analogWrite(ledPin, i);
delay(10);
}
analogWrite(ledPin, 0);
onOff = false;
fadeIt = false;
} else if ((digitalRead(buttonPin2) == LOW) && (onOff == false)) { // button presed state = LOW
fadeIt = true;
for (int i = 0; i <= 255; i++) {
analogWrite(ledPin, i);
delay(10);
}
analogWrite(ledPin, 255);
onOff = true;
fadeIt = false;
}
}
}



Well that's it - and Happy Bunny Day! I also built an entire rail system that controlled 4 different directions in minecraft - took some time but was a lot of fun, and I think I made it pretty efficient too.

No comments:

Post a Comment