Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This code assumes that you have connected the common cathode 7-segment display to the Arduino with the following pin connections:
- - Pin A: Arduino digital pin 2
- - Pin B: Arduino digital pin 3
- - Pin C: Arduino digital pin 4
- - Pin D: Arduino digital pin 5
- - Pin E: Arduino digital pin 6
- - Pin F: Arduino digital pin 7
- - Pin G: Arduino digital pin 8
- 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.
- */
- const int SEG_A = 2;
- const int SEG_B = 3;
- const int SEG_C = 4;
- const int SEG_D = 5;
- const int SEG_E = 6;
- const int SEG_F = 7;
- const int SEG_G = 8;
- void setup() {
- pinMode(SEG_A, OUTPUT);
- pinMode(SEG_B, OUTPUT);
- pinMode(SEG_C, OUTPUT);
- pinMode(SEG_D, OUTPUT);
- pinMode(SEG_E, OUTPUT);
- pinMode(SEG_F, OUTPUT);
- pinMode(SEG_G, OUTPUT);
- }
- void loop() {
- displayNumber(0);
- delay(1000);
- displayNumber(1);
- delay(1000);
- displayNumber(2);
- delay(1000);
- displayNumber(3);
- delay(1000);
- displayNumber(4);
- delay(1000);
- displayNumber(5);
- delay(1000);
- displayNumber(6);
- delay(1000);
- displayNumber(7);
- delay(1000);
- displayNumber(8);
- delay(1000);
- displayNumber(9);
- delay(1000);
- }
- void displayNumber(int num) {
- switch (num) {
- case 0:
- digitalWrite(SEG_A, HIGH);
- digitalWrite(SEG_B, HIGH);
- digitalWrite(SEG_C, HIGH);
- digitalWrite(SEG_D, HIGH);
- digitalWrite(SEG_E, HIGH);
- digitalWrite(SEG_F, HIGH);
- digitalWrite(SEG_G, LOW);
- break;
- case 1:
- digitalWrite(SEG_A, LOW);
- digitalWrite(SEG_B, HIGH);
- digitalWrite(SEG_C, HIGH);
- digitalWrite(SEG_D, LOW);
- digitalWrite(SEG_E, LOW);
- digitalWrite(SEG_F, LOW);
- digitalWrite(SEG_G, LOW);
- break;
- case 2:
- digitalWrite(SEG_A, HIGH);
- digitalWrite(SEG_B, HIGH);
- digitalWrite(SEG_C, LOW);
- digitalWrite(SEG_D, HIGH);
- digitalWrite(SEG_E, HIGH);
- digitalWrite(SEG_F, LOW);
- digitalWrite(SEG_G, HIGH);
- break;
- case 3:
- digitalWrite(SEG_A, HIGH);
- digitalWrite(SEG_B, HIGH);
- digitalWrite(SEG_C, HIGH);
- digitalWrite(SEG_D, HIGH);
- digitalWrite(SEG_E, LOW);
- digitalWrite(SEG_F, LOW);
- digitalWrite(SEG_G, HIGH);
- break;
- case 4:
- digitalWrite(SEG_A, LOW);
- digitalWrite(SEG_B, HIGH);
- digitalWrite(SEG_C, HIGH);
- digitalWrite(SEG_D, LOW);
- digitalWrite(SEG_E, LOW);
- digitalWrite(SEG_F, HIGH);
- digitalWrite(SEG_G, HIGH);
- break;
- case 5:
- digitalWrite(SEG_A, HIGH);
- digitalWrite(SEG_B, LOW);
- digitalWrite(SEG_C, HIGH);
- digitalWrite(SEG_D, HIGH);
- digitalWrite(SEG_E, LOW);
- digitalWrite(SEG_F, HIGH);
- digitalWrite(SEG_G, HIGH);
- break;
- case 6:
- digitalWrite(SEG_A, HIGH);
- digitalWrite(SEG_B, LOW);
- digitalWrite(SEG_C, HIGH);
- digitalWrite(SEG_D, HIGH);
- digitalWrite(SEG_E, HIGH);
- digitalWrite(SEG_F, HIGH);
- digitalWrite(SEG_G, HIGH);
- break;
- case 7:
- digitalWrite(SEG_A, HIGH);
- digitalWrite(SEG_B, HIGH);
- digitalWrite(SEG_C, HIGH);
- digitalWrite(SEG_D, LOW);
- digitalWrite(SEG_E, LOW);
- digitalWrite(SEG_F, LOW);
- digitalWrite(SEG_G, LOW);
- break;
- case 8:
- digitalWrite(SEG_A, HIGH);
- digitalWrite(SEG_B, HIGH);
- digitalWrite(SEG_C, HIGH);
- digitalWrite(SEG_D, HIGH);
- digitalWrite(SEG_E, HIGH);
- digitalWrite(SEG_F, HIGH);
- digitalWrite(SEG_G, HIGH);
- break;
- case 9:
- digitalWrite(SEG_A, HIGH);
- digitalWrite(SEG_B, HIGH);
- digitalWrite(SEG_C, HIGH);
- digitalWrite(SEG_D, HIGH);
- digitalWrite(SEG_E, LOW);
- digitalWrite(SEG_F, HIGH);
- digitalWrite(SEG_G, HIGH);
- break;
- default:
- digitalWrite(SEG_A, LOW);
- digitalWrite(SEG_B, LOW);
- digitalWrite(SEG_C, LOW);
- digitalWrite(SEG_D, LOW);
- digitalWrite(SEG_E, LOW);
- digitalWrite(SEG_F, LOW);
- digitalWrite(SEG_G, LOW);
- break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement