Advertisement
_Black_Panther_

Interfacing 7 Segment Display with Arduino

Sep 27th, 2021 (edited)
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.92 KB | None | 0 0
  1. #define P0 12
  2. #define P1 11
  3. #define P2 10
  4. #define P3 9
  5. #define P4 8
  6. #define P5 7
  7. #define P6 6
  8. #define LED 3
  9.  
  10. #define LIGHTSEG0 digitalWrite(P0, HIGH)
  11. #define LIGHTSEG1 digitalWrite(P1, HIGH)
  12. #define LIGHTSEG2 digitalWrite(P2, HIGH)
  13. #define LIGHTSEG3 digitalWrite(P3, HIGH)
  14. #define LIGHTSEG4 digitalWrite(P4, HIGH)
  15. #define LIGHTSEG5 digitalWrite(P5, HIGH)
  16. #define LIGHTSEG6 digitalWrite(P6, HIGH)
  17.  
  18. #define OFFSEG0 digitalWrite(P0, LOW)
  19. #define OFFSEG1 digitalWrite(P1, LOW)
  20. #define OFFSEG2 digitalWrite(P2, LOW)
  21. #define OFFSEG3 digitalWrite(P3, LOW)
  22. #define OFFSEG4 digitalWrite(P4, LOW)
  23. #define OFFSEG5 digitalWrite(P5, LOW)
  24. #define OFFSEG6 digitalWrite(P6, LOW)
  25.  
  26.  
  27. void printOne(){
  28.   digitalWrite(P1, HIGH);
  29.   digitalWrite(P2, HIGH);
  30.   delay(1000);
  31.   digitalWrite(P1, LOW);
  32.   digitalWrite(P2, LOW);
  33. }
  34.  
  35. void printTwo(){
  36.   digitalWrite(P0, HIGH);
  37.   digitalWrite(P1, HIGH);
  38.   digitalWrite(P6, HIGH);
  39.   digitalWrite(P4, HIGH);
  40.   digitalWrite(P3, HIGH);
  41.   delay(1000);
  42.   digitalWrite(P0, LOW);
  43.   digitalWrite(P1, LOW);
  44.   digitalWrite(P6, LOW);
  45.   digitalWrite(P4, LOW);
  46.   digitalWrite(P3, LOW);
  47. }
  48.  
  49. void printThree(){
  50.   digitalWrite(P0, HIGH);
  51.   digitalWrite(P1, HIGH);
  52.   digitalWrite(P6, HIGH);
  53.   digitalWrite(P2, HIGH);
  54.   digitalWrite(P3, HIGH);
  55.   delay(1000);
  56.   digitalWrite(P0, LOW);
  57.   digitalWrite(P1, LOW);
  58.   digitalWrite(P6, LOW);
  59.   digitalWrite(P2, LOW);
  60.   digitalWrite(P3, LOW);
  61. }
  62.  
  63. void printFour(){
  64.   digitalWrite(P1, HIGH);
  65.   digitalWrite(P5, HIGH);
  66.   digitalWrite(P6, HIGH);
  67.   digitalWrite(P2, HIGH);
  68.   delay(1000);
  69.   digitalWrite(P1, LOW);
  70.   digitalWrite(P5, LOW);
  71.   digitalWrite(P6, LOW);
  72.   digitalWrite(P2, LOW);
  73. }
  74.  
  75. void printFive(){
  76.   digitalWrite(P0, HIGH);
  77.   digitalWrite(P5, HIGH);
  78.   digitalWrite(P6, HIGH);
  79.   digitalWrite(P2, HIGH);
  80.   digitalWrite(P3, HIGH);
  81.   delay(1000);
  82.   digitalWrite(P0, LOW);
  83.   digitalWrite(P5, LOW);
  84.   digitalWrite(P6, LOW);
  85.   digitalWrite(P2, LOW);
  86.   digitalWrite(P3, LOW);
  87. }
  88.  
  89. void printSix(){
  90.   digitalWrite(P0, HIGH);
  91.   digitalWrite(P5, HIGH);
  92.   digitalWrite(P6, HIGH);
  93.   digitalWrite(P2, HIGH);
  94.   digitalWrite(P3, HIGH);
  95.   digitalWrite(P4, HIGH);
  96.   delay(1000);
  97.   digitalWrite(P0, LOW);
  98.   digitalWrite(P5, LOW);
  99.   digitalWrite(P6, LOW);
  100.   digitalWrite(P2, LOW);
  101.   digitalWrite(P3, LOW);
  102.   digitalWrite(P4, LOW);
  103. }
  104.  
  105. void printSeven(){
  106.   LIGHTSEG0;
  107.   LIGHTSEG1;
  108.   LIGHTSEG2;
  109.   delay(1000);
  110.   OFFSEG0;
  111.   OFFSEG1;
  112.   OFFSEG2;
  113. }
  114.  
  115. void printEight(){
  116.   LIGHTSEG0;
  117.   LIGHTSEG1;
  118.   LIGHTSEG2;
  119.   LIGHTSEG3;
  120.   LIGHTSEG4;
  121.   LIGHTSEG5;
  122.   LIGHTSEG6;
  123.   delay(1000);
  124.   OFFSEG0;
  125.   OFFSEG1;
  126.   OFFSEG2;
  127.   OFFSEG3;
  128.   OFFSEG4;
  129.   OFFSEG5;
  130.   OFFSEG6;
  131. }
  132.  
  133.  
  134. void printNine(){
  135.   LIGHTSEG0;
  136.   LIGHTSEG1;
  137.   LIGHTSEG2;
  138.   LIGHTSEG3;
  139.   LIGHTSEG5;
  140.   LIGHTSEG6;
  141.   delay(1000);
  142.   OFFSEG0;
  143.   OFFSEG1;
  144.   OFFSEG2;
  145.   OFFSEG3;
  146.   OFFSEG5;
  147.   OFFSEG6;
  148. }
  149.  
  150. void printZero(){
  151.   LIGHTSEG0;
  152.   LIGHTSEG1;
  153.   LIGHTSEG2;
  154.   LIGHTSEG3;
  155.   LIGHTSEG4;
  156.   LIGHTSEG5;
  157.   delay(1000);
  158.   OFFSEG0;
  159.   OFFSEG1;
  160.   OFFSEG2;
  161.   OFFSEG3;
  162.   OFFSEG4;
  163.   OFFSEG5;
  164. }
  165.  
  166.  
  167. void setup() {
  168.   Serial.begin(9600);
  169.   Serial.println("Initializing Pins...");
  170.   pinMode(P0, OUTPUT);
  171.   pinMode(P1, OUTPUT);
  172.   pinMode(P2, OUTPUT);
  173.   pinMode(P3, OUTPUT);
  174.   pinMode(P4, OUTPUT);
  175.   pinMode(P5, OUTPUT);
  176.   pinMode(P6, OUTPUT);
  177.   pinMode(LED , OUTPUT);
  178.   delay(1000);
  179. }
  180.  
  181. void loop() {
  182.  
  183.   int pot_val = analogRead(A2);
  184.   int mp_val = map(pot_val, 1, 1023, 0, 9);
  185.  
  186.   if(pot_val < 100 || pot_val >800){
  187.     digitalWrite(LED, HIGH);
  188.   }
  189.   else{
  190.     digitalWrite(LED, LOW);
  191.   }
  192.  
  193.   Serial.print("Potentiometer Value: ");
  194.   Serial.println(pot_val);
  195.  
  196.   Serial.print("Mapped Value: ");
  197.   Serial.println(mp_val);
  198.  
  199.   switch(mp_val){
  200.      case 1:
  201.            Serial.println("Printing 1");
  202.            printOne();
  203.            delay(1000);
  204.            break;
  205.      case 2:
  206.            Serial.println("Printing 2");
  207.            printTwo();
  208.            delay(1000);
  209.            break;
  210.      case 3:
  211.            Serial.println("Printing 3");
  212.            printThree();
  213.            delay(1000);
  214.            break;
  215.      case 4:
  216.            Serial.println("Printing 4");
  217.            printFour();
  218.            delay(1000);
  219.            break;
  220.      case 5:
  221.            Serial.println("Printing 5");
  222.            printFive();
  223.            delay(1000);
  224.            break;
  225.      case 6:
  226.            Serial.println("Printing 6");
  227.            printSix();
  228.            delay(1000);
  229.            break;
  230.      case 7:
  231.            Serial.println("Printing 7");
  232.            printSeven();
  233.            delay(1000);
  234.            break;
  235.      case 8:
  236.            Serial.println("Printing 8");
  237.            printEight();
  238.            delay(1000);
  239.            break;
  240.      case 9:
  241.            Serial.println("Printing 9");
  242.            printNine();
  243.            delay(1000);
  244.            break;
  245.      default:
  246.            Serial.println("Printing 0");
  247.            printZero();
  248.            delay(1000);
  249.   }
  250.  
  251.   Serial.println(" ");
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement