Advertisement
learnelectronics

Arduino Motion-controlled Dot Matrix Display

Sep 4th, 2017
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. /*
  2.  * Arduino Motion-controlled Dot Matrix Display
  3.  *
  4.  * 1 Sept 2017
  5.  * learnelectronics
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  * arduino0169@gmail.com
  9.  *
  10.  * LedControl library can be found here: https://github.com/wayoda/LedControl
  11.  * More info: https://wayoda.github.io/LedControl/pages/software
  12.  */
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. #include "LedControl.h"                           //library to control 8x8 led modules w/max7219 chip
  20.  
  21.  
  22. LedControl lc=LedControl(12,11,10,1);             //create instance of LedControl called lc
  23.                                                   //arduino pin 12 -> DIN
  24.                                                   //arduino pin 11 -> CLK
  25.                                                   //arduino pin 10 -> LOAD(CS)
  26.                                                   //number of modules in use
  27.  
  28.                                                  
  29. int a[5],k=0,rno=0;                               //variables
  30.  
  31. unsigned long delayTime=200;                      //int variable for milisecond wait time
  32.  
  33. byte invader1a[] =                                
  34. {
  35.    B00011000,                                     // First frame of invader #1
  36.    B00111100,
  37.    B01111110,
  38.    B11011011,
  39.    B11111111,
  40.    B00100100,
  41.    B01011010,
  42.    B10100101
  43. };
  44.  
  45. byte invader1b[] =                                
  46. {
  47.   B00011000,                                      // Second frame of invader #1
  48.   B00111100,
  49.   B01111110,
  50.   B11011011,
  51.   B11111111,
  52.   B00100100,
  53.   B01011010,
  54.   B01000010
  55. };
  56.  
  57. byte invader2a[] =
  58. {
  59.   B00100100,                                      // First frame of invader #2
  60.   B00100100,
  61.   B01111110,
  62.   B11011011,
  63.   B11111111,
  64.   B11111111,
  65.   B10100101,
  66.   B00100100
  67. };
  68.  
  69. byte invader2b[] =
  70. {
  71.   B00100100,                                      // Second frame of invader #2
  72.   B10100101,
  73.   B11111111,
  74.   B11011011,
  75.   B11111111,
  76.   B01111110,
  77.   B00100100,
  78.   B01000010
  79. };
  80.  
  81. void lister(){                                    //  Makes a stack of the analog input from A0
  82.   for (k=0;k<4;k++){  
  83.     a[k]=a[k+1];
  84.    }
  85.   a[4]=analogRead(A0)/75;
  86.   Serial.print("Input = ");
  87.   Serial.println(a[4]);
  88.  delay(100);
  89.  }
  90.  
  91. int check(){                                      // Checks all the values in the stack and returns 1 if any value is different
  92.   int flag=0;
  93.   for(k=0;k<4;k++){
  94.     if(a[k]!=a[k+1]){
  95.       flag=1;
  96.       break;
  97.     }
  98.    
  99.   }
  100.   return(flag);
  101. }
  102.  
  103. void sinvader1a()                                 // Draws frame one from invader #1
  104. {
  105.   for (int i = 0; i < 8; i++)  
  106.   {
  107.     lc.setColumn(0,i,invader1a[i]);
  108.   }
  109. }
  110.  
  111. void sinvader1b()                                 // Draws frame two from invader #1
  112. {
  113.   for (int i = 0; i < 8; i++)
  114.   {
  115.     lc.setColumn(0,i,invader1b[i]);
  116.   }
  117. }
  118.  
  119. void sinvader2a()                                 // Draws frame one from invader #2
  120. {
  121.   for (int i = 0; i < 8; i++)  
  122.   {
  123.     lc.setColumn(0,i,invader2a[i]);
  124.   }
  125. }
  126.  
  127. void sinvader2b()                                 // Draws frame two from invader #2
  128. {
  129.   for (int i = 0; i < 8; i++)
  130.   {
  131.     lc.setColumn(0,i,invader2b[i]);
  132.   }
  133. }
  134.  
  135. void setup(){
  136.   lc.setIntensity(0,5);                           // Set intensity level
  137.   lc.clearDisplay(0);                             // Clear Display
  138. }
  139.  
  140. void disp(long sel){                              // Displays invader depending on the arguments recived
  141.   if(sel==1){
  142.     sinvader1a();
  143.     delay(delayTime);
  144.     sinvader1b();
  145.     delay(delayTime);
  146.     }
  147.     if(sel==2){
  148.     sinvader2a();
  149.     delay(delayTime);
  150.     sinvader2b();
  151.     delay(delayTime);
  152.     }
  153.   }
  154.  
  155. void loop(){
  156.  
  157.   int inc=-1,test;
  158.   lister();
  159.   test=check();
  160.   Serial.print("Test = ");
  161.   Serial.println(test);
  162.   if (test==1){
  163.     lc.shutdown(0,false);                         //Turns on the display
  164.     disp(rno);
  165.     }
  166.    else{
  167.    
  168.     if(rno==0||rno==1)
  169.      inc=-inc;
  170.      
  171.      rno=rno+inc;
  172.      lc.shutdown(0,true);                         // Turns off the display
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement