Advertisement
gPiOBox

Further Project - Copy Me - Arduino

Dec 4th, 2017
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.82 KB | None | 0 0
  1. /* During initiation, be sure to send every part to every max7219 and then
  2. upload it.
  3. For example, if you have five max7219's, you have to send the scanLimit 5 times
  4. before you load it-- other wise not every max7219 will get the data. The
  5. function maxInUse keeps track of this, just tell it how many max7219 you are
  6. using.
  7. */
  8.  
  9. // Added By LKPridgeon
  10. int matrix_buffer[] {255,255,255,255,255};
  11. int charList[256];
  12. int matrix_populate_delay = 0;
  13. int matrix_update_delay = 1000;
  14. int updates_per_second = 2;
  15.  
  16.  
  17. // Sloppy stuff that just 'works'
  18. int dataIn = 7;
  19. int load = 6;
  20. int clock = 5;
  21. int mx = 1; //current display
  22. int maxInUse = 5; //change this variable to set how many MAX7219's you'll use so 3 boards = 3
  23. int e = 0; // just a varialble
  24. int D;
  25. int TeamA_Score=75;
  26. int MOD;
  27. int MOD1;
  28. int FirstRead = 1;
  29. int Seg_map[] { 119, 68, 107, 109, 92, 61, 63, 100, 255, 125 };   // Maps 0-9 to required pattern
  30. // define max7219 registers
  31. byte max7219_reg_noop = 0x00;
  32. byte max7219_reg_digit0 = 0x01;
  33. byte max7219_reg_digit1 = 0x02;
  34. byte max7219_reg_digit2 = 0x03;
  35. byte max7219_reg_digit3 = 0x04;
  36. byte max7219_reg_digit4 = 0x05;
  37. byte max7219_reg_digit5 = 0x06;
  38. byte max7219_reg_digit6 = 0x07;
  39. byte max7219_reg_digit7 = 0x08;
  40. byte max7219_reg_decodeMode = 0x09;
  41. byte max7219_reg_intensity = 0x0a;
  42. byte max7219_reg_scanLimit = 0x0b;
  43. byte max7219_reg_shutdown = 0x0c;
  44. byte max7219_reg_displayTest = 0x0f;
  45. void putByte(byte data) {
  46. byte i = 8;
  47. byte mask;
  48. while (i > 0) {
  49. mask = 0x01 << (i - 1); // get bitmask
  50. digitalWrite( clock, LOW); // tick
  51. if (data & mask) { // choose bit
  52. digitalWrite(dataIn, HIGH);// send 1
  53. } else {
  54. digitalWrite(dataIn, LOW); // send 0
  55. }
  56. digitalWrite(clock, HIGH); // tock
  57. --i; // move to lesser bit
  58. }
  59. }
  60. void maxSingle( byte reg, byte col) {
  61. //maxSingle is the "easy" function to use for a //single max7219
  62. digitalWrite(load, LOW); // begin
  63. putByte(reg); // specify register
  64. putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
  65. // and load da shit
  66. digitalWrite(load, HIGH);
  67. }
  68. void maxAll (byte reg, byte col) { // initialize all MAX7219's in the system
  69. int c = 0;
  70. digitalWrite(load, LOW); // begin
  71. for ( c = 1; c <= maxInUse; c++) {
  72. putByte(reg); // specify register
  73. putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
  74. }
  75. digitalWrite(load, HIGH);
  76. }
  77. void maxOne(byte maxNr, byte reg, byte col) {
  78. //maxOne is for adressing different MAX7219's,
  79. //whilele having a couple of them cascaded
  80. int c = 0;
  81. digitalWrite(load, LOW); // begin
  82. for ( c = maxInUse; c > maxNr; c--) {
  83. putByte(0); // means no operation
  84. putByte(0); // means no operation
  85. }
  86. putByte(reg); // specify register
  87. putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
  88. for ( c = maxNr - 1; c >= 1; c--) {
  89. putByte(0); // means no operation
  90. putByte(0); // means no operation
  91. }
  92. digitalWrite(load, HIGH);
  93. }
  94. void setup () {
  95.   pinMode(dataIn, OUTPUT);
  96.   pinMode(clock, OUTPUT);
  97.   pinMode(load, OUTPUT);
  98.   Serial.begin(115200);
  99.   Serial.flush();
  100.   digitalWrite(clock, HIGH);
  101.   //initiation of the max 7219
  102.   maxAll(max7219_reg_scanLimit, 0x07);
  103.   maxAll(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
  104.   maxAll(max7219_reg_shutdown, 0x01); // not in shutdown mode
  105.   maxAll(max7219_reg_displayTest, 0x00); // no display test
  106.   for (e = 1; e <= 8; e++) { // empty registers, turn all LEDs off
  107.     maxAll(e, 0);
  108.   }
  109.   maxAll(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f value you can set to alter brightness
  110.   // range: 0x00 to 0x0f
  111.  
  112.   createCharList();
  113. }
  114.  
  115. void createCharList(){
  116.   charList[48]  = 119; //0
  117.   charList[49]  =  68; //1
  118.   charList[50]  = 107; //2
  119.   charList[51]  = 109; //3
  120.   charList[52]  =  92; //4
  121.   charList[53]  =  61; //5
  122.   charList[54]  =  63; //6
  123.   charList[55]  = 100; //7
  124.   charList[56]  = 255; //8
  125.   charList[57]  = 125; //9
  126.   charList[45]  =   8; //-
  127.   charList[71]  = 125; //g
  128.   charList[103] = 125;
  129.   charList[80]  = 122; //p
  130.   charList[112] = 122;
  131.   charList[73]  = 256; //Special Case for lower case i
  132.   charList[105] = 256;
  133.   charList[79]  = 119; //o
  134.   charList[111] = 119;
  135.   charList[76]  =  19; //l
  136.   charList[108] =  19;
  137.   charList[85]  =   7; //u
  138.   charList[117] =   7;
  139.   charList[84]  =  27; //t
  140.   charList[116] =  27;
  141.   charList[67]  =  51; //c
  142.   charList[99]  =  51;
  143.   charList[83]  =  61; //s
  144.   charList[115] =  61;
  145.   charList[82]  =  10; //r
  146.   charList[114] =  10;
  147.   charList[72]  =  30; //h
  148.   charList[104] =  30;
  149.   charList[69]  =  59; //e
  150.   charList[101] =  59;
  151.   charList[65]  = 126; //a
  152.   charList[97]  = 126;
  153.   charList[88]  = 256; //Special Case for Do Not Update
  154.   charList[32]  =   0; //Blank
  155.   charList[33]  = 256; //Special Case for !
  156.   charList[63]  = 256; //Special Case for ?
  157. }
  158.  
  159. // Matrix(mx) details 1=TeamA units,2=TeamA Tens,3=TeamB units,4=TeamB Tens
  160. void loop () {
  161.   LED(D); // Write Out
  162.  
  163.   /*for (int i; i<maxInUse; i++){
  164.     for(int j; j<8; j++){
  165.       maxOne((maxInUse-i), j, matrix_buffer[i]);
  166.     }
  167.   } */
  168.  
  169.   delay (1000/updates_per_second);
  170. }
  171.  
  172.  
  173. void LED(int D){
  174.   getChar();
  175.   Serial.flush();
  176. }
  177.  
  178. void getChar() {
  179.   if (Serial.available()){
  180.     while (Serial.available()){
  181.       char data[0];
  182.       Serial.readBytes(data, maxInUse);
  183.       data[maxInUse] = 0; // Bad way to remove overflow
  184.       Serial.println(data);
  185.       //Serial.println(maxInUse);
  186.       for (int i=0; i<maxInUse; i++){
  187.         //Serial.println("It:" + i);
  188.         if(charList[data[i]] != 256){
  189.           for(int t1=0; t1<8; t1++){
  190.             maxOne((maxInUse-i), t1, charList[data[i]]);
  191.           }
  192.         }
  193.         else
  194.         {
  195.           specialCase(data[i], (maxInUse-i));
  196.         }
  197.         //Serial.println(charList[data[i]]);
  198.        
  199.         delay (matrix_populate_delay);
  200.       }
  201.      
  202.       delay (matrix_update_delay);
  203.     }
  204.   }
  205. }
  206.  
  207.  
  208. void specialCase(int data, int DID){
  209.   switch (data)  {
  210.     case 67:
  211.       generateI(DID);
  212.       break;
  213.     case 105:
  214.       generateI(DID);    
  215.       break;
  216.     case 33:
  217.       generateEMark(DID);
  218.       break;
  219.     case 63:
  220.       generateQMark(DID);
  221.       break;
  222.     case 88:
  223.       break;
  224.     case 120:
  225.       break;
  226.     default:
  227.       break;
  228.   }
  229. }
  230.  
  231. void generateI(int DID){
  232.   maxOne(DID, 1, 4); // mx=current matrix, D=Pattern Match for Digit to be LIT
  233.   maxOne(DID, 2, 4);
  234.   maxOne(DID, 3, 68);
  235.   maxOne(DID, 4, 4);
  236.   maxOne(DID, 5, 4);
  237.   maxOne(DID, 6, 4);
  238.   maxOne(DID, 7, 4);
  239.   //Serial.println("Generating lower case i");
  240. }
  241.  
  242. void generateEMark(int DID){
  243.   maxOne(DID, 1, 68);
  244.   maxOne(DID, 2, 64);
  245.   maxOne(DID, 3, 68);
  246.   maxOne(DID, 4, 68);
  247.   maxOne(DID, 5, 68);
  248.   maxOne(DID, 6, 68);
  249.   maxOne(DID, 7, 68);
  250. }
  251.  
  252. void generateQMark(int DID){
  253.   maxOne(DID, 1, 106);
  254.   maxOne(DID, 2, 106);
  255.   maxOne(DID, 3, 106);
  256.   maxOne(DID, 4, 106);
  257.   maxOne(DID, 5, 104);
  258.   maxOne(DID, 6, 106);
  259.   maxOne(DID, 7, 106);
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement