Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Arduino 3-digit counter

By: a guest on Jan 21st, 2013  |  syntax: C  |  size: 1.34 KB  |  views: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Code for 3 7-segment displays (approx. 1 second timer)
  2. // Are there better ways to do this?  Yes.
  3.  
  4. int seg[] = {6, 7, 8, 9, 10, 11, 12};
  5. int sel[] = {3, 4, 5};
  6.  
  7. int a = 0;
  8. int b = 0;
  9. int c = 0;
  10. int n = 0;
  11.  
  12. void setup() {
  13.   pinMode(seg[0], OUTPUT);
  14.   pinMode(seg[1], OUTPUT);
  15.   pinMode(seg[2], OUTPUT);
  16.   pinMode(seg[3], OUTPUT);
  17.   pinMode(seg[4], OUTPUT);
  18.   pinMode(seg[5], OUTPUT);
  19.   pinMode(seg[6], OUTPUT);
  20.   pinMode(sel[0], OUTPUT);
  21.   pinMode(sel[1], OUTPUT);
  22.   pinMode(sel[2], OUTPUT);
  23. }
  24.  
  25. void writeDigit(int pos, int val) {
  26.   digitalWrite(sel[0], (pos==0)?HIGH:LOW);
  27.   digitalWrite(sel[1], (pos==1)?HIGH:LOW);
  28.   digitalWrite(sel[2], (pos==2)?HIGH:LOW);
  29.   digitalWrite(seg[0], (val==0||val==2||val==6||val==8)?LOW:HIGH);
  30.   digitalWrite(seg[1], (val==1||val==4||val==7||val==9)?HIGH:LOW);
  31.   digitalWrite(seg[2], (val==2)?HIGH:LOW);
  32.   digitalWrite(seg[3], (val==5||val==6)?HIGH:LOW);
  33.   digitalWrite(seg[4], (val==1||val==4)?HIGH:LOW);
  34.   digitalWrite(seg[5], (val==1||val==2||val==3||val==7)?HIGH:LOW);
  35.   digitalWrite(seg[6], (val==0||val==1||val==7)?HIGH:LOW);
  36. }
  37.  
  38. void loop() {
  39.   writeDigit(0, a);
  40.   delay(3);
  41.   writeDigit(1, b);
  42.   delay(3);
  43.   writeDigit(2, c);
  44.   delay(4);
  45.   n++;
  46.   if (n == 100) {
  47.     n = 0;
  48.     c++;
  49.     if (c == 10) { c = 0; b++; }
  50.     if (b == 10) { b = 0; a++; }
  51.     if (a == 10) { a = 0; }
  52.   }
  53. }
clone this paste RAW Paste Data