Hi folks,
I am pretty new to Arduino and very new to the site. I have multiple questions but I will reserve this post for a specific challenge that I am having with. I am currently experimenting with the code for PWM exercise from Massimo Banzi’s book “Getting Started with Arduino”. Here is a link to the code that I am trying to modify:
http://cdn.makezine.com/make/books/getstartedarduino/eg/Example_04.txt
What I am trying to do, is modify the code such that I can have 3 LEDs fade in and out such that the second LED fades in when the first one’s brightness reaches 50%, and the third LED starts to fade in when the second LED reaches 50% brightness. According to the book, analogWrite(x, 128) will set an LED to approximately 50% of its brightness since it accepts a value between 0-255.
Based on this I was thinking that I could use the following logic in my code:
#define LED1 9
#define LED 2 10
#define LED3 11
int i = 0; //brightness counter for LED 1
int j = 0; //brightness counter for LED 2
int k = 0; //brightness counter for LED 3
void setup(){
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
void loop(){
for (i = 0; i < 255; i++){
digitalWrite(LED1, i); //fade in LED 3
delay(10);
}
…now here is where I try to change things such that LED2 starts when LED 1 gets to 50%. I use the same method for LED 3 so I don’t think there is any need for me to paste the entire sketch here:
for(i = 128; i < 255; j++){ //here I am trying to tell j to start incrementing once i reaches 128 (50%)
digitalWrite(LED2, j);
delay(10)
The result isn’t what I expected. Instead of fading up to full brightness LED 2 seems to loop at a halfway point and the code never appears to get to LED3. I know that I may need to state somewhere that j should continue to increment until its own counter reaches 255, but I am not sure where to put it. On the other hand I may be totally off track here.
In any case my goal is to learn by experimenting with the code in the book in an effort to increase my understanding. What I was hoping to see was the LEDs fading in and out and the combination of a red blue and green LEDs creating different colors.
As I said, I am new at all this so any guidance or recommendations are greatly appreciated.
Thanks,
~Rick