Advertisement
Guest User

Arduino 7 Segment display

a guest
Oct 9th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. int pins1[8] = {2,3,4,5,6,7,8}; // array of led pins for digit #1
  2. int pins2[8] = {9,10,11,12,13,A0,A1,A5}; // array of led pins for digit #2
  3.  
  4. // Below is a 2D array of appropriate LEDs for each digit.
  5. // You should set these to correspond to each segment of the display
  6. // In this example the digits are set like this (where "#" is a segment):
  7. // 2
  8. // #
  9. // 7# #3
  10. // #<------8
  11. // 6# #4
  12. // #
  13. // 5
  14. // THE CORRESPONDING PINS MIGHT BE DIFFERENT IN YOUR CASE, either rewire them to match
  15. // this or change the code to match your layout.
  16. //
  17. // In Arduino language 1 == HIGH, 0 == LOW
  18. //
  19. // Corresponds to: 2,3,4,5,6,7,8 for pins1 (9,10,11,12,13,A0,A1,A5 for pins2)
  20. byte digits[10][7] = { { 1,1,1,1,1,1,0 }, // = 0
  21. { 0,1,1,0,0,0,0 }, // = 1
  22. { 1,1,0,1,1,0,1 }, // = 2
  23. { 1,1,1,1,0,0,1 }, // = 3
  24. { 0,1,1,0,0,1,1 }, // = 4
  25. { 1,0,1,1,0,1,1 }, // = 5
  26. { 1,0,1,1,1,1,1 }, // = 6
  27. { 1,1,1,0,0,0,0 }, // = 7
  28. { 1,1,1,1,1,1,1 }, // = 8
  29. { 1,1,1,0,0,1,1 } // = 9
  30. };
  31.  
  32.  
  33. void setup() {
  34. //OTHER CODE HERE
  35.  
  36. // Set LED pins as output
  37. for (int i=0; i<7; i++){
  38. pinMode(pins1[i], OUTPUT);
  39. pinMode(pins2[i], OUTPUT);
  40. }
  41. }
  42. void loop() {
  43. //OTHER CODE HERE
  44.  
  45. displayDigit(23); // as an example call display function to show "23"
  46. }
  47.  
  48.  
  49. displayDigit(int num){
  50. int dig1 = num / 10; // Figure out 1st digit
  51. // i.e.: 23 / 10 = 2.3 = int 2
  52. 3 / 10 = 0.3 = int 0
  53. int dig2 = num % 10; // Figure out 2nd digit. "%" is called "mod", returns the
  54. // remainder of a division operation
  55. // i.e.: 23 % 10 = 3
  56. // 3 % 10 = 3
  57.  
  58. // Increment through both arrays of LEDs and set the appropriate segments
  59. // 1(HIGH) or 0(LOW)
  60. for(int i=0; i<7; i++) {
  61. digitalWrite(pins1[i], digits[dig1][i]);
  62. digitalWrite(pins2[i], digits[dig2][i]);
  63. }
  64. }
  65.  
  66. // This way there is no need for ledLOW() function, no need for all the individual function to // display each digit, consequently there is less things that can go wrong.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement