Thanks TroyO i'll have a look threw your code.
Well everything is working great. I only have 5 10k resistors so one of the buttons has a 100 ohm on it. I'm not sure of what Groove is referring too. Do i add lines to the void setup or in the loop to tell it to digital write the buttons?
I found one other problem with my sketch. It seams pin 12 doesn't do analogWrite. so i adjusted things so the leds are on pins 9, 10, and 11.
Here is the final code for those interested.
/*
RGB Color Wheel
Indivigually control the brightness of a Red LED, Blue LED and
Green LED using 6 pushbuttons attached to pins 2 thru 7. The
pushbuttons on the even number pins will brighten the corosponding
LED and the odd numbered ones will dim them. LEDs will be attached
to pins 10, 11, and 12.
The Circuit:
* Red LED from pin 9 to ground threw a 220 Ohm resistor
* Blue LED from pin 10 to ground threw a 220 Ohm Resistor
* Green LED from Pin 11 to ground threw a 220 Ohm Resistor
* pushbutton 1 attached to pin 2 from +5V
* 10k resistor attached to pin 2 from ground
* pushbutton 2 attached to pin 3 from +5V
* 10k resistor attached to pin 3 from ground
* pushbutton 3 attached to pin 4 from +5V
* 10k resistor attached to pin 4 from ground
* pushbutton 4 attached to pin 5 from +5V
* 10k resistor attached to pin 5 from ground
* pushbutton 5 attached to pin 6 from +5V
* 10k resistor attached to pin 6 from ground
* pushbutton 6 attached to pin 7 from +5V
* 10k resistor attached to pin 7 from ground
created 13 Jan 2010
by digimike
modified 14 jan 2010
by digimike
*/
int redled = 9; // assign LEDs to pins
int greenled = 10;
int blueled = 11;
int button2 = 2; // assign pushbuttons to pins
int button3 = 3;
int button4 = 4;
int button5 = 5;
int button6 = 6;
int button7 = 7;
int button2State = 0; // variable for reading the pushbuttons
int button3State = 0;
int button4State = 0;
int button5State = 0;
int button6State = 0;
int button7State = 0;
int fadered = 0;
int fadegreen = 0;
int fadeblue = 0;
void setup () {
pinMode(button2, INPUT); //initialize pushbutton pins as input:
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(button6, INPUT);
pinMode(button7, INPUT);
}
void loop() {
button2State = digitalRead(button2); //check button state
if(button2State == HIGH && fadered <= 250) {
// if button is pressed increase brightness by 5
// as long as brightness isn't greater than 250
fadered +=5;
analogWrite(redled, fadered); // lights LED at current brightness level
delay(250); // allows time so button won't be detected multiple times
}
button3State = digitalRead(button3); //check button state
if (button3State == HIGH && fadered >= 5) {
// if button is pressed decrease brightness by 5
// as long as brightness isn't less than 5
fadered -=5;
analogWrite(redled, fadered); // lights LED at current brightness level
delay(250); // allows time so button won't be detected multiple times
}
button4State = digitalRead(button4); //check button state
if (button4State == HIGH && fadegreen <= 250) {
// if button is pressed increase brightness by 5
// as long as brightness isn't greater than 250
fadegreen +=5;
analogWrite(greenled, fadegreen); // lights LED at current brightness level
delay(250); // allows time so button won't be detected multiple times
}
button5State = digitalRead(button5); //check button state
if (button5State == HIGH && fadegreen >= 5) {
// if button is pressed decrease brightness by 5
// as long as brightness isn't less than 250
fadegreen -=5;
analogWrite(greenled, fadegreen); // lights LED at current brightness level
delay(250); // allows time so button won't be detected multiple times
}
button6State = digitalRead(button6); //check button state
if (button6State == HIGH && fadeblue <= 250) {
// if button is pressed increase brightness by 5
// as long as brightness isn't greater than 250
fadeblue += 5;
analogWrite(blueled, fadeblue); // lights LED at current brightness level
delay(250); // allows time so button won't be detected multiple times
}
button7State = digitalRead(button7); //check button state
if (button7State == HIGH && fadeblue >= 5) {
// if button is pressed decrease brightness by 5
// as long as brightness isn't less than 250
fadeblue -= 5;
analogWrite(blueled, fadeblue); // lights LED at current brightness level
delay(250); // allows time so button won't be detected multiple times
}
}
Hope its as simple and clean as i wanted it to be. I like to try and keep things as simple as possible. The last time i did any amount of programing was on my old TI-85 calc using TIBasic. Doesn't take long for basic to eat up the little memory the calc had.
EDIT:
After reviewing TroyO's code i think i figured out what you meant about the buttons.
So my setup will now look like this, right?
void setup () {
pinMode(button2, INPUT); //initialize pushbutton pins as input:
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(button6, INPUT);
pinMode(button7, INPUT);
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
digitalWrite(button4, HIGH);
digitalWrite(button5, HIGH);
digitalWrite(button6, HIGH);
digitalWrite(button7, HIGH);
}
Then of course the if statements will be looking for the button to read LOW. It works like a charm. Thanks.
Next i think i'll have the leds blink a couple of times if i hit the 255 max. Its hard to tell if its on full. its seams to be as bright as it will get at around 150. guess i should have it report back to the PC what teh level is. but i havn't gotten that far in learning this yet.