Hey everybody! I'm new to the forum, new to Arduino, and new to programming in general.
I am working on a project where I would like to mimic the functionality of an oven timer as part of a larger project. I want it to increment by 1 second when pressed once, and when the button is held, continue increment at an increasing rate, until the timer reaches a multiple of 10, and then count by 10s. IE: 16,17,18,19,20, 30, 40, 50 etc.
I have figured out how to do the one shot button press, and how to have the counter continue to count up at an increasing rate when the button is held, but I am at a loss of how to do the change to counting by 10s.
My initial thoughts are to do something with changing the data type for my SetTime varible to a float, then divide SetTime by 10 to get a decimal number, then use the int() conversion function to truncate SetTime to a multiple of 10, multiply SetTime by 10 to get back to the correct value, then change the increment to 10 instead of 1.
My concerns with this are that it will always round downward, since the int() function always truncates downward. IE:: 169 seconds, divided by 10 is 16.9, which will truncate to 16, and will multiply back to 160, not 170.
Is there another, better way to do this? I would like to add the counting by 10s function to my existing time change subroutine, as it is working very well to count by increments of 1.
Varible Definitions:
int SpeedUp = A4; // Speed(+) Button Pin Location
int SpdUp = 0; // variable for Speed(+) pin status
int SpeedDown = A1; // Speed(-) Button Pin Location
int SpdDown = 0; // variable for Speed(-) pin status
int TimeUp = A5; // Time(+) Button Pin Location
int TUp = 0; // varible for Time(+) pin status
int TimeDown = A2; // Time(-) Button Pin Location
int TDown = 0; // varible for Time(-) pin statusunsigned long CT = 0; // Current Time (millisecs) - Current Clock Time, continuous running
unsigned long ST = 0; // Saved Time (millisecs) - clock time saved for next increment (Time buttons)
unsigned long PT = 0; // Pulse Time Varible (millisecs) - time between increments on button hold (Time buttons)
unsigned long ST2 = 0; // Saved Time (millisecs) - clock time saved for next increment (Speed buttons)
unsigned long PT2 = 0; // Pulse Time Varible (millisecs) - time between increments on button hold (Speed buttons)
int MinP = 25; // Minimum Pulse Time (millisecs) - minimum time between increments on button hold
int MaxP = 300; // Maximum Pulse Time (millisecs) - maximum time between increments on button hold
const float Ramp = .9; // Pulse Time Accel %
int MaxTime = 240; // Max Run Time for machine (seconds)int RunState = 0; // Machine Running State for FSM use: 0=on 1=ready 2=running 3=paused 4=completed 5=canceled 6=diagnostic
//EEprom memory usage
int SpeedState = EEPROM.read(11); // read SpeedState stored in memory from before reset
int SetTime = EEPROM.read(10); // read SetTime stored in memory from before reset - SetTime is time set via Time+ and Time- buttons
Time Change Subroutine Code:
void TimeChange()
{
TUp = digitalRead(TimeUp); // read input value
TDown = digitalRead(TimeDown); // read input value
if (TUp == HIGH || TDown == HIGH) { // check if either input is HIGH (either button is pressed)
if (TUp == HIGH && SetTime < MaxTime) { // check if input is HIGH (button is pressed)
if (CT > ST){ // check if current time is higher than the time for next increment
SetTime++; // increment one
tone(Spkr,Freq1,ShortBeep); // Play short button press tone
ST = CT + PT; // set time for next increment to current time plus pulse time
if (PT > MinP){ // check that pulse time is above minium pulse time
PT = PT * Ramp; // decrease pulse time by Ramp% to decrease pulse time
}
}
}
if (TDown == HIGH && SetTime > 1) { // check if input is HIGH (button is pressed)
if (CT > ST){ // check if current time is higher than the time for next increment
SetTime--; // decrement one
tone(Spkr,Freq1,ShortBeep); // Play short button press tone
ST = CT + PT; // set time for next increment to current time plus pulse time
if (PT > MinP){ // check that pulse time is above minium pulse time
PT = PT * Ramp; // decrease pulse time by % to decrease pulse time
}
}
}
}
else {
ST = CT; // set time to next increment equal to current time
PT = MaxP; // reset pulse time to max pulse time
}
if (SetTime >= MaxTime){ // do not count higher than max run time
SetTime = MaxTime;
}
if (SetTime <= 1){ // set time cannot be less than 0
SetTime = 1;
}
if (SetTime != EEPROM.read(10)){ // if SetTime is different that stored value of SetTime
EEPROM.write(10, SetTime); // then write current SetTime to EEPROM
}
DispTime = SetTime; // time for display = to the set time
}
Any and all advice is greatly appreciated. Thanks!
TJ