Advertisement
microrobotics

7 Segment Display

Apr 17th, 2023 (edited)
1,802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   This code assumes that you have connected the common cathode 7-segment display to the Arduino with the following pin connections:
  3.   - Pin A: Arduino digital pin 2
  4.   - Pin B: Arduino digital pin 3
  5.   - Pin C: Arduino digital pin 4
  6.   - Pin D: Arduino digital pin 5
  7.   - Pin E: Arduino digital pin 6
  8.   - Pin F: Arduino digital pin 7
  9.   - Pin G: Arduino digital pin 8
  10.  
  11.   Once you have uploaded the code to your Arduino board, the `loop()` function will repeatedly call the `displayNumber()` function with arguments from 0 to 9, causing the 7-segment display to display the numbers in sequence with a delay of 1 second between each number. You can also call the `displayNumber()` function with a specific number as an argument to display that number on the 7-segment display.
  12. */
  13.  
  14. const int SEG_A = 2;
  15. const int SEG_B = 3;
  16. const int SEG_C = 4;
  17. const int SEG_D = 5;
  18. const int SEG_E = 6;
  19. const int SEG_F = 7;
  20. const int SEG_G = 8;
  21.  
  22. void setup() {
  23.   pinMode(SEG_A, OUTPUT);
  24.   pinMode(SEG_B, OUTPUT);
  25.   pinMode(SEG_C, OUTPUT);
  26.   pinMode(SEG_D, OUTPUT);
  27.   pinMode(SEG_E, OUTPUT);
  28.   pinMode(SEG_F, OUTPUT);
  29.   pinMode(SEG_G, OUTPUT);
  30. }
  31.  
  32. void loop() {
  33.   displayNumber(0);
  34.   delay(1000);
  35.  
  36.   displayNumber(1);
  37.   delay(1000);
  38.  
  39.   displayNumber(2);
  40.   delay(1000);
  41.  
  42.   displayNumber(3);
  43.   delay(1000);
  44.  
  45.   displayNumber(4);
  46.   delay(1000);
  47.  
  48.   displayNumber(5);
  49.   delay(1000);
  50.  
  51.   displayNumber(6);
  52.   delay(1000);
  53.  
  54.   displayNumber(7);
  55.   delay(1000);
  56.  
  57.   displayNumber(8);
  58.   delay(1000);
  59.  
  60.   displayNumber(9);
  61.   delay(1000);
  62. }
  63.  
  64. void displayNumber(int num) {
  65.   switch (num) {
  66.     case 0:
  67.       digitalWrite(SEG_A, HIGH);
  68.       digitalWrite(SEG_B, HIGH);
  69.       digitalWrite(SEG_C, HIGH);
  70.       digitalWrite(SEG_D, HIGH);
  71.       digitalWrite(SEG_E, HIGH);
  72.       digitalWrite(SEG_F, HIGH);
  73.       digitalWrite(SEG_G, LOW);
  74.       break;
  75.     case 1:
  76.       digitalWrite(SEG_A, LOW);
  77.       digitalWrite(SEG_B, HIGH);
  78.       digitalWrite(SEG_C, HIGH);
  79.       digitalWrite(SEG_D, LOW);
  80.       digitalWrite(SEG_E, LOW);
  81.       digitalWrite(SEG_F, LOW);
  82.       digitalWrite(SEG_G, LOW);
  83.       break;
  84.     case 2:
  85.       digitalWrite(SEG_A, HIGH);
  86.       digitalWrite(SEG_B, HIGH);
  87.       digitalWrite(SEG_C, LOW);
  88.       digitalWrite(SEG_D, HIGH);
  89.       digitalWrite(SEG_E, HIGH);
  90.       digitalWrite(SEG_F, LOW);
  91.       digitalWrite(SEG_G, HIGH);
  92.       break;
  93.     case 3:
  94.       digitalWrite(SEG_A, HIGH);
  95.       digitalWrite(SEG_B, HIGH);
  96.       digitalWrite(SEG_C, HIGH);
  97.       digitalWrite(SEG_D, HIGH);
  98.       digitalWrite(SEG_E, LOW);
  99.       digitalWrite(SEG_F, LOW);
  100.       digitalWrite(SEG_G, HIGH);
  101.       break;
  102.     case 4:
  103.       digitalWrite(SEG_A, LOW);
  104.       digitalWrite(SEG_B, HIGH);
  105.       digitalWrite(SEG_C, HIGH);
  106.       digitalWrite(SEG_D, LOW);
  107.       digitalWrite(SEG_E, LOW);
  108.       digitalWrite(SEG_F, HIGH);
  109.       digitalWrite(SEG_G, HIGH);
  110.       break;
  111.     case 5:
  112.       digitalWrite(SEG_A, HIGH);
  113.       digitalWrite(SEG_B, LOW);
  114.       digitalWrite(SEG_C, HIGH);
  115.       digitalWrite(SEG_D, HIGH);
  116.       digitalWrite(SEG_E, LOW);
  117.       digitalWrite(SEG_F, HIGH);
  118.       digitalWrite(SEG_G, HIGH);
  119.       break;
  120.     case 6:
  121.       digitalWrite(SEG_A, HIGH);
  122.       digitalWrite(SEG_B, LOW);
  123.       digitalWrite(SEG_C, HIGH);
  124.       digitalWrite(SEG_D, HIGH);
  125.       digitalWrite(SEG_E, HIGH);
  126.       digitalWrite(SEG_F, HIGH);
  127.       digitalWrite(SEG_G, HIGH);
  128.       break;
  129.     case 7:
  130.       digitalWrite(SEG_A, HIGH);
  131.       digitalWrite(SEG_B, HIGH);
  132.       digitalWrite(SEG_C, HIGH);
  133.       digitalWrite(SEG_D, LOW);
  134.       digitalWrite(SEG_E, LOW);
  135.       digitalWrite(SEG_F, LOW);
  136.       digitalWrite(SEG_G, LOW);
  137.       break;
  138.     case 8:
  139.       digitalWrite(SEG_A, HIGH);
  140.       digitalWrite(SEG_B, HIGH);
  141.       digitalWrite(SEG_C, HIGH);
  142.       digitalWrite(SEG_D, HIGH);
  143.       digitalWrite(SEG_E, HIGH);
  144.       digitalWrite(SEG_F, HIGH);
  145.       digitalWrite(SEG_G, HIGH);
  146.       break;
  147.     case 9:
  148.       digitalWrite(SEG_A, HIGH);
  149.       digitalWrite(SEG_B, HIGH);
  150.       digitalWrite(SEG_C, HIGH);
  151.       digitalWrite(SEG_D, HIGH);
  152.       digitalWrite(SEG_E, LOW);
  153.       digitalWrite(SEG_F, HIGH);
  154.       digitalWrite(SEG_G, HIGH);
  155.       break;
  156.     default:
  157.       digitalWrite(SEG_A, LOW);
  158.       digitalWrite(SEG_B, LOW);
  159.       digitalWrite(SEG_C, LOW);
  160.       digitalWrite(SEG_D, LOW);
  161.       digitalWrite(SEG_E, LOW);
  162.       digitalWrite(SEG_F, LOW);
  163.       digitalWrite(SEG_G, LOW);
  164.       break;
  165.  
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement