learnelectronics

Fun with LEDs

Nov 1st, 2020
2,337
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /************************************
  2.  *                                  *
  3.  *        Arduino for Kids          *
  4.  *         Fun with LEDs            *
  5.  * www.youtube.com/learnelectronics *
  6.  *       arduino0169@gmail.com      *
  7.  *                                  *
  8.  *          Hookup guide:           *
  9.  *                                  *
  10.  *  Pin 2 Led1   Red        1k      *                        
  11.  *  Pin 3 Led2   Red        1k      *                  
  12.  *  Pin 4 Led3   Green      1k      *
  13.  *  Pin 5 Led4   Green      1k      *
  14.  *  Pin 6 Led5   Yellow   470R      *
  15.  *  Pin 7 Led6   Green      1k      *
  16.  *  Pin 8 Led7   Green      1k      *
  17.  *  Pin 9 Led8   Red        1k      *
  18.  *  Pin10 Led9   Red        1k      *
  19.  *                                  *    
  20.  ************************************                                  
  21.  */
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. int dly = 100;                        //delay variable, how long to wait between doing things (100=1/10sec)
  29.  
  30. void larsen(){                        //subroutine called larsen, like Knight Rider
  31.   digitalWrite(2,LOW);                //set led to off
  32.   digitalWrite(3,LOW);                //set led to off
  33.   digitalWrite(4,LOW);                //set led to off
  34.   digitalWrite(5,LOW);                //set led to off
  35.   digitalWrite(6,LOW);                //set led to off
  36.   digitalWrite(7,LOW);                //set led to off
  37.   digitalWrite(8,LOW);                //set led to off
  38.   digitalWrite(9,LOW);                //set led to off
  39.   digitalWrite(10,LOW);               //all leds are initially off
  40.  
  41.   digitalWrite(2,HIGH);               //turn on led1
  42.   delay(dly/2);                       //wait for 1/2 of our delay
  43.   digitalWrite(2,LOW);                //turn led1 off
  44.   delay(dly/2);                       //wait for 1/2 of our delay
  45.  
  46.    digitalWrite(3,HIGH);              //do the same with led2
  47.   delay(dly/2);
  48.   digitalWrite(3,LOW);
  49.   delay(dly/2);
  50.  
  51.    digitalWrite(4,HIGH);              //do the same with led3
  52.   delay(dly/2);
  53.   digitalWrite(4,LOW);
  54.   delay(dly/2);
  55.  
  56.    digitalWrite(5,HIGH);              //do the same with led4
  57.   delay(dly/2);
  58.   digitalWrite(5,LOW);
  59.   delay(dly/2);
  60.  
  61.    digitalWrite(6,HIGH);              //do the same with led5
  62.   delay(dly/2);
  63.   digitalWrite(6,LOW);
  64.   delay(dly/2);
  65.  
  66.    digitalWrite(7,HIGH);              //do the same with led6
  67.   delay(dly/2);
  68.   digitalWrite(7,LOW);
  69.   delay(dly/2);
  70.  
  71.    digitalWrite(8,HIGH);              //do the same with led7
  72.   delay(dly/2);
  73.   digitalWrite(8,LOW);
  74.   delay(dly/2);
  75.  
  76.    digitalWrite(9,HIGH);              //do the same with led8
  77.   delay(dly/2);
  78.   digitalWrite(9,LOW);
  79.   delay(dly/2);
  80.  
  81.    digitalWrite(10,HIGH);             //do the same with led9
  82.   delay(dly/2);
  83.   digitalWrite(10,LOW);
  84.   delay(dly/2);
  85.  
  86.   digitalWrite(9,HIGH);               //do the same with led10
  87.   delay(dly/2);
  88.   digitalWrite(9,LOW);
  89.   delay(dly/2);
  90.  
  91.   digitalWrite(8,HIGH);               //now we go back down until we reach led1
  92.   delay(dly/2);
  93.   digitalWrite(8,LOW);                
  94.   delay(dly/2);
  95.  
  96.   digitalWrite(7,HIGH);
  97.   delay(dly/2);
  98.   digitalWrite(7,LOW);
  99.   delay(dly/2);
  100.  
  101.   digitalWrite(6,HIGH);
  102.   delay(dly/2);
  103.   digitalWrite(6,LOW);
  104.   delay(dly/2);
  105.  
  106.   digitalWrite(5,HIGH);
  107.   delay(dly/2);
  108.   digitalWrite(5,LOW);
  109.   delay(dly/2);
  110.  
  111.   digitalWrite(4,HIGH);
  112.   delay(dly/2);
  113.   digitalWrite(4,LOW);
  114.   delay(dly/2);
  115.  
  116.   digitalWrite(3,HIGH);
  117.   delay(dly/2);
  118.   digitalWrite(3,LOW);
  119.   delay(dly/2);
  120.  
  121. }
  122.  
  123.  
  124.  
  125. void upndown(){                         //subroutine upndown (fades leds on and off)
  126.   digitalWrite(2,LOW);                  //start with all leds off
  127.   digitalWrite(3,LOW);
  128.   digitalWrite(4,LOW);
  129.   digitalWrite(5,LOW);
  130.   digitalWrite(6,LOW);
  131.   digitalWrite(7,LOW);
  132.   digitalWrite(8,LOW);
  133.   digitalWrite(9,LOW);
  134.   digitalWrite(10,LOW);
  135.  
  136.   int x = 1;                            //keep track of if we are going up or down
  137.    for (int i = 0; i > -1; i = i + x){  //count from 0 to 255
  138.       analogWrite(5, i);                //set led brightness to the count
  139.       analogWrite(6,i);                 // do the same with other led
  140.       if (i == 255) x = -1;             // switch direction at peak
  141.       delay(10);
  142.    }
  143.  
  144. x = 1;
  145.    for (int i = 0; i > -1; i = i + x){  //same as above just different leds
  146.       analogWrite(10, i);
  147.       analogWrite(9,i);
  148.       if (i == 255) x = -1;            
  149.       delay(10);
  150.    }  
  151.    
  152. }
  153.  
  154.  
  155.  
  156.  
  157. void ctrout(){                          //start at the center and move outward
  158.   digitalWrite(6,HIGH);                 //center LED on
  159.     delay(dly);                         //wait
  160.    
  161.   digitalWrite(7,HIGH);                 //turn on first pair
  162.   digitalWrite(5,HIGH);
  163.     delay(dly);                         //wait
  164.    
  165.   digitalWrite(8,HIGH);                 //turn on next outer pair
  166.   digitalWrite(4,HIGH);
  167.     delay(dly);                         //wait
  168.    
  169.   digitalWrite(9,HIGH);                 //turn on thrid pair
  170.   digitalWrite(3,HIGH);
  171.     delay(dly);                         //wait
  172.    
  173.   digitalWrite(10,HIGH);                //turn on outer pair
  174.   digitalWrite(2,HIGH);
  175.     delay(dly*6);                       //wait 6 times as long with all leds on
  176.    
  177.   digitalWrite(2,LOW);                  //turn all leds off
  178.   digitalWrite(3,LOW);
  179.   digitalWrite(4,LOW);
  180.   digitalWrite(5,LOW);
  181.   digitalWrite(6,LOW);
  182.   digitalWrite(7,LOW);
  183.   digitalWrite(8,LOW);
  184.   digitalWrite(9,LOW);
  185.   digitalWrite(10,LOW);
  186.   delay(dly*8);                         //wait 8 times as long with all leds off
  187. }
  188.  
  189. void outin() {                          //start from the outside and move in turning off each pair as the next lights
  190.  
  191. digitalWrite(10,HIGH);                  //outer pair on then off
  192. digitalWrite(2,HIGH);
  193. delay(dly);
  194. digitalWrite(10,LOW);
  195. digitalWrite(2,LOW);
  196.  
  197.  
  198.  
  199. digitalWrite(9,HIGH);                   //next inner pair on then off
  200. digitalWrite(3,HIGH);
  201. delay(dly);
  202. digitalWrite(9,LOW);    
  203. digitalWrite(3,LOW);
  204.  
  205.  
  206. digitalWrite(8,HIGH);                   //next inner pair on then off
  207. digitalWrite(4,HIGH);
  208. delay(dly);
  209. digitalWrite(8,LOW);
  210. digitalWrite(4,LOW);
  211.  
  212.  
  213. digitalWrite(7,HIGH);                   //next inner pair on then off
  214. digitalWrite(5,HIGH);
  215. delay(dly);
  216. digitalWrite(7,LOW);
  217. digitalWrite(5,LOW);
  218.  
  219.  
  220. digitalWrite(6,HIGH);                   //center led on then off
  221. delay(dly);
  222. digitalWrite(6,LOW);
  223. delay(dly*8);                           //wait extra long at the end
  224. }
  225.  
  226. void setup() {
  227.                                         // put your setup code here, to run once:
  228.  
  229.   pinMode(2,OUTPUT);                    //set pin 2 for output
  230.   pinMode(3,OUTPUT);                    //set pin 3 for output
  231.   pinMode(4,OUTPUT);                    //set pin 4 for output
  232.   pinMode(5,OUTPUT);                    //set pin 5 for output
  233.   pinMode(6,OUTPUT);                    //set pin 6 for output
  234.   pinMode(7,OUTPUT);                    //set pin 7 for output
  235.   pinMode(8,OUTPUT);                    //set pin 8 for output
  236.   pinMode(9,OUTPUT);                    //set pin 9 for output    
  237.   pinMode(10,OUTPUT);                   //set pin 10 for output
  238.   digitalWrite(2,LOW);                  //start with all leds off
  239.   digitalWrite(3,LOW);
  240.   digitalWrite(4,LOW);
  241.   digitalWrite(5,LOW);
  242.   digitalWrite(6,LOW);
  243.   digitalWrite(7,LOW);
  244.   digitalWrite(8,LOW);
  245.   digitalWrite(9,LOW);
  246.   digitalWrite(10,LOW);
  247.  
  248. }
  249.  
  250. void loop() {
  251.                                         // put your main code here, to run repeatedly:
  252.  
  253.  
  254.  for (int i=0; i <= 2; i++){            //do what's inside the {} brackets 3 times
  255.       ctrout();                         //call subroutine ctrout
  256.       delay(10);                        //very small delay
  257.    }
  258.  
  259. for (int i=0; i <= 2; i++){             //do what's inside the {} brackets 3 times
  260.       outin();                          //call subroutine outin
  261.       delay(10);
  262.    }
  263.  
  264.  
  265. for (int i=0; i <= 2; i++){             //do what's inside the {} brackets 3 times
  266.       upndown();                        //call subroutine upndown
  267.       delay(10);
  268.    }
  269.  
  270.  
  271. for (int i=0; i <= 2; i++){             //do what's inside the {} brackets 3 times            
  272.       larsen();                         //call subroutine larsen
  273.       delay(10);
  274.    }
  275.  
  276.    
  277.                                         //end of the program
  278.  
  279. }
RAW Paste Data