A 7 Segment LED includes 7 pieces to display numbers and letters, in this project we will be controlling one with a Infrared sensor, to display numbers and even do math.

We need to connect our IR receiver again. Review the instructions here

Once you have your IR receiver connected, connect the 7 segment LED to the breadboard, Each pin corresponds to a display on the LED, 2 pins on each side will correspond to ground. Once you connect the ground and output pins, create a layout of your 7segment LED and Key codes for easy access

Then, define each pin of the 7 segment LED, and a pin for the IR receiver.


#include <IRremote.h>
//this program will only show 2-digit numbers

//Layout of the 7Segment LED. There is also another segment for a period which is not being used

//     - 6
// 5 |    | 7
//     - 4
// 3 |    | 1
//     - 2

/* CH-   0xFFA25D
   CH  0xFF629D
   CH+   0xFFE21D
   <<  0xFF22DD
   >>  0xFF02FD
   >||   0xFFC23D
   –   0xFFE01F
   +   0xFFA857
   EQ  0xFF906F
   100+  0xFF9867
   200+  0xFFB04F
   0   0XFF6897
   1   0xFF30CF
   2   0xFF18E7
   3   0xFF7A85
   4   0xFF10EF
   5   0xFF38C7
   6   0xFF5AA5
   7   0xFF42BD
   8   0xFF4AB5
   9   0xFF52AD
*/


const int LED1 = 3;
const int LED2 = 4;
const int LED3 = 5;
const int LED4 = 6;
const int LED5 = 8;
const int LED6 = 9;
const int LED7 = 10;

const int RECV_PIN = 7;
decode_results results;
IRrecv irrecv(RECV_PIN);



Next, lets go to setup and setup our LED and IR receiver, set the LED segments to output and enable the IR receiver, your setup code should look like this.


void setup() {
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  irrecv.enableIRIn(); //start the IR receiver
}

Once you finish the setup, lets create functions for displaying things on the LED, first lets create a function to turn off all segments on the LED, which will be useful later when refreshing numbers.


void turn_off_segments() {
  //turn off all the segments on the led
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
}

Next, lets create a function to display numbers passed through it, in order display the value given from the remote to the receiver.


void displayNumb(int numb) {
  //displays the number passed on the led.
  if (numb == 0) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
  }
  if (numb == 1) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED7, HIGH);
  }

  if (numb == 2) {
    turn_off_segments();
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED2, HIGH);
  }

  if (numb == 3) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
  }

  if (numb == 4) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED7, HIGH);
  }

  if (numb == 5) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED4, HIGH);
  }

  if (numb == 6) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED4, HIGH);
  }

  if (numb == 7) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
  }

  if (numb == 8) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
  }

  if (numb == 9) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
  }

}

Once you do that, you can create keys for each button pressed to display it on the LED, but lets start with the math first, lets create a function that will store the values of 2 numbers, in order to add or subtract. Lets also create a counter that will be useful later on.


int number_one = 0;
int number_two = 0;
int counter = 0;

void changeNumberValue(int cnumb) {
  number_one = number_two;
  number_two = cnumb;
}

We are defining the integers before the function is declared because in order for it to be defined in that scope.

Now, we need a function that will display the number, change the counter, and change the number value we just defined.


void displayCounter(int numb) {
  displayNumb(numb); // display the value
  counter = (counter + 1); //change the counter
  changeNumberValue(numb); //set either number_one or number_two value based on the counter
}

Now, we will define the addition and subtraction later, lets start on making the buttons display the number passed, this needs to be done in the loop.


void loop() {
  if (irrecv.decode(&results)) { //call to function decode

    if (results.value == 0XFF6897) { //Keypad button "0"
      displayCounter(0);
    }
  }
}

Now do this for each keypad button 0-9


