Advertisement
otakus

Arduino 7segment display

Jun 29th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. //////////////////////////////////////////////////////
  2. //   Author: Sanel Smajlovic
  3. //   Licence: N/A do w/e you please
  4. //
  5. //   Dispaly Setup:
  6. //    aaa
  7. //   f   b
  8. //   f   b
  9. //    ggg
  10. //   e   c
  11. //   e   c
  12. //    ddd   h
  13. //
  14. //  Setup for common cathode. For common anode invert the 1's and 0's in table below***
  15. //  You should use resistors(220 Ohm suggested for 5V boards) for each connection beside ground for uniform brightness
  16.  
  17.  
  18. //Pin Setup: Change accordingly
  19. int a=6;
  20. int b=5;
  21. int c=12;
  22. int d=11;
  23. int e=10;
  24. int f=8;
  25. int g=9;
  26. int h=13;
  27.  
  28. int segs[]={a,b,c,d,e,f,g};         //Simple Array
  29. byte segmap[][7]={{1,1,1,1,1,1,0},  //0  ***Invert the 1's and 0's for common anode
  30.                   {0,1,1,0,0,0,0},  //1
  31.                   {1,1,0,1,1,0,1},  //2
  32.                   {1,1,1,1,0,0,1},  //3
  33.                   {0,1,1,0,0,1,1},  //4
  34.                   {1,0,1,1,0,1,1},  //5
  35.                   {1,0,1,1,1,1,1},  //6
  36.                   {1,1,1,0,0,0,0},  //7
  37.                   {1,1,1,1,1,1,1},  //8
  38.                   {1,1,1,1,0,1,1},  //9
  39.                   {1,0,0,1,1,1,1}}; //E - Error code
  40.  
  41. // the setup routine runs once when you press reset:
  42. void setup() {                
  43.   for(int i=0;i<sizeof(segs);i++){    //Iterate through all the pins and set them as outputs
  44.     pinMode(segs[i], OUTPUT);
  45.   }
  46.   pinMode(h,OUTPUT);                  //Lets not forget the dot
  47. }
  48.  
  49. // the loop routine runs over and over again forever:
  50. void loop() {
  51.   for(int i=0;i<=10;i++){            //Iteratate from 0-9, any number less than 0 or greated than 9 will display "E" for error.
  52.     disp7seg(i);
  53.     delay(1000);                    //1000 millisecond(1 second) pause
  54.   }
  55.   disp7segDot(HIGH);                //Turn on the dot and turn it off after 0.5 seconds
  56.   delay(500);        
  57.   disp7segDot(LOW);                  
  58. }
  59.  
  60. //Dot control function
  61. void disp7segDot(byte state){
  62.   digitalWrite(h,state);
  63. }
  64.  
  65. //7 segment driver function
  66. void disp7seg(int num){
  67.   for(int i=0;i<7;i++){                          //Iterate through all the segments
  68.     if(num>=0 && num<10){                        //Check for valid number
  69.       digitalWrite(segs[i],segmap[num][i]);      //Light the segment for corrosponding number
  70.     }
  71.     else{
  72.       digitalWrite(segs[i],segmap[10][i]);       //Light the segment to display "E"
  73.     }
  74.   }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement