Using push button to go to next void function

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.

Post your program between [code] and [/code] tags, like this:

[code]
 your
  code
   here
[/code]

you can use if like this,or else you can use switch case

int a=A1;
int count =1;
if (a==HIGH)
{
if (count==1)
{
voidtemp1()

}
if(counnt==2)
{
voidtemp2()

}
if(count==3)
{
voidtemp3()
count=1;
}
}

if loop

There is no such thing as an if loop

You could use a library for the debouncing and state change detection.

I like to use GitHub - thomasfredericks/Bounce2: Debouncing library for Arduino and Wiring.

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.

BlinkWithoutDelay here we come

I can give you an example using the mentioned library

#include <Bounce2.h>

Bounce button;

void setup() {
  Serial.begin(250000);
  button.attach(A1, INPUT_PULLUP);
}

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;
    }
  }
}

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.

Whandall:
I can give you an example using the mentioned library

#include <Bounce2.h>

Bounce button;

void setup() {
 Serial.begin(250000);
 button.attach(A1, INPUT_PULLUP);
}

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

here is my code for the temperature

#include <U8glib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire bus(2);
DallasTemperature sensors(&bus);
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST);

float temperature;
char temperatureString[6] = "-";

void draw(void) {
  u8g.setFont(u8g_font_helvB14);
  u8g.drawStr( 0, 15, "Engine:");
  u8g.setFont(u8g_font_fur35n);
  u8g.drawStr( 0, 60, temperatureString);
  u8g.setFont(u8g_font_helvB14);
  u8g.drawStr(95, 60, "\260C");
}

void setup() {
  sensors.begin();
}

void loop() {
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );

  //read temperature
  sensors.requestTemperatures();
  temperature = sensors.getTempCByIndex(0);
  dtostrf(temperature, 2, 0, temperatureString);
  
  delay(1000);
}

here is my code for the temperature

  delay(1000);

Thats only to moderate output.

My example had the 'once a second' part builtin.