Advertisement
Braulio777

Arduino 7-Segment LED Die

Nov 17th, 2014
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. //Program for 7-Segment LED Die  
  2. int segmentPins[] = { 2, 3, 4, 5, 6, 7, 8, 9};
  3. int displayPins[] = {10};
  4. int buttonPin = 11;
  5. byte digits[10][8] = {
  6. // a b c d e f g .
  7. { 1, 1, 1, 1, 1, 1, 0, 0}, // 0
  8. { 0, 1, 1, 0, 0, 0, 0, 0}, // 1
  9. { 1, 1, 0, 1, 1, 0, 1, 0}, // 2
  10. { 1, 1, 1, 1, 0, 0, 1, 0}, // 3
  11. { 0, 1, 1, 0, 0, 1, 1, 0}, // 4
  12. { 1, 0, 1, 1, 0, 1, 1, 0}, // 5
  13. { 1, 0, 1, 1, 1, 1, 1, 0}, // 6
  14. { 1, 1, 1, 0, 0, 0, 0, 0}, // 7
  15. { 1, 1, 1, 1, 1, 1, 1, 0}, // 8
  16. { 1, 1, 1, 1, 0, 1, 1, 0} // 9
  17. };
  18. void setup()
  19. {
  20. for (int i=0; i < 8; i++)
  21. {
  22. pinMode(segmentPins[i], OUTPUT);
  23. }
  24. pinMode(displayPins[0], OUTPUT);
  25. pinMode(displayPins[0], OUTPUT);
  26. pinMode(buttonPin, INPUT);
  27. }
  28. void loop()
  29. {
  30. static int dice;
  31. if (digitalRead(buttonPin))
  32. {
  33. dice = random(1,7);
  34.  
  35. }
  36. updateDisplay(dice);
  37. }
  38. void updateDisplay(int value1)
  39. {
  40. digitalWrite(displayPins[0], HIGH);
  41. digitalWrite(displayPins[1], LOW);
  42. setSegments(value1);
  43. delay(5);
  44. digitalWrite(displayPins[0], LOW);
  45. digitalWrite(displayPins[1], HIGH);
  46. delay(5);
  47. }
  48. void setSegments(int n)
  49. {
  50. for (int i=0; i < 8; i++)
  51. {
  52. digitalWrite(segmentPins[i], ! digits[n][i]);
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement