Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  1. const int LEEG_DISPLAY = 1;
  2. const int ROLLEN = 2;
  3. const int UITROLLEN = 3;
  4. const int TOON_WAARDE = 4;
  5.  
  6. int huidigeToestand = LEEG_DISPLAY;
  7.  
  8. const int SEQUENCES = 16;
  9. byte LEDsequence[SEQUENCES] = { B11111100, // 0
  10.                                 B01100000, // 1
  11.                                 B11011010, // 2
  12.                                 B11110010, // 3
  13.                                 B01100110, // 4
  14.                                 B10110110, // 5
  15.                                 B10111110, // 6
  16.                                 B11100000, // 7
  17.                                 B11111110, // 8
  18.                                 B11110110, // 9
  19.                                 B11101110, // A
  20.                                 B00111110, // B
  21.                                 B10011100, // C
  22.                                 B01111010, // D
  23.                                 B10011110, // E
  24.                                 B10001110  // F
  25.                               };
  26.  
  27. const int dataPin = 6;  // loopt naar 14 DS
  28. const int latchPin = 8; // loopt naar 12 ST_CP
  29. const int clockPin = 10; // loopt naar 11 SH_CP
  30.  
  31. unsigned long int previousMillis;
  32. int INTERVAL = 1000;
  33.  
  34. void setup() {
  35.  
  36.   Serial.begin(9600);
  37.   initializePins();
  38.   previousMillis = 0 - INTERVAL;
  39.   serialFlush();
  40. }
  41.  
  42. void serialFlush() {
  43.   while (Serial.available()  > 0)
  44.   {
  45.     Serial.read();
  46.   }
  47. }
  48.  
  49. void loop() {
  50.   listenSerialPort();
  51.   currentSeq();
  52. }
  53.  
  54. void listenSerialPort() {
  55.   if (Serial.available()) {
  56.     checkEditInput(Serial.read());
  57.   }
  58. }
  59.  
  60. void initializePins() {
  61.  
  62.   pinMode(dataPin, OUTPUT);
  63.   pinMode(latchPin, OUTPUT);
  64.   pinMode(clockPin, OUTPUT);
  65. }
  66.  
  67. void checkEditInput(char input) {
  68.   Serial.print(input);
  69.   switch (input) {
  70.     case 'S':
  71.       huidigeToestand = ROLLEN;
  72.       loopThroughSequences();
  73.       break;
  74.     case 'H':
  75.       huidigeToestand = UITROLLEN;
  76.       loopThroughSequences();
  77.       break;
  78.     case 'E':
  79.       huidigeToestand = LEEG_DISPLAY;
  80.       turnOff();
  81.       break;
  82.   }
  83. }
  84.  
  85. void currentSeq() {
  86.  
  87.   switch (huidigeToestand) {
  88.     case LEEG_DISPLAY:
  89.       turnOff();
  90.       break;
  91.     case ROLLEN:
  92.       loopThroughSequences();
  93.       break;
  94.     case UITROLLEN:
  95.       loopThroughSequences();
  96.       break;
  97.     case TOON_WAARDE:
  98.      
  99.       break;
  100.   }
  101. }
  102.  
  103. int index;
  104.  
  105. void loopThroughSequences() {
  106.  
  107.   unsigned long currentMillis = millis();
  108.  
  109.   if (currentMillis - previousMillis >= INTERVAL) {
  110.     if ( huidigeToestand == ROLLEN ) {
  111.       int spd = -100;
  112.       speedFix(spd);
  113.     }
  114.    
  115.     if ( huidigeToestand == UITROLLEN | huidigeToestand == TOON_WAARDE) {
  116.       int spd = 100;
  117.       speedFix(spd);
  118.       showRoll(LEDsequence[index]);
  119.       previousMillis = 0;
  120.     }
  121.    
  122.     myfnUpdateDisplay(LEDsequence[index]);
  123.     index++;
  124.  
  125.     if (index >= SEQUENCES) {
  126.       index = 0;
  127.     }
  128.  
  129.     previousMillis = currentMillis;
  130.   }
  131.  
  132. }
  133.  
  134. void showRoll (byte bits) {
  135.   unsigned long currentMillis = millis();
  136.   if ( currentMillis - previousMillis >= 3500) {
  137.     myfnUpdateDisplay(bits);
  138.   }
  139. }
  140.  
  141.  
  142. void speedFix(int spd) {
  143.   if (INTERVAL + spd >= 100) {
  144.     INTERVAL += spd;
  145.   }
  146. }
  147.  
  148. void turnOff() {
  149.   INTERVAL = 1000;
  150.   index = 0;
  151.   int lampsOff = B00000000;
  152.   myfnUpdateDisplay(lampsOff);
  153. }
  154.  
  155. void myfnUpdateDisplay(byte eightBits) {
  156.  
  157.   digitalWrite(latchPin, LOW);  // prepare shift register for data
  158.   shiftOut(dataPin, clockPin, LSBFIRST, eightBits); // send data
  159.   digitalWrite(latchPin, HIGH); // update display
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement