jamstraz
Active Member
So I understand this code, and tweaking it I can get it to work with the Arduino that (finally) came today. Only problem is anytime I try to add a button it doesn't seem to work. I don't know if the code is wrong or I am not wiring it right on the breadboard to test it.
The code is as follows
/*
Fade
This example shows how to fade an LED on pin 9
using the analogWrite() function.
This example code is in the public domain.
*/
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// check to see if we are ready for flash
if (brightness > 150) {
// flash the led
analogWrite(led, 255);
delay(50);
// start the dimming effect
fadeAmount = -fadeAmount ;
Serial.println(brightness);
}
if (brightness <= 0) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
When I try to add a button to control it ala the code here and put the previous code where the button should control it, it fails.
If anyone can assist here I would be grateful. Coding gave me a headache in college and its starting to now lol. Otherwise I will try to see about joining an Arduino forum.
The code is as follows
/*
Fade
This example shows how to fade an LED on pin 9
using the analogWrite() function.
This example code is in the public domain.
*/
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// check to see if we are ready for flash
if (brightness > 150) {
// flash the led
analogWrite(led, 255);
delay(50);
// start the dimming effect
fadeAmount = -fadeAmount ;
Serial.println(brightness);
}
if (brightness <= 0) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
When I try to add a button to control it ala the code here and put the previous code where the button should control it, it fails.
If anyone can assist here I would be grateful. Coding gave me a headache in college and its starting to now lol. Otherwise I will try to see about joining an Arduino forum.