First Arduino project: Knight Rider, 10 leds and afterglow


hi all,

this first post on board. got first arduino (mega 2560) , started off knight rider effect on 10 leds. watched youtube videos make original possible, included afterglow.

and works out fine:


 watch video here



here's code:

code: [select]
/* knight rider 05
  board: arduino mega 2560
  led's 1 10 connected ports 2,3,4,5,6,7,8,9,10,11
  pwm values corrected "gamma"
  array used hold pwm values
*/

//led configuration
  int pinarray[] = {2,3,4,5,6,7,8,9,10,11};
  int pincount = 10;
 
//array holds pwm values
  int arrpwmvalues[] = {255,0,0,0,0,0,0,0,0,0};


//fading variables
  //delay in microseconds
  int delaytime = 1850;
  int intoffset = 210;
  //direction: right = 1, left = 0
  int intdirection = 1;


void setup(){
 
    //switch off onboard led13
    pinmode(13, output);
    digitalwrite(13, low);
   
    //set ports in pinarray[] output
    (int = 0; < pincount; i++) {
      pinmode(pinarray[i], output);
    }
   
}  //end setup


void loop() {
 
    //=============================
    //direction right

    if (intdirection == 1)
    {
     
        for(int = 0; <= 9; i++)
        {
         
            if(arrpwmvalues[i] > 0)
            {
              --arrpwmvalues[i];
            }
           
            if(arrpwmvalues[i] == intoffset && < 9)
            {
              arrpwmvalues[i+1] = 255;
            }
           
            if( == 9 && arrpwmvalues[i] == intoffset)
            {
              intdirection = 0;
              arrpwmvalues[0] = 0;
              arrpwmvalues[1] = 0;
              arrpwmvalues[2] = 0;
              arrpwmvalues[3] = 0;
              arrpwmvalues[4] = 0;
              arrpwmvalues[5] = 0;
              arrpwmvalues[6] = 0;
              arrpwmvalues[7] = 0;
              arrpwmvalues[8] = 0;
              arrpwmvalues[9] = 255;
            }  //end if
           
        }  //end for
       
    }  //end if
   
    //=============================
    //direction left
   
    if (intdirection == 0)
    {
        for(int = 0; <= 9; i++)
        {
         
          if(arrpwmvalues[i] > 0)
           {
             --arrpwmvalues[i];
           }
           
           if(arrpwmvalues[i] == intoffset && >= 0)
           {
             arrpwmvalues[i-1] = 255;
           }
           
           if( == 0 && arrpwmvalues[i] == intoffset)
           {
              intdirection = 1;
              arrpwmvalues[0] = 255;
              arrpwmvalues[1] = 0;
              arrpwmvalues[2] = 0;
              arrpwmvalues[3] = 0;
              arrpwmvalues[4] = 0;
              arrpwmvalues[5] = 0;
              arrpwmvalues[6] = 0;
              arrpwmvalues[7] = 0;
              arrpwmvalues[8] = 0;
              arrpwmvalues[9] = 0;
           }  //end if
           
        }  //end for 
       
    }  //end if
   
    //output pwm values in arrpwmvalues leds
    //value = pwmvalue^2 / 255 > perceived brightness correction
   
    for(int = 0; i<=9; i++){
      //uint8_t value = sq(arrpwmvalues[i])/255;
      analogwrite( pinarray[i], sq(arrpwmvalues[i])/255 );
    }
   
    delaymicroseconds(delaytime);
 
}  //end main loop



Arduino Forum > Community > Exhibition / Gallery > First Arduino project: Knight Rider, 10 leds and afterglow


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code