Hey guys, I'm new here and also new to programming. I was hoping someone with more experience can tell me where I'm going wrong.
The program is for an mBED LPC1768 microcontroller and is to take an input from a keypad and output the result to a 7 segment display. The keypad and 7 segment are correctly wired to the microcontroller on a breadboard as far as I know.
I'm pretty sure my DisplayNumber function for the 7 segment is OK as I have tested it using button presses to increment and decrement the number on screen and it works. The problem is with my keypad function. I don't know if this type of keypad is overly common but it is a grid where the 3 columns are put high individually and if a button is pressed it puts the corresponding row high, and you can tell which button has been pushed.
When I compile the code I get a "return value type does not match the function type" error in my keypad function.
Any help would be much appreciated as I have an assessment in uni next week and need to know how to send and return values from functions.
Code below:
#include "mbed.h"
//Program to take input from a keypad display numbers and symbols on the 7 segment display when corresponding keys on keypad are pressed. Using polling.
BusOut LED_Disp(p7,p11,p9,p8,p5,p6,p10,p12); //set up segment pins for Busout
DigitalOut Col1(p26); //create col1 object on pin 26 to put 1st column high
DigitalOut Col2(p28); //create col2 object on pin 26 to put 2nd column high
DigitalOut Col3(p24); //create col3 object on pin 26 to put 3rd column high
int Num0,Num1,Num2,Num3,Num4,Num5,Num6,Num7,Num8,Num9,Num10,Num11,Numblank; //create variables to store the hex for each Display digit
int num; //create integer to store current number value that changes when buttons are pressed
void DisplayNumber(int num)
{
switch(num)
{
case 0:
LED_Disp = Num0; // Bit pattern for '0’
break;
case 1:
LED_Disp = Num1; // Bit pattern for '1’
break;
case 2:
LED_Disp = Num2; // Bit pattern for '2’
break;
case 3:
LED_Disp = Num3; // Bit pattern for '3’
break;
case 4:
LED_Disp = Num4; // Bit pattern for '4’
break;
case 5:
LED_Disp = Num5; // Bit pattern for '5’
break;
case 6:
LED_Disp = Num6; // Bit pattern for '6’
break;
case 7:
LED_Disp = Num7; // Bit pattern for '7’
break;
case 8:
LED_Disp = Num8; // Bit pattern for '8’
break;
case 9:
LED_Disp = Num9; // Bit pattern for '9’
break;
case 10:
LED_Disp = Num10; // Bit pattern for '*’
break;
case 11:
LED_Disp = Num11; // Bit pattern for '#’
break;
case 12:
LED_Disp = Numblank; // Bit pattern for 'blank’
break;
if (Row1 == 1) // if row 1 output high
{
return(1); // output corresponding value for num integer
}
if (Row2 == 1) // if row 2 output high
{
return(4);
}
if (Row3 == 1) // if row 3 output high
{
return(7);
}
if (Row4 == 1) // if row 3 output high
{
return(10);
}
Col1 == 0;
Col2 == 1;
Col3 == 0;
if (Row1 == 1) // if row 1 output high
{
return(2);
}
if (Row2 == 1) // if row 2 output high
{
return(5);
}
if (Row3 == 1) // if row 3 output high
{
return(8);
}
if (Row4 == 1) // if row 3 output high
{
return(0);
}
Col1 == 0;
Col2 == 0;
Col3 == 1;
if (Row1 == 1) // if row 1 output high
{
return(3);
}
if (Row2 == 1) // if row 2 output high
{
return(6);
}
if (Row3 == 1) // if row 3 output high
{
return(9);
}
if (Row4 == 1) // if row 3 output high
{
return(11);
}
DisplayNumber(num);
if (num = 0,1,2,3,4,5,6,7,8,9,10,11)
break; //break out of loop if a button is pressed
}
}
int main()
{
Num0=0x40;
Num1=0x79;
Num2=0x24;
Num3=0x30;
Num4=0x19;
Num5=0x12;
Num6=0x02;
Num7=0x78;
Num8=0x00;
Num9=0x18;
Num10=0x9c;
Num11=0xa3;
Numblank=0xff; //define the hex value for each display digit
num = 12;
DisplayNumber(num); //sets display at start as blank (case 12 = 0xff)
while(1) // infinite loop to check for input and display
{
reply=keypad(num); //call keypad function to check for input
Please use code tags next time. Reading an unformatted piece of code is no fun.
You declare keypad() as void keypad(int num), but inside the function you return numbers in several places and you use the return value here: reply=keypad(num);. Presumably what you really wanted was for keypad() to return an int.
My apologies, This is my first post and I messed up the formatting. Thank for the reply, Yes I want keypad to return an integer (num) to be passed to the DisplayNumber function, which outputs to the 7 segment display.
I'm still confused to be honest, I need to know how to return in the keypad function a value to be assigned to to the integer num, which is then passed to DisplayNumber.
Sorry if I'm a bit slow, I'm new to this.