Advertisement
learnelectronics

Arduino Motion-controlled Dot Matrix Display

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