void loop() {
  if (irrecv.decode(&results)) { //call to function decode

    if (results.value == 0XFF6897) { //Keypad button "0"
      displayCounter(0);
    }

    if (results.value == 0xFF30CF) { //Keypad button "1"
      displayCounter(1);
    }

    if (results.value == 0xFF18E7) { //Keypad button "2"
      displayCounter(2);
    }

    if (results.value == 0xFF7A85) { //Keypad button "3"
      displayCounter(3);
    }

    if (results.value == 0xFF10EF) { //Keypad button "4"
      displayCounter(4);
    }

    if (results.value == 0xFF38C7) { //Keypad button "5"
      displayCounter(5);
    }

    if (results.value == 0xFF5AA5) { //Keypad button "6"
      displayCounter(6);
    }

    if (results.value == 0xFF42BD) { //Keypad button "7"
      displayCounter(7);
    }

    if (results.value == 0xFF4AB5) { //Keypad button "8"
      displayCounter(8);
    }

    if (results.value == 0xFF52AD) { //Keypad button "9"
      displayCounter(9);
    }
    Serial.print(number_one);
    Serial.print(number_two);
}

You can also include serial prints in order to help decode the program

Now, lets start on the addition and subtraction, first, lets define the buttons to either add or subtract(we can define one, then mirror it afterwards)


if (results.value == 0xFFA857) {
   int number_value = (int(number_one) + int(number_two));
}

Now lets create a function that can display all values given from the addition or subtraction.


void displayAllNumb(int numbv) {
  //display double digit numbers
  if (numbv < 9) {
    displayNumb(int(numbv / 10));
    delay(250);
    turn_off_segments();
    delay(250);
    displayNumb((numbv % 10));
  }
  //display double digit negative numbers
  if (numbv > 0) {
    if ((numbv * -1)  < 9) {
      turn_off_segments();
      delay(250);
      digitalWrite(LED4, HIGH);
      delay(250);
      displayNumb(int((numbv * -1)) / 10);
      delay(250);
      turn_off_segments();
      delay(250);
      displayNumb(((numbv * -1) % 10));
    }
    else {
      //display one digit negative numbers
      turn_off_segments();
      delay(250);
      digitalWrite(LED4, HIGH);
      delay(250);
      turn_off_segments();
      delay(250);
      displayNumb((numbv * -1));
    }

  }
  else {
    //display one digit positive number
    displayNumb(numbv);
  }
}

With, this function created we can finish defining our addition function.


//Keypad button "+"
if (results.value == 0xFFA857) {
      if (counter > 1) {
        int number_value = (int(number_one) + int(number_two));
        displayAllNumb(number_value);
        number_two = number_value; //changes the first number to answer value
        counter = 1; //changes counter to one so that a second number will need to be input
      }
      else {
        Serial.print("Does not work");
      }
    }

To explain this code, we are making sure the counter is greater than 1 so that the code is not adding where enough numbers were not input, which is either too early when 2 numbers have not been pressed yet, or just after addition, where the program wants another number to be input. The value of the addition is saved in number_two, so that when a second number is input, the value is saved and you can now add on to the number given.

Now, you can change the button code and number_value from adding to subtracting in order to do subtraction.


//Keypad button "-"
if (results.value == 0xFFE01F) {
      if (counter > 1) {
        int number_value = (int(number_one) - int(number_two));
        displayAllNumb(number_value);
        number_two = number_value; //changes the first number to answer value
        counter = 1; //changes counter to one so that a second number will need to be input
      }
      else {
        Serial.print("Does not work");
      }
    }

The final code should look somewhat like this

#include <IRremote.h>
//this program will only show 2-digit numbers

//Layout of the 7Segment LED. There is also another segment for a period which is not being used
//     - 6
// 5 |    | 7
//     - 4
// 3 |    | 1
//     - 2

/* CH-   0xFFA25D
   CH  0xFF629D
   CH+   0xFFE21D
   <<  0xFF22DD
   >>  0xFF02FD
   >||   0xFFC23D
   –   0xFFE01F
   +   0xFFA857
   EQ  0xFF906F
   100+  0xFF9867
   200+  0xFFB04F
   0   0XFF6897
   1   0xFF30CF
   2   0xFF18E7
   3   0xFF7A85
   4   0xFF10EF
   5   0xFF38C7
   6   0xFF5AA5
   7   0xFF42BD
   8   0xFF4AB5
   9   0xFF52AD
*/


const int LED1 = 3;
const int LED2 = 4;
const int LED3 = 5;
const int LED4 = 6;
const int LED5 = 8;
const int LED6 = 9;
const int LED7 = 10;

const int RECV_PIN = 7;
decode_results results;
IRrecv irrecv(RECV_PIN);


void setup() {
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  irrecv.enableIRIn(); //start the IR receiver

}

void turn_off_segments() {
  //turn off all the segments on the led
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
}



void displayNumb(int numb) {
  //displays the number passed on the led.
  if (numb == 0) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
  }
  if (numb == 1) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED7, HIGH);
  }

  if (numb == 2) {
    turn_off_segments();
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED2, HIGH);
  }

  if (numb == 3) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
  }

  if (numb == 4) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED7, HIGH);
  }

  if (numb == 5) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED4, HIGH);
  }

  if (numb == 6) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED4, HIGH);
  }

  if (numb == 7) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
  }

  if (numb == 8) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
  }

  if (numb == 9) {
    turn_off_segments();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
  }

}


int number_one = 0;
int number_two = 0;
int counter = 0;

void changeNumberValue(int cnumb) {
  number_one = number_two;
  number_two = cnumb;
}



void displayCounter(int numb) {
  displayNumb(numb); // display the value
  counter = (counter + 1); //change the counter
  changeNumberValue(numb); //set either number_one or number_two value based on the counter
}



void displayAllNumb(int numbv) {
  //display double digit numbers
  if (numbv > 9) {
    displayNumb(int(numbv / 10));
    delay(250);
    turn_off_segments();
    delay(250);
    displayNumb((numbv % 10));
  }
  //display double digit negative numbers
  if (numbv < 0) {
    if ((numbv * -1) > 9) {
      turn_off_segments();
      delay(250);
      digitalWrite(LED4, HIGH);
      delay(250);
      displayNumb(int((numbv * -1)) / 10);
      delay(250);
      turn_off_segments();
      delay(250);
      displayNumb(((numbv * -1) % 10));
    }
    else {
      //display one digit negative numbers
      turn_off_segments();
      delay(250);
      digitalWrite(LED4, HIGH);
      delay(250);
      turn_off_segments();
      delay(250);
      displayNumb((numbv * -1));
    }

  }
  else {
    //display one digit positive number
    displayNumb(numbv);
  }
}




void loop() {
  if (irrecv.decode(&results)) { //call to function decode

    if (results.value == 0XFF6897) { //Keypad button "0"
      displayCounter(0);
    }

    if (results.value == 0xFF30CF) { //Keypad button "1"
      displayCounter(1);
    }

    if (results.value == 0xFF18E7) { //Keypad button "2"
      displayCounter(2);
    }

    if (results.value == 0xFF7A85) { //Keypad button "3"
      displayCounter(3);
    }

    if (results.value == 0xFF10EF) { //Keypad button "4"
      displayCounter(4);
    }

    if (results.value == 0xFF38C7) { //Keypad button "5"
      displayCounter(5);
    }

    if (results.value == 0xFF5AA5) { //Keypad button "6"
      displayCounter(6);
    }

    if (results.value == 0xFF42BD) { //Keypad button "7"
      displayCounter(7);
    }

    if (results.value == 0xFF4AB5) { //Keypad button "8"
      displayCounter(8);
    }

    if (results.value == 0xFF52AD) { //Keypad button "9"
      displayCounter(9);
    }
    Serial.print(number_one);
    Serial.print(number_two);


    //Keypad button "+"
    if (results.value == 0xFFA857) {
      if (counter > 1) {
        int number_value = (int(number_one) + int(number_two));
        displayAllNumb(number_value);
        number_two = number_value; //changes the first number to answer value
        counter = 1; //changes counter to one so that a second number will need to be input
      }
      else {
        Serial.print("Does not work");
      }
    }

    //Keypad button "-"
    if (results.value == 0xFFE01F) {
      if (counter > 1) {
        int number_value = (int(number_one) - int(number_two));
        displayAllNumb(number_value);
        number_two = number_value; //changes the first number to answer value
        counter = 1; //changes counter to one so that a second number will need to be input
      }
      else {
        Serial.print("Does not work");
      }
    }

    Serial.print(number_one);
    Serial.print(number_two);
    irrecv.resume();
  }

}

Categories: Arduino