| 7 segment LED display by Jose Ojeda Shows how to drive a 7 segment LED display. The display shows the numbers from 0 to 9 over and over. Those displays can be common anode (the segments will be ON by turning the pins LOW) or common cathode (the segments will be ON by turning the pins HIGH). Created 1 May 2007 |

    // 7 segment LED display
    // by Jose Ojeda

    int number = 0;      // variable the keeps number number in the display

    void turn_all_off() {  // function to turn off all the lights
      for(int i=0; i<8; i++) {
        digitalWrite(i, LOW);
      }
    }

    void setup() {
      for(int i=0; i<8; i++) {  // initializes pins 0 to 7 as outputs
        pinMode(i, OUTPUT);
      }
    }

    void loop() {
      turn_all_off(); // turns the display off
      if(number==0) {
        digitalWrite(5, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(0, HIGH);
        digitalWrite(1, HIGH);
        digitalWrite(2, HIGH);
      }
      if(number==1) {
        digitalWrite(2, HIGH);
        digitalWrite(7, HIGH);
      }
      if(number==2) {
        digitalWrite(7, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(0, HIGH);
        digitalWrite(1, HIGH);
      }
      if(number==3) {
        digitalWrite(4, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(1, HIGH);
        digitalWrite(2, HIGH);
      }
      if(number==4) {
        digitalWrite(5, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(2, HIGH);
      }
      if(number==5) {
        digitalWrite(4, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(1, HIGH);
        digitalWrite(2, HIGH);
      }
      if(number==6) {
        digitalWrite(4, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(0, HIGH);
        digitalWrite(1, HIGH);
        digitalWrite(2, HIGH);
      }
      if(number==7) {
        digitalWrite(6, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(2, HIGH);
      }
      if(number==8) {
        digitalWrite(4, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(0, HIGH);
        digitalWrite(1, HIGH);
        digitalWrite(2, HIGH);
      }
      if(number==9) {
        digitalWrite(4, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(1, HIGH);
        digitalWrite(2, HIGH);
      }
      delay(1000); // waits for 1000 milli seconds
      number = number + 1; // increments the variable to show
      if(number > 9) {  // check for the range, if greater then 9 goes back to 0s
        number = 0;
      }
    }