Go Down

Topic: LED array controlled by sine wave? (Read 693 times) previous topic - next topic

Hey guys, I've been playing around with an array of 10 LEDs and I'd like to make them bounce back and forth. I want the speed at which they bounce back and forth to be incremented by a sine wave function, but I don't know how I'd do that. I have it all written out and working, but using linear math. Here's the code I have for example (I know it's sloppy):

Code: [Select]
const int ledCount = 10;

int ledPins[] = {4,5,6,7,8,9,10,11,12,13};
int ledPinsReverse[] = {13,12,11,10,9,8,7,6,5,4};
int slowness = 0;
int boool = true;

void setup()
{
  for (int thisLed = 0; thisLed < ledCount; thisLed++)
  {
    pinMode(ledPins[thisLed], OUTPUT);
  }
}

void loop()
{
     if(slowness==0){boool=true;}
     else if(slowness==80){boool=false;}
     
   for(int thisLed = 0; thisLed < ledCount; thisLed++)
     {
       digitalWrite(ledPins[thisLed], HIGH);
       delay(slowness);
       digitalWrite(ledPins[thisLed], LOW);
     }
   for(int thisLed = 0; thisLed < ledCount; thisLed++)
     {
       digitalWrite(ledPinsReverse[thisLed], HIGH);
       delay(slowness);
       digitalWrite(ledPinsReverse[thisLed], LOW);
     }
     if(boool==true){slowness += 10;}
     if(boool==false){slowness -= 10;}
}


For reference if you're interested in building it and manually testing it, I simply have LEDs in ports 4 through 13.

Thanks in advance!

robtillaart

sinus goes from -1 to 1 which you want to map upon the values 4..13  ==> map()

not tested
Code: [Select]
void loop()
{
  for (int i=0; i< 360; i++)  // 1 sinus per loop
  {
    float radians = i * 3.14159265/180;
    int value = sin(radians) *100;
    int led = map(value, -100, 100, 4, 13);
    digitalWrite(led, HIGH);
    delay(100);
    digitalWrite(led, LOW);
  }
}


homework is to add setup() and appropiate comments ;)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up