Go Down

Topic: Re: create a 'wave' with 5 Leds (Read 961 times) previous topic - next topic

antonio_elb

Although I firgured out to create the 'wave' they (Leds) start after eachother instead that they start after the led before is at its peak 255. Does someone know how I can solve this problem? The code below is what I use now.
Code: [Select]


int Pin5 = 5;       // Led 1
int Pin3 = 3;       // Led 5
int Pin9 = 9;       // Led 2
int Pin10 = 10;       // Led 3
int Pin11 = 11;       // Led 4      (the 'wave' goes Led: 1-2-3-4-5)

int Value5 = 0;    //Value Led1
int Value3 = 0;    //Value Led5
int Value9 = 0;    //Value Led2
int Value10 = 0;    //Value Led3
int Value11 = 0;    //Value Led4
       
int analog0Pin = 0;      // button to start wave
int val0 = 0;             // variable to store the value read

int threshold = 512;     // threshold for button
int timer = 100;

void setup()  
{
 
 pinMode(analog0Pin, INPUT);
 pinMode(Pin5, OUTPUT);   // sets the digital pin as output
 pinMode(Pin3, OUTPUT);   // sets the digital pin as output
 pinMode(Pin9, OUTPUT);   // sets the digital pin as output
 pinMode(Pin10, OUTPUT);   // sets the digital pin as output
 pinMode(Pin11, OUTPUT);   // sets the digital pin as output    
 
 Serial.begin(9600);          //  setup serial

}


void loop()
{
 val0 = analogRead(analog0Pin);    // read the input pin for button
 Serial.println(val0);             // debug value

if (val0 >= threshold)                //

{
 for(Value5 = 0 ; Value5 <= 255; Value5+=5) // fade in (from min to max) Led 1
 {
   analogWrite(Pin5, Value5);           // sets the value (range from 0 to 255)
   delay(30);                            // waits for 30 milli seconds to see the dimming effect
 }
 for(Value5 = 255; Value5 >=0; Value5-=5)   // fade out (from max to min)
 {
   analogWrite(Pin5, Value5);
   delay(30);
     }
 
 
  if (Value5 = 255)                //

{
 for(Value9 = 0 ; Value9 <= 255; Value9+=5) // fade in (from min to max) Led 2
 {
   analogWrite(Pin9, Value9);           // sets the value (range from 0 to 255)
   delay(30);                            // waits for 30 milli seconds to see the dimming effect
 }
 for(Value9 = 255; Value9 >=0; Value9-=5)   // fade out (from max to min)
 {
   analogWrite(Pin9, Value9);
   delay(30);
     }
 
   
    if (Value9 = 255)                //

{
 for(Value10 = 0 ; Value10 <= 255; Value10+=5) // fade in (from min to max) Led 3
 {
   analogWrite(Pin10, Value10);           // sets the value (range from 0 to 255)
   delay(30);                            // waits for 30 milli seconds to see the dimming effect
 }
 for(Value10 = 255; Value10 >=0; Value10-=5)   // fade out (from max to min)
 {
   analogWrite(Pin10, Value10);
   delay(30);
     }
 
}

     if (Value10 = 255)                //

{
 for(Value11 = 0 ; Value11 <= 255; Value11+=5) // fade in (from min to max) Led 4
 {
   analogWrite(Pin11, Value11);           // sets the value (range from 0 to 255)
   delay(30);                            // waits for 30 milli seconds to see the dimming effect
 }
 for(Value11 = 255; Value11 >=0; Value11-=5)   // fade out (from max to min)
 {
   analogWrite(Pin11, Value11);
   delay(30);
     }
 
}

      if (Value11 = 255)                //

{
 for(Value3 = 0 ; Value3 <= 255; Value3+=5) // fade in (from min to max) Led 5
 {
   analogWrite(Pin3, Value3);           // sets the value (range from 0 to 255)
   delay(30);                            // waits for 30 milli seconds to see the dimming effect
 }
 for(Value3 = 255; Value3 >=0; Value3-=5)   // fade out (from max to min)
 {
   analogWrite(Pin3, Value3);
   delay(30);
       }
 
    }

   }

 }
 
}






There might be a shorter and smarter code but therefore I need to gain more experience ;)

MysteriousAges

After reading through your code (albeit rather quickly), I'm surprised that any light beside the one on pin 5 comes on at all.
I did something similar with only 3 LEDs, and a potentiometer to control the speed a while back. I'll post up the code, but be warned, it doesn't contain many comments. : |

Code: [Select]
int led_blue = 9;
int led_green = 10;
int led_red = 11;

int pot_pin = 0;

int blue_val = 0;
int green_val = 0;
int red_val = 255;

int fade_val = 0;
int temp = 0;

void setup()
{
 pinMode(led_blue, OUTPUT);
 pinMode(led_green, OUTPUT);
 pinMode(led_red, OUTPUT);
 
 //Serial.begin(9600);
}

void loop()
{
 analogWrite(led_blue, blue_val);
 analogWrite(led_green, green_val);
 analogWrite(led_red, red_val);
 
 temp = analogRead(pot_pin);
 
 temp = temp / 20 + 1;
 
 if (temp != fade_val)
 {
   //Serial.print("Fade changed from ");
   //Serial.print(fade_val, DEC);
   //Serial.print(" to: ");
   //Serial.println(temp, DEC);
   
   fade_val = temp;
 }
 
 if (red_val == 255)
 {
   if (blue_val > 0)
   {
     blue_val--;
   }
   else if (green_val < 255)
   {
     green_val++;
     if (green_val == 255)
     {
       red_val--;
     }
   }
 }
 else if (green_val == 255)
 {
   if (red_val > 0)
   {
     red_val--;
   }
   else if (blue_val < 255)
   {
     blue_val++;
     if (blue_val == 255)
     {
       green_val--;
     }
   }
 }
 else if (blue_val == 255)
 {
   if (green_val > 0)
   {
     green_val--;
   }    
   else if (red_val < 255)
   {
     red_val++;
     if (red_val == 255)
     {
       blue_val--;
     }
   }
 }
 
 delayMicroseconds(fade_val * 10);
}


Basically what it's doing is every iteration of the loop, it will write the values computed last go-round to the PWM pins, and then figure out which value needs to increment/decrement. Hopefully it'll help you figure out what's going on with yours. ^_^;

simond

This appears remarkably similar to your earlier posting (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1206438619/0#0), to which you received two excellent responses. If you are having trouble understanding those responses, the best thing would be to follow up to the original, rather than posting a new topic for the same/ similar problem.

That said, I think your approach is going to be error prone to say the least. You would be much better off learning how to use arrays effectively, than having separate variables named for a pin or LED tracking their own state.

Go Up