Hi all!
I'd like to get some help with my Arduino programing.(im using arduino mini pro)
I'd like to make arduino go to my next function when a button pressed.
i have 3 function in my code for room temperature sensors.
void temp1() //temperature sensor 1
void temp2() //temperature sensor 2
void temp3() //temperature sensor 3
i'm using only one button connected to the Analoginput(A1) and the other go directly to the ground. so when i start the arduino it will go to void temp1(), and when i pressed the button it will go to void temp2() and when i pressed it again it will go to void temp3() and so on if i pressed it again it will go to void temp1() again
Can anybody help me how to make the code go to next function ?
Thanks a lot!
Look at the StateChangeDetection example in the IDE to see how to detect when a button becomes pressed. Use that to increment a counter and run your functions based on the counter value.
Whatever solution is adopted, unless the functions take little time to run then moving to the next one after a button press may be more of a problem than it appears as first.
void loop() {
static byte selectFunction = 1;
static unsigned long lastAction;
unsigned long topLoop = millis();
if (button.update()) {
if (button.fell()) {
if (++selectFunction >= 4) {
selectFunction = 1;
}
lastAction = 0; // this starts the selected function directly
switch (selectFunction) {
case 1:
Serial.println(F("select temp1"));
break;
case 2:
Serial.println(F("select temp2"));
break;
case 3:
Serial.println(F("select temp3"));
break;
}
}
}
if (topLoop - lastAction >= 1000) {
lastAction = topLoop;
switch (selectFunction) {
case 1:
Serial.println(F("execute temp1"));
break;
case 2:
Serial.println(F("execute temp2"));
break;
case 3:
Serial.println(F("execute temp3"));
break;
}
}
}
UKHeliBob:
All very well, but what if case 1 was
case 1:
for (int x = 0; x < 255; x++)
{
//do something using the value of x
delay(100);
}
break;
Let's see what the OP wants to do in the functions.
hey thanks for the response guys !
i will try using the bounce2 library first. I will let you know if there is any progress in my code.
oh, and one another question about the push button.
i connected the push button to the ground(GND) and to analoginput1(A1) am i doing it right ?
because when i do a research in google i found out that some people using a 10k resistor for their button