Run and repeat function until press next pushbutton

Hello I am new here. This is my first post :) I try to write sketch. I have three functions: void program1() void program2() void program3()

And three pushbutton

When i push button1 i need to run program1() and REPEAT this program until i press button 2 and run program2().....

Thanks Sorry for my bad english

So what have you tried so far?

run program or print "button1" only once

if (buttonState1 == LOW)
{        
   serial.println("button1"); 
   program1();
}
else if (buttonState2 == LOW)
{
  buttonState2 = LOW;
  serial.println("button2");
  program2();
}

If you want to cycle thru three programs one at a time when a button is pushed, you will probably need to count the button pushes, and reset the count when the third program has been run. Check the current count to know which program to run.

I have 3 button for 3 program.

deanz: I have 3 button for 3 program.

Count as in count the button pushes via code, not count on your fingers and post on the forums.

You'll probably also want to detect signal transitions rather than the switch's state, i.e. when the switch goes from reading LOW to HIGH. Whether or not that's possible depends entirely on what happens inside program1(), program2(), and program3(). More specifically, any delays in those programs won't make it very responsive.

If you want more specific help, you need to post all of your code, and not just snip-its.

I think that the OP wants button A to cause program A to run, button B to cause program B to run and so on.

In which case lets say the 2 "programs" cause 3 different led to flash. So first the term is function not program. On the Arduino there can be only one program.

The OP's first step should be to look at the blink with out delay example, as using buttons with calls to delay does not work very well.

The next thing should be to look at button debouncing.

Third a look a finite state machines (look in the playground on this site). he can then combine things.

After all that the OP can then move on to using 3 buttons.

Mark

I did what you want but with one button by using a variable to store the counts as previously suggested.

int Mode = 0; Mode = (digitalRead, button); if(Mode == HIGH) { Mode ++; } if(Mode == 0) { do something; } // repeat for each function then you will need to reset your counter if(Mode > 2) { Mode = 0; }

and the result was?

Mark

PS use code tags when posting your code.

M

BlackSnake: I did what you want but with one button by using a variable to store the counts as previously suggested.

I don't think the original question is about counting buttons - there are three physical buttons, and each one should be associated with a function; whenever a button is pressed, the corresponding function should be called repeatedly until a different button is pressed.

I'd solve this by using a state variable to record which button was pressed most recently. In loop(), test for a button press and update the state variable if necessary. Then use the state variable to decide which function to call - I'd just use a switch statement.

Is the button press continous, or is it momentary?

i resolve my problem :) thx for help

void loop();

buttonState1 = digitalRead(A1);
buttonState2 = digitalRead(A2);
buttonState3 = digitalRead(A3);

  if (buttonState1 == LOW)
  {
    program1();
  }

  else if (buttonState2 == LOW)
  {
    program1();
  }

  else if (buttonState3 == LOW)
  {
    program3();
  }

}   //end (VOID LOOP)

void program1() { 
   //////something to do
if (buttonState2 == LOW || buttonState3 == LOW )
  {
    return;
  }
program1(); //loop program1
}

void program2() { 
   //////something to do
if (buttonState1 == LOW || buttonState3 == LOW )
  {
    return;
  }
program2(); //loop program2
}

void program3() { 
   //////something to do
if (buttonState1 == LOW || buttonState2 == LOW )
  {
    return;
  }
program3(); //loop program3
}

i resolve my problem

Not with the code you posted. It doesn't even compile.

Now is ok.

#include <SoftwareSerial.h>
SoftwareSerial RFID(7,1);
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int buttonPin1 = A1;      //pushbutton A1
const int buttonPin2 = A2;      //A2
const int buttonPin3 = A3;      //A3
int buttonState1 = 1;
int buttonState2= 1;
int buttonState3= 1;

//433mhz transmiter
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
long randNumber;  //send rundom number

//////////////

void setup() {

  pinMode(buttonPin1, INPUT);
  digitalWrite(buttonPin1,HIGH);
  pinMode(buttonPin2, INPUT);
  digitalWrite(buttonPin2,HIGH);
  pinMode(buttonPin3, INPUT);
  digitalWrite(buttonPin3,HIGH);

  RFID.begin(9600);
  lcd.clear();
  lcd.begin(16, 2);

  // Print a message to the LCD.
  lcd.setCursor(2,0);
  lcd.print("SISTEM O.K. ");
  lcd.setCursor(0,1);
  lcd.print("Ready ");

  //433mhz transmiter
  mySwitch.enableTransmit(A5);      // Transmitter is connected to Arduino Pin #A5 
  // mySwitch.setPulseLength(320);  // Optional set pulse length.
  // mySwitch.setProtocol(2);      // Optional set protocol (default is 1, will work for most outlets)
  mySwitch.setRepeatTransmit(5);  // Optional set number of transmission repetitions org=15.

} //end VOID SETUP
//-------------------------------------------------//



void loop() {

buttonState1 = digitalRead(A1);
buttonState2 = digitalRead(A2);
buttonState3 = digitalRead(A3);

  if (buttonState1 == LOW)
  {
    program1();
  }

  else if (buttonState2 == LOW)
  {
    program1();
  }

  else if (buttonState3 == LOW)
  {
    program3();
  }

}   //end (VOID LOOP)

void program1() { 
   //////something to do
if (buttonState2 == LOW || buttonState3 == LOW )
  {
    return;
  }
program1(); //loop program1
}

void program2() { 
   //////something to do
if (buttonState1 == LOW || buttonState3 == LOW )
  {
    return;
  }
program2(); //loop program2
}

void program3() { 
   //////something to do
if (buttonState1 == LOW || buttonState2 == LOW )
  {
    return;
  }
program3(); //loop program3
}

(press CTRL+T in the IDE to get better indentation)

void program1() { 
    //////something to do
    if (buttonState2 == LOW || buttonState3 == LOW ) {
        return;
    }

    program1(); //loop program1
}

Very bad! You are calling program1() recursively (i.e. from within itself) and the only exit condition is whether someone will press a button. Have you tried to let this thing run without touching any buttons ? I suspect it will continuously reset...

Hint: the proper way to solve this is to have programN() functions coded as state machines, have a global currProgram variable that remembers which program is currently running and having loop() call one of the programN() function based on currProgram value. The other piece of loop() code should detect when one of the buttons changes state, from not pressed to pressed; it then should set currProgram to the number of button just pressed.

deanz:
Now is ok.

#include <SoftwareSerial.h>

SoftwareSerial RFID(7,1);
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int buttonPin1 = A1;      //pushbutton A1
const int buttonPin2 = A2;      //A2
const int buttonPin3 = A3;      //A3
int buttonState1 = 1;
int buttonState2= 1;
int buttonState3= 1;

//433mhz transmiter
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
long randNumber;  //send rundom number

//////////////

void setup() {

pinMode(buttonPin1, INPUT);
 digitalWrite(buttonPin1,HIGH);
 pinMode(buttonPin2, INPUT);
 digitalWrite(buttonPin2,HIGH);
 pinMode(buttonPin3, INPUT);
 digitalWrite(buttonPin3,HIGH);

RFID.begin(9600);
 lcd.clear();
 lcd.begin(16, 2);

// Print a message to the LCD.
 lcd.setCursor(2,0);
 lcd.print("SISTEM O.K. ");
 lcd.setCursor(0,1);
 lcd.print("Ready ");

//433mhz transmiter
 mySwitch.enableTransmit(A5);      // Transmitter is connected to Arduino Pin #A5
 // mySwitch.setPulseLength(320);  // Optional set pulse length.
 // mySwitch.setProtocol(2);      // Optional set protocol (default is 1, will work for most outlets)
 mySwitch.setRepeatTransmit(5);  // Optional set number of transmission repetitions org=15.

} //end VOID SETUP
//-------------------------------------------------//

void loop() {

buttonState1 = digitalRead(A1);
buttonState2 = digitalRead(A2);
buttonState3 = digitalRead(A3);

if (buttonState1 == LOW)
 {
   program1();
 }

else if (buttonState2 == LOW)
 {
   program1();
 }

else if (buttonState3 == LOW)
 {
   program3();
 }

}   //end (VOID LOOP)

void program1() {
  //////something to do
if (buttonState2 == LOW || buttonState3 == LOW )
 {
   return;
 }
program1(); //loop program1
}

void program2() {
  //////something to do
if (buttonState1 == LOW || buttonState3 == LOW )
 {
   return;
 }
program2(); //loop program2
}

void program3() {
  //////something to do
if (buttonState1 == LOW || buttonState2 == LOW )
 {
   return;
 }
program3(); //loop program3
}

Why I am unable to switch to program 2 from program 1 after I press the push button?

jun_2306: Why I am unable to switch to program 2 from program 1 after I press the push button?

Put some Serial.print statements in your program*n() * functions to display some message unique to each one so you can observe what's going on at runtime.