Hello,
I am having trouble with my code. I am trying to utilize a 4x4 keypad and put in the minutes I want for the 7 segment 8 digit countdown clock. The function for the keypad entry is Getkeys(); and the displayNumber8x7segment(byte Justify, unsigned long value); is where I want the value generated by the keypad to be utilized. It looks like the when the value is being used in the displayNumber function it gets garbeled up as can be seedn by the Serial.println(value);
My full Code is attached (rather big), but here are the two functions, both are inserted before the void setup():
//function to display value on 8x7 segment display according to the justify state
void displayNumber8x7segment(byte justify, unsigned long value)
{
Serial.println(value);
byte decimal[8] = {0};
value = value % 100000000; //ensure the value is within 8 digits only
decimal[7] = value / 10000000; //extract digit 7 from value
value = value % 10000000; //extract the rest of 7 digit value
decimal[6] = value / 1000000;
value = value % 1000000;
decimal[5] = value / 100000;
value = value % 100000;
decimal[4] = value / 10000;
value = value % 10000;
decimal[3] = value / 1000;
value = value % 1000;
decimal[2] = value / 100;
value = value % 100;
decimal[1] = value / 10;
decimal[0] = value % 10;
byte zero = 0;
if (justify == RIGHT)
{
for (byte e = 8; e > 0 ; e --)
{
if (zero == 0)
{
if (decimal[e - 1] != 0)
{
display8x7segment(DATA, CLOCK, LATCH, anode[e - 1], cathode[decimal[e - 1]]);
zero = 1;
}
}
else display8x7segment(DATA, CLOCK, LATCH, anode[e - 1], cathode[decimal[e - 1]]);
delay(1);
}
}
else //if justify == left
{
byte d = 0;
for (byte e = 8; e > 0; e --)
{
if (zero == 0)
{
if (decimal[e - 1] != 0)
{
display8x7segment(DATA, CLOCK, LATCH, anode[7], cathode[decimal[e - 1]]);
zero = 1;
d ++;
delay(1);
}
}
else
{
display8x7segment(DATA, CLOCK, LATCH, anode[7 - d], cathode[decimal[e - 1]]);
d ++;
delay(1);
}
}
}
}
//function to get the keys inputed into the keypad
int Getkeys() {
Serial.begin(9600);
while (currentLength < 7)
{
char entered = keypad.getKey();
entered == NO_KEY;
if (entered != NO_KEY)
{
Serial.println(entered);
if ((entered != '*') && (entered != '#'))
{
Time[currentLength] = entered;
currentLength++;
//unsigned long value = Time;
}
}
}
if (currentLength == 7)
{
//unsigned long y = Time;
//Serial.println(entered);
char buffer[7];
buffer[0] = Time[0];
buffer[1] = Time[1];
buffer[2] = Time[2];
buffer[3] = Time[3];
buffer[4] = Time[4];
buffer[5] = Time[5];
buffer[6] = Time[6];
buffer[7] = '\0';
//Serial.println(buffer);
unsigned long value = atoi(buffer);
//unsigned long y = strtoul( buffer, NULL, 8);//converts value to a number
//Serial.println(value);
return value;
}
}
void loop() {
unsigned long value = Getkeys();
Serial.println(value);
displayNumber8x7segment(RIGHT, value);
}
Please copy and paste the Serial output you get which shows that "value" is getting messed up.
Is some of your code missing?
sketch_mar12a:23: error: 'RIGHT' was not declared in this scope
sketch_mar12a:31: error: 'DATA' was not declared in this scope
sketch_mar12a:31: error: 'CLOCK' was not declared in this scope
sketch_mar12a:31: error: 'LATCH' was not declared in this scope
sketch_mar12a:31: error: 'anode' was not declared in this scope
sketch_mar12a:31: error: 'cathode' was not declared in this scope
sketch_mar12a:31: error: 'display8x7segment' was not declared in this scope
sketch_mar12a:70: error: 'currentLength' was not declared in this scope
sketch_mar12a:72: error: 'keypad' was not declared in this scope
sketch_mar12a:73: error: 'NO_KEY' was not declared in this scope
sketch_mar12a:79: error: 'Time' was not declared in this scope
Compare entered to NO_KEY. Discard the result. Why bother?
while (currentLength < 7)
That means that you will be writing into Time[6], which is beyond the end of your array.
if (currentLength == 7)
currentLength can't possibly be anything other than 7, so the test is pointless.
buffer[6] = Time[6];
Time[6] is NOT an element in your array.
buffer[7] = '\0';
buffer[7] is NOT an element in your array.
You don't seem to understand that when you declare an array, you are defining how many elements are in the array, NOT the upper index.
unsigned long value = atoi(buffer);
//unsigned long y = strtoul( buffer, NULL, 8);//converts value to a number
//Serial.println(value);
return value;
There are several problems here. atoi() expects that value in the string to represent an int. Anything larger than an int will not return good results. The fact that you store the result in an unsigned long doesn't matter.
The function returns an int, NOT an unsigned long.
It looks like the when the value is being used in the displayNumber function it gets garbeled up as can be seedn by the Serial.println(value);
With this program I am trying to take 7 entries from a keypad that will be used as my input for the time that I display on my 7 segment displays. The displayumber8x7segment function use an unsigned long value to display the countdown on the 7 segment displays. Does that help you understand why I need a long value?
char Time[7]; // number of characters in our time
int currentLength = 0; //defines which number we are currently writing
int i = 0;
char entered[7];
unsigned long Getkeys() {
Serial.begin(9600);
while (currentLength < 7)
{
char entered = keypad.getKey();
entered == NO_KEY;
if (entered != NO_KEY)
{
Serial.println(entered);
if ((entered != '*') && (entered != '#'))
{
Time[currentLength] = entered;
currentLength++;
//unsigned long value = Time;
}
}
}
if (currentLength == 7)
{
unsigned long y = strtoul( Time, NULL, 8);
}
}
First step is to go to preferences, set Compiler Warnings to ALL, and Verify the sketch. That will point out mistakes that are so blatant that even the compiler can detect them. The numbers after the file name are the line number and character position where the error was detected.
/Users/john/Documents/Downloads/sketch_countdownclock_Remote_KYX3461BS/sketch_countdownclock_Remote_KYX3461BS.ino: In function 'long unsigned int Getkeys()':
/Users/john/Documents/Downloads/sketch_countdownclock_Remote_KYX3461BS/sketch_countdownclock_Remote_KYX3461BS.ino:159:13: warning: statement has no effect [-Wunused-value]
entered == NO_KEY;
^
/Users/john/Documents/Downloads/sketch_countdownclock_Remote_KYX3461BS/sketch_countdownclock_Remote_KYX3461BS.ino:192:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
/Users/john/Documents/Downloads/sketch_countdownclock_Remote_KYX3461BS/sketch_countdownclock_Remote_KYX3461BS.ino: In function 'void loop()':
/Users/john/Documents/Downloads/sketch_countdownclock_Remote_KYX3461BS/sketch_countdownclock_Remote_KYX3461BS.ino:228:15: warning: statement has no effect [-Wunused-value]
for (value; value != -1; value --)
^
/Users/john/Documents/Downloads/sketch_countdownclock_Remote_KYX3461BS/sketch_countdownclock_Remote_KYX3461BS.ino:228:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (value; value != -1; value --)
^
/Users/john/Documents/Downloads/sketch_countdownclock_Remote_KYX3461BS/sketch_countdownclock_Remote_KYX3461BS.ino: In function 'long unsigned int Getkeys()':
/Users/john/Documents/Downloads/sketch_countdownclock_Remote_KYX3461BS/sketch_countdownclock_Remote_KYX3461BS.ino:181:13: warning: array subscript is above array bounds [-Warray-bounds]
buffer[6] = Time[6];
^
/Users/john/Documents/Downloads/sketch_countdownclock_Remote_KYX3461BS/sketch_countdownclock_Remote_KYX3461BS.ino:182:13: warning: array subscript is above array bounds [-Warray-bounds]
buffer[7] = '\0';
^