I am trying to make a programmable timer. I have more about it here: http://forum.arduino.cc/index.php?topic=247174.msg1766691#msg1766691
Now I have it all put together and stuff. But my program isn’t working correctly. What it should do that it isn’t is one: it should see if I press the # button on a keypad. I do, it doesn’t. Upon doing this, it is supposed to reset the timer to the default 0300 seconds.
Next, it should tell if I press the * button. It does. But it should also let me insert a new time in when I do. It don’t. I have added some extra code to see where the problems lie. It is acting like it’s scanning the buttons as it should, but it’s not registering them.
I’ve traced a few bugs back to the multiplexer I have; it was disabling it when it wasn’t supposed to. I have it set to enable the multiplexer just before the initial scan, then disable it afterward. I tried to see if it was disabling it. It wasn’t. So I tried to move the part of the code that tests for # to before the part that tests *. Pressing # still does nothing. But pressing # will execute part of the code that resets the time (it beeps three times) before executing the correct code (beeps again) and still failing. In the following code, you can see where I commented out the code that tests for #.
// define numbers, buttons
const int numbers[] = {0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b10110110,0b00111110,0b11100000,0b11111110,0b11100110}; // 0-9
const int buttons[] = {7,0,4,8,1,5,9,2,6,10,3,11}; //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, *, #
// set the enable pins on both shift registers
const int en1 = 10; // set low when sending data, display
const int en2 = A2; // same as above, Mux
// these are the digits on the display
const int a1 = 2;
const int a2 = 3;
const int a3 = 4;
const int a4 = 5;
//data, clock, buttons, mux enable
const int sda = 7;
const int sck = 8;
const int sig = A0;
const int me = A1;
//set the digits time
int a = 0; //a-d are defualt time
int b = 3;
int c = 0;
int d = 0;
int e = 0; //temporary storage
int f = 0; // ^
int g = 0; // ^^
int h = 0; // ^^^
int done = 0; //^^^^
//to remember which button we're on
int button;
//buzzer
const int buzzer = 13;
//We'll need these for timing
unsigned long now; //for the millis() function
unsigned long wait; //for the fail system
int seconds;
void setup(){
pinMode(en1,OUTPUT); //enable 1
pinMode(en2,OUTPUT); //enable 2
pinMode(me,OUTPUT); //Mux Enable
pinMode(a1,OUTPUT); //digit one
pinMode(a2,OUTPUT); //digit two
pinMode(a3,OUTPUT); //digit three
pinMode(a4,OUTPUT); //digit four
pinMode(sda,OUTPUT); //data out
pinMode(sck,OUTPUT); //clock
pinMode(sig,INPUT); //button read
pinMode(buzzer,OUTPUT); //buzzer
digitalWrite(buzzer,0); //make sure buzzer is off initially
digitalWrite(en1,1); //stop any random write cycle to the registers
digitalWrite(en2,1);
delay(10); //just here to compensate for propegation; can be removed
digitalWrite(en1,0); //write all 0's to both registers
digitalWrite(en2,0);
shiftOut(sda,sck,LSBFIRST,0);
digitalWrite(en1,1);
digitalWrite(en2,1);
digitalWrite(me,0); //disable the mux
}
void loop(){
now=millis();
while(millis() - now < 1000){ //while one second has not passed, strobe the time to the display
digitalWrite(en1,0);
shiftOut(sda,sck,LSBFIRST,numbers[a]);
digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
digitalWrite(en1,1);
digitalWrite(a1,0);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
//delay(1);
digitalWrite(en1,0);
shiftOut(sda,sck,LSBFIRST,numbers[b]);
digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
digitalWrite(en1,1);
digitalWrite(a1,1);digitalWrite(a2,0);digitalWrite(a3,1);digitalWrite(a4,1);
//delay(1);
digitalWrite(en1,0);
shiftOut(sda,sck,LSBFIRST,numbers[c]);
digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
digitalWrite(en1,1);
digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,0);digitalWrite(a4,1);
//delay(1);
digitalWrite(en1,0);
shiftOut(sda,sck,LSBFIRST,numbers[d]);
digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
digitalWrite(en1,1);
digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,0);
//delay(1);
//end of the strobing
digitalWrite(me,1); //enable the mux
button_read(); //test for a button press
digitalWrite(me,0); //disable the mux
}
// subtract a second
seconds = (a*1000)+(b*100)+(c*10)+d; //combine all digits to one number
if(seconds>0){ //is there still time left
seconds--; //if so, subtract one
a=seconds/1000; //the rest of the code puts the number back to four digits
b=seconds/100 - (a*10);
c=seconds/10 - (a*100) - (b*10);
d=seconds - (a*1000) - (b*100) - (c*10);
}
}
void button_read(){
/* digitalWrite(en2,0);
shiftOut(sda,sck,LSBFIRST,11); //test the # button
digitalWrite(en2,1);
if(digitalRead(sig)==1){ //if # was pressed, reset timer to 0300 seconds
for(int q=0;q<4;q++){
digitalWrite(buzzer,1);
delay(100);
digitalWrite(buzzer,0);
delay(100);
}
a=0;
b=3;
c=0;
d=0;
}*/
digitalWrite(en2,0);
shiftOut(sda,sck,LSBFIRST,3); //look for * button
digitalWrite(en2,1);
if(digitalRead(sig)==1){ //was the * pressed
digitalWrite(buzzer,1);
delay(250);
digitalWrite(buzzer,0);
while(digitalRead(sig)==1){
//wait
}
e=buttonn();
if(e != 30){ //30 is the "cancel" code
f=buttonn();
if(f!=30){
g=buttonn();
if(g!=30){
h=buttonn();
if(h!=30){
a=e; //a, b, c, d are the individual digits in the number
b=f; //we are taking our temorary number and making it permanent
c=g;
d=h;
}
}
}
}
}
}
int buttonn(){
wait = millis();
while(millis() - wait < 10000){ //keep scanning buttons for no longer than 10 seconds
for(button=0;button<12;button++){ //all buttons are read on a Mux controlled by a shift regiter
digitalWrite(en2,0); //enable pin for shift register
shiftOut(sda,sck,LSBFIRST,buttons[button]); //the buttons[] array allows to scan the buttons in order (0-9, *, #)
digitalWrite(en2,1);
if(digitalRead(sig)==1){ //if there's a hit
digitalWrite(buzzer,1); //sound the buzzer
delay(25);
digitalWrite(buzzer,0);
while(digitalRead(sig)==1){ //we don't want to fill all four variables with the same number by holding one button down
//wait
}
if(button==10||button==11){ //these are my "cancel" buttons
return 30; //exit code
}
else{
return button; //pressed number
}
}
}
}
return 30; //if 10 seconds passed
}
I can tell it’s executing the function button_read() correctly and that it’s nesting buttonn() correctly because after 10 seconds, it fails like it should and contenues the countdown. This is killing me.