Advertisement
rafikamal

Untitled

May 24th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. byte ledMap[10][7] = {
  2.     { 1,1,1,1,1,1,0 },
  3.     { 0,1,1,0,0,0,0 },
  4.     { 1,1,0,1,1,0,1 },
  5.     { 1,1,1,1,0,0,1 },
  6.     { 0,1,1,0,0,1,1 },
  7.     { 1,0,1,1,0,1,1 },
  8.     { 1,0,1,1,1,1,1 },
  9.     { 1,1,1,0,0,0,0 },
  10.     { 1,1,1,1,1,1,1 },
  11.     { 1,1,1,0,0,1,1 }
  12. };
  13.  
  14. // digit which is being displayed on the seven-segment display
  15. byte currentDigit = 0;
  16. // how much should we increment in each second
  17. int inc = 0;
  18.  
  19. void setup() {
  20.     Serial.begin(9600);
  21.     for (byte pin = 2; pin <= 9; pin++) {
  22.         pinMode(pin, OUTPUT);
  23.     }
  24. }
  25.    
  26. void write(byte digit) {
  27.     for (byte pin = 0; pin < 7; pin++) {
  28.         digitalWrite(pin + 2, ledMap[digit][pin]);
  29.     }
  30.     currentDigit = digit;
  31. }
  32.  
  33. void clear() {
  34.     for (byte pin = 0; pin < 7; pin++) {
  35.         digitalWrite(pin + 2, 0);
  36.     }
  37. }
  38.  
  39. void loop() {
  40.     if (Serial.available()) {
  41.         byte input = Serial.read();
  42.        
  43.         switch(input) {
  44.             case 'u':       // up counter
  45.                 inc = 1;
  46.                 break;
  47.             case 'd':       // down counter
  48.                 inc = -1;
  49.                 break;
  50.             case 'c':       // clear
  51.                 clear();
  52.                 break;
  53.             case '+':       // increment by one
  54.                 write((currentDigit + 1) % 10);
  55.                 break;
  56.             case '-':       // decrement by one
  57.                 write((currentDigit + 9) % 10);
  58.                 break;
  59.             default:
  60.                 if (input >= '0' && input <= '9')
  61.                     write(input - '0');
  62.                 break;
  63.         }
  64.     }
  65.     else {
  66.         if (inc != 0) {
  67.             byte next = (currentDigit + 10 + inc) % 10;
  68.             write(next);
  69.             delay(1000);
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement