Advertisement
learnelectronics

Arduino for kids - mood light

Jul 5th, 2017
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. /*
  2.  * Arduino for kids - Mood Light
  3.  *
  4.  * learnelectronics
  5.  * 4 JUL 2017
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  *
  9.  * Common Cathode RGB LED
  10.  *
  11.  *            [   ]
  12.  *            [   ]
  13.  *            -----
  14.  *            ||||
  15.  *            RGGB
  16.  *            ERRL
  17.  *            DOEU
  18.  *             UEE
  19.  *             N
  20.  *             D
  21.  *        
  22.  */
  23.  
  24.  
  25. #define led0    9                                 //RED LED on PWM pin 9
  26. #define led1    10                                //GREEN LED on PWM pin 9
  27. #define led2    12                                //BLUE LED on PWM pin 9
  28.  
  29. int brightness = 200;                             //Set brightnes refererence
  30. int red = 0;                                      //Set RED to 0
  31. int blue = 0;                                     //Set Blue to 0
  32. int green = 0;                                    //Set Green to 0
  33.  
  34.  
  35. void setup() {
  36.  
  37.   pinMode(led0,OUTPUT);                           //sets pin 9 to output
  38.   pinMode(led1,OUTPUT);                           //sets pin 10 to output
  39.   pinMode(led2,OUTPUT);                           //sets pin 11 to output
  40.  
  41. }
  42.  
  43. void loop() {
  44.  
  45.   for (float x= 0;x <PI; x = x + 0.000004){
  46.     red = brightness * abs(sin(x*(180/PI)));            //do some trig to calculate a value for red
  47.     green = brightness * abs(sin((x+PI/3)*(180/PI)));   //do some trig to calculate a value for green
  48.     blue = brightness * abs(sin((x+2*PI)/3)*(180/PI));  //do some trig to calculate a value for blue
  49.  
  50.     analogWrite(led0, red);                       //send the value for red to red LED
  51.     analogWrite(led1, green);                     //send the value for green to green LED
  52.     analogWrite(led2, blue);                      //send the value for blue to blue LED
  53.  
  54.   }                                               //end of for loop
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement