Calculator from keypad and signle seven segment led

I'm working on an Arduino program that will display the result of any two distinct numbers from 1 to 5 entered via the keypad on a seven-segment display. but I can't seem to figure out how to connect the keypad with a single seven-segment led to display my result.

This is my circuit:


This is my code:

// C++ code
//
#include <Keypad.h>
		// number of the pin
int a= 7; //7
int b= 8; //6
int c= 9; //4
int d= 10; //2
int e= 11; //1
int f= 12; //9
int g= 13; //10

void number0(){
  digitalWrite(a, HIGH); //0
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}
void number1(){
  digitalWrite(a, LOW); //1
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}
void number2(){
  digitalWrite(a, HIGH); //2
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}
void number3(){
  digitalWrite(a, HIGH); //3
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}
void number4(){
  digitalWrite(a, LOW); //4
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
void number5(){
  digitalWrite(a, HIGH); //5
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  delay(1000);
}
void number6(){
  digitalWrite(a, HIGH); //6
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
void number7(){
  digitalWrite(a, HIGH); //7
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}
void number8(){
  digitalWrite(a, HIGH); //8
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
void number9(){
  digitalWrite(a, HIGH); //9
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
void blank(){
  digitalWrite(a, LOW); //blank
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

const byte Rows = 4; //four rows
const byte Cols = 4; //three columns

char keys[Rows][Cols] = {
  {'1','2','3','+'},
  {'4','5','6','-'},
  {'7','8','9','*'},
  {'C','0','#','/'}
};

byte rowPins[Rows] = {6,5,4,3}; //connect to the row pinouts of the keypad
byte colPins[Cols] = {2,1,0,A0}; //connect to the column pinouts of the keypad

Keypad myKeypad = Keypad(makeKeymap(keys),rowPins,colPins,Rows,Cols);

boolean presentValue = false;
boolean next = false;
boolean final = false;
String num1, num2;
int answer;
char op;

void setup()
{
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Enter two distinct numbers from (1 - 5)");  
}

void loop()
{
  char key = myKeypad.getKey();
  if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')){
    if (presentValue != true){
      num1 = num1 + key;
      int numLength = num1.length();
      //
    }
    else 
    {
      num2 = num2 + key;
      int numLength = num2.length();
      //
      final = true;
    }
  }
  else if (final == true && key != NO_KEY && key == '='){
    if (op == '+'){
      answer = num1.toInt() + num2.toInt();
    }
    else if (op == '-'){
      answer = num1.toInt() - num2.toInt();
    }
    else if (op == '*'){
      answer = num1.toInt() * num2.toInt();
    }
    else if (op == '/'){
      answer = num1.toInt() / num2.toInt();
    }
    
  }
  else if (key != NO_KEY && key == 'C'){
    //
    presentValue = false;
    final = false;
    num1 = "";
    num2 = "";
    answer = 0;
    op = ' ';
  }
}

You are using pins 0 and 1 for your keypad but those pins are used by Serial. You can't do both. I would suggest moving those keypad pins to A1 and A2.

The comparison to NO_KEY is not needed there... It will always be true (key != NO_KEY) as well as matching any of the digits.

Thank you
anything else?
also
I still can't figure out how to connect the keypad to seven-digit for example
if I pressed 1 it should display 1 and when I sum it will display the result.
is it possible or am I making it too complicated?

Have you proved that the display works independently of the keypad?

I apologize but can you elaborate more on this, unless you meant if the component work, then that is not the issue since I am using the Tinkercad emulator.

Is this a school assignment?

By the way,

  if (key != NO_KEY and key >= '0' and key <= '9') {

is easier. But, didn't you say 1-5 not 0-9?
acknowledging post #2,

  if (key >= '0' and key <= '9') {

should work.

I still can't figure out how to connect the keypad to seven-digit for example
if I pressed 1 it should display 1 and when I sum it will display the result.

I suppose this is the main problem of the assignment. Usually, the methods that you would need to complete a task like this, would be covered in previous lessons or in the labs or lectures. Did you miss some?

If we solve it for you, would you learn anything? Would it be fair to the other students?

What you call "connecting" should follow the IPO model, input-processing-output. If you think of it that way, in steps, you should be able to do it. Convert input to numeric format, perform the addtion, and send to display. It's all step by step. If you code and test all the steps independently you should have no problem.

Also, please answer all questions that you are asked. Reply #4 is still open.

1 Like

Oh.  I assumed hardware.

Actually uni classwork we just started couple of weeks ago so we are learning by ourself. The professor won't teach us unless you are really close to solution.

But yes I agree with what you said, unfortunately I dont have any materials to go back to only assignments which is different.

Also yes it was required 1-5 wince it's a single seven segment led. i think I got lost in that coding when I searching online.

Because of COVID we have week on/off situation so we are using Tinkercad for this.

I find this simulator somewhat better: https://wokwi.com/

As for your code, can you get a digit to display on the LEDs just as a first step?

I would love to but unfortunately the Tinkercad account under uni, so my professor is watching progress

Wouldn't "making the LEDs work" be considered a kind of progress? :slight_smile:

Okay, what learning resources have you used? There is really a lot online.

For the second time, reply #4 remains unanswered.

Hi,
Have you got code that JUST cycles the display though 0 to 9?
This is to prove your display hardware and software.

Have you got code that JUST reads the keypad?
This is to prove your keypad hardware and software.

Until you can prove each works, FORGET about combining them in one code.

I hope your professor has taught you to program in stages, getting each individual stage working before going onto the next.
THEN.
When you have each code segment working, combine them ONE at a time. Each time getting the combination working before going onto the next.

This procedure will get you to write code with as low a number of bugs as possible.

If he/she hasn't, are they programmers?

Tom... :smiley: :+1: :coffee: :australia:

Your code supports all digits 0-9. Go look.

I don't have any one source, I usually see it online videos. And see other examples

Also I did reply to #4 and he replied back, apologies for forgetting to personal reply.

But thank you for the tip

First one yes

Second one no we kinda jumped through.

My professor didn't teach us anything for Arduino except Big O notation she stopped after c++ and she told us that Arduino will self study and of you're close to solution then she'll help.

She's is an electrical Engineer our original Tutor contract ended and he left uni since he wasn't able to renew it.

But thank you for the tip I'll work on it

Yes as I said, I was lost in a moment and wrote a lot, it was supposed to be results where max is 5+4= 9 with single seven segment

Where? I read all the replies after that and didn't see anything. Actually, the same idea as in #4 is repeated in #10 and #14 with no answer that I can see... unless that is "the tip" that you referred to. It's important because it can help you a lot. Maybe you also meant that with "first one, second one" but it was kind of a vague reference...

If you have already written successful keypad and display code, you should post it.

Reply #5.