Advertisement
Guest User

matrix4x5.ino

a guest
Dec 14th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. /* Created:   Fri Dec 2 16
  2.  * Processor: ATmega328P
  3.  * Compiler:  Arduino AVR
  4.  */
  5. #define DEBUG                           // Definizione che permette il debug
  6.                                         // Se commentato, nessun debug
  7. #include <TimerOne.h>                   // usato per le temporizzazioni
  8.  
  9. #define INBTN 2                          // segnale di ingresso
  10. #define TMPRESS 10                      // piedino del suono
  11. #define TIMEOUT 7200000UL               // fine tempo contatore
  12. #define NUMCOLS 4                       // numero di colonne
  13. #define NUMROWS 5                       // numero di righe
  14. #define MAX_LEDS NUMCOLS * NUMROWS      // numero di led
  15. #define BLINKTIME 10
  16.  
  17. uint8_t data[NUMCOLS];
  18. uint8_t led_count = 0;
  19. uint8_t ticks = 0;
  20. uint32_t period = 0;                        // periodo da contare per 2 ore
  21. uint8_t led_blink = 1;
  22. bool push_btn;
  23.  
  24. void countdown () {
  25.     period = millis();
  26.             #ifdef DEBUG
  27.             Serial.println("Inizio il conteggio");
  28.             #endif
  29.     while (period < (millis()-TIMEOUT)) {};
  30.     clrmem();
  31.     led_count = 0;
  32. }
  33.  
  34. void refresh() {
  35.     if (ticks < NUMCOLS-1) ticks++;
  36.     else ticks = 0;
  37.     PORTC = 0;
  38.     PORTB = data[ticks];
  39.     bitWrite(PORTC, ticks, 1);
  40.     delay(23);
  41. }
  42.  
  43. void clrmem() {
  44.     for (uint8_t x=0; x < NUMCOLS; x++) data[x] = 0;
  45. }
  46.  
  47. void btn_press() {
  48.     uint8_t col;
  49.     if (pulseIn(INBTN,LOW) > TMPRESS && (led_blink == 1)) {
  50. #ifdef DEBUG
  51.         Serial.print("led_count = ");
  52.         Serial.println(led_count, DEC);
  53. #endif
  54.         push_btn = 1;
  55.     }
  56.     if (push_btn) {
  57.       if (led_blink < 5) {
  58.         led_blink++;
  59.         col = led_count / NUMROWS;
  60.         bitWrite(data[col], led_count % NUMROWS, led_blink % 2);
  61.         delay(BLINKTIME);
  62.       }
  63.       else {
  64.         led_count++;
  65.         led_blink = 1;
  66.         push_btn = 0;
  67.       }
  68.     }
  69. }
  70.  
  71. void setup(){
  72.     #ifdef DEBUG
  73.     Serial.begin(57600);
  74.     #endif
  75.     for (uint8_t x=8; x < 23; x++) {
  76.         pinMode(x, OUTPUT);
  77.     }
  78.     clrmem();
  79.     pinMode(INBTN, INPUT_PULLUP);
  80.     #ifdef DEBUG
  81.     Serial.println(F("Monitor pronto"));
  82.     #endif
  83.     Timer1.initialize(9761);            // set a timer of length 5 milliseconds
  84.     Timer1.attachInterrupt(refresh);    // attach the service routine here
  85. }
  86.  
  87. void loop() {
  88.     btn_press();
  89.     if (led_count >= MAX_LEDS) countdown();
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement