Step 10Button and sensor code
I first wrote the code with debouncing for the buttons, but later when I tested my buttons I noticed that I didn't seem to need it, so I removed the debouncing to get less and easier to read code, however I included the function with debouncing in the tab f_comments_etc in the sketch.
Check for button presses:
The code that checks if the buttons are pressed are really simple. It just reads the state of the buttons pins. The arduinos internal pullup resistors are used so the buttons states are HIGH when they are not pressed and LOW when they are pressed.
The touch sensor is read with help of the CapSense library: touch_sensor.capSense(10) this will measure the capacitance 10 times and return an average. Depending on the value returned the snooze_button_state variable will be set to either HIGH or LOW. I have found that when I rest my hand on the lid of the clock i will get a value around 150 and with no hand about 30 ( if I recall correctly). so I used 100 as the limit to change the state of the snooze-"button". Change the value as needed.
______________________________________________________________________
The function that checks for button presses:
void update_buttons_state()
{
hour_button_state = digitalRead(hour_button_pin);
minute_button_state = digitalRead(minute_button_pin);
setting_switch_state = digitalRead(setting_switch_pin);
/* Read the snooze touch sensor: */
if(touch_sensor.capSense(10) > 100) // adjust this value if needed
snooze_button_state = LOW;
else
snooze_button_state = HIGH;
// the if-else above I think could be written shorter as:
// snooze_button_state = (touch_sensor.capSense(10) > 100) ? LOW : HIGH;
}
______________________________________________________________________
Do things if the buttons are pressed:
The settings switch will change the two pointers hours_p and minutes_p to point to either the variables hours and minutes, or alarm_hours and alarm_minutes. The code for the hour and minute buttons will use these pointers, so when the switch changes they will automatically set the right thing; alarm or time. The display function also uses the hours_p and minutes_p pointers so when the settings switch is on alarm, the alarm time is shown.
The hour and minute buttons increase the hours and minutes by one each time they are pressed.
The snooze button will turn off the alarm signal, and set a snooze off time (current time + 10 minutes) when the signal should start again. it also sets the variable snooze_on to true, this will cause the alarm function to start to check for when the snooze of time is.
If the snooze button is held down for 3 seconds it will toggle the alarm off or on. A high pitched tone is played when the alarm is turned on and a low pitched tone is played when the alarm is turned off.
______________________________________________________________________
The buttons function:
// "INTERNAL" VARIABLES FOR BUTTONS FUNCTION:
boolean first_time_hour = true; // these are used to make sure that the hours
boolean first_time_minute = true; // and minutes only is increased once every keypress.
unsigned long snooze_button_timer; // used to keep track of how long the snooze button has
// been held down. when the button has been held down
// a certain amount of time, the alarm will be turned
// of completely.
void buttons()
{
// LOW == button pressed
// HIGH == button released
// (this is because pullup resistors is used)
// Decide if we should set time or alarm:
// (this also makes the display show the alarm time)
if(setting_switch_state==LOW) // LOW = Set time
{
hours_p = &hours;
minutes_p = &minutes;
}
else // HIGH = Set alarm
{
hours_p = &alarm_hours;
minutes_p = &alarm_minutes;
}
// If hour button is pressed, increase hours:
if(hour_button_state==LOW && first_time_hour) // only increase the hours once
{ // every button press.
if(*hours_p < 23)
(*hours_p)++;
else
*hours_p = 0;
first_time_hour = false;
if(DEBUG)
{
Serial.println("hour increase");
Serial.println(hour_button_state);
}
}
else if(hour_button_state==HIGH)
{
first_time_hour = true; // reset when button is released,
} // so that the next press will be registerd.
// If minute button is pressed, increase minutes:
if(minute_button_state==LOW && first_time_minute) // only increase the minutes
{ // once every button press.
if(*minutes_p < 59)
(*minutes_p)++;
else
*minutes_p = 0;
first_time_minute = false;
}
else if(minute_button_state==HIGH)
{
first_time_minute = true; // reset when button is released,
} // so that the next press will be registerd.
if(snooze_button_state==LOW)
{
if(signal_on)
{
// set the time when the alarm signal will start again,
// this will give 10 minutes snooze:
if(minutes<50)
{
snooze_off_minutes = minutes+10;
snooze_off_hours = hours;
}
else
{
snooze_off_minutes = minutes - 50;
snooze_off_hours = hours + 1;
}
snooze_on = true;
signal_on = false;
}
// if the snooze button has been held down for more than 3 seconds turn off/on the alarm
if((millis() - snooze_button_timer) > 3000)
{
if(alarm_on) // if on, turn off
{
signal_on = false;
alarm_on = false;
// play tone so the user know the alarm turned off:
tone_maker.play(NOTE_A3, 100);
}
else if(alarm_on==false) // if off, turn on
{
alarm_on = true;
// play tone so the user know the alarm turned off:
tone_maker.play(NOTE_A7, 100);
}
//reset the snooze button timer
snooze_button_timer=millis();
}
}
else
{
//reset the snooze button timer
snooze_button_timer=millis();
}
}
___________________________________________________________________
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|













































