So I made the following sketch, it works fine on my Duemilanove hooked up to my MacBook's USB port, but when I remove the chip, put it on the breadboard, and wire everything up, I get nothing. Obviously, I'm not wiring it properly.
Here's how I have it wired:
- Positive line from battery pack to pin 7 (VCC)
- Negative line from battery pack to pin 8 (GND)
- Positive lead from blue LED to pin 15 (digital pin 9)
- Positive lead from red LED to pin 16 (digital pin 10)
- Common negative from both LEDs to negative lead of battery pack
- Switch wire to pin 4 (digital pin 2) with switch leads connected to +/- battery pack leads (Is this necessary? Does the switch need to be powered?)
The battery pack in the photo is four AAAs, but in the final project it will be three AAs.
The sketch starts off with the blue 3-Watt LED on and the red 3-Watt LED off. When the button is pressed, blue goes off and red goes on. When pressed again, it goes into flashing mode, alternating between blue and red. When pressed again, it goes back to blue-on/red-off.
// 3-mode blue and red
const int ledRed = 10; // assign red LED to pin 10
const int ledBlue = 9; // assign blue LED to pin 9
const int buttonPin = 2; // assign tactile switch to pin 2int button = 0; // current state of button
int previousButton = 0; // previous debounced state of button
unsigned long deBounceTimer = 0;
const unsigned long deBounceDelay = 6; // increase if you get multiple presses in one buttonpress.
int x = 0; // a number that tells us where we are in the cyclevoid setup() // setting things up
{
pinMode(ledBlue, OUTPUT); // set blue LED pin to output
pinMode(ledRed, OUTPUT); // set red LED pin to output
pinMode(buttonPin,INPUT); // set button pin as input
}
void loop() // starting loop
{
button = digitalRead(buttonPin); // read current state of the button
// If button changed state to pressed and stayed there for > deBounceDelay milliseconds:
if (button == previousButton) deBounceTimer = millis();
if (!button && previousButton && (millis()-deBounceTimer) > deBounceDelay)
{
x++;
if (x>2)x=0; // variable x is reset to 0 and the cycle begins again
previousButton = button; // set previous state to current state which is debounced and assumed sure.
deBounceTimer = millis(); // reset debouncetimer
}
// If button changed state to not pressed and stayed there for > deBounceDelay milliseconds:
if (button && !previousButton && (millis()-deBounceTimer) > deBounceDelay)
{
previousButton = button; // set previous state to current state which is debounced and assumed sure.
deBounceTimer = millis(); // reset debouncetimer
}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
if (!button && previousButton && (millis()-deBounceTimer) > deBounceDelay)
{
x++;
if (x>2)x=0; // variable x is reset to 0 and the cycle begins again
previousButton = button; // set previous state to current state which is debounced and assumed sure.
deBounceTimer = millis(); // reset debouncetimer
}
// If button changed state to not pressed and stayed there for > deBounceDelay milliseconds:
if (button && !previousButton && (millis()-deBounceTimer) > deBounceDelay)
{
previousButton = button; // set previous state to current state which is debounced and assumed sure.
deBounceTimer = millis(); // reset debouncetimer
}
digitalWrite(ledRed, LOW); // turns off red
digitalWrite(ledBlue, HIGH); // turns on blue
delay(300); // waits 300 milliseconds
digitalWrite(ledBlue, LOW); // turns off blue
digitalWrite(ledRed, HIGH); // turns on red
delay(300); // waits 300 milliseconds
}
}