While loop looking at 2 push buttons conditions

Hello every one
I am trying to make a while code loop using two push buttons, work OUTSIDE this aera of programme i.e.

void loop() {
// put your main code here, to run repeatedly:

}

basis example:- Thi is only 'looked at once' as an intial setup ready for my main programme

??? can objective programme condition be done or is there some other method of programming code

void SELECT_TARRIF (void)
{
pushB2=digitalRead(pushB2In);
pushB3=digitalRead(pushB3In);
while ((pushB2==0) || (pushB3==0)){ // any push button operation
pushB2=digitalRead(pushB2In);
pushB3=digitalRead(pushB3In);
lcd.setCursor(0, 2);
lcd.print("SINGLE TARIFF = PB2");
lcd.setCursor(0, 3);
lcd.print(" DUAL TARIFF = PB3");
}
pushB2=digitalRead(pushB2In);
if (pushB2==1){
lcd.setCursor(0, 2);
lcd.print(" SINGLE TARIFF ");
lcd.setCursor(0, 3);
lcd.print(" To SELECT use PB4 ");
SINGLE_TARIFF_SELECTED ();
}
pushB3=digitalRead(pushB3In);
if (pushB3==1){
lcd.setCursor(0, 2);
lcd.print(" DUAL TARIFF ");
lcd.setCursor(0, 3);
lcd.print("SET TIMER press PB3");
PUSH_BUTTON_3_RELEASE ();
}
}

void setup() {
// put your setup code here, to run once:

SELECT_TARRIF ():

void loop() {
// put your main code here, to run repeatedly:

// I am now running my main program here

}

Welcome to the forum

If you are asking whether a function can be called from setup() then the answer is yes

However, this is wrong

void setup() {
// put your setup code here, to run once:

SELECT_TARRIF ():

void loop() {
// put your main code here, to run repeatedly:

// I am now running my main program here

}

because the closing brace of setup() is missing

You might want to look at this How to get the best out of this forum before you proceed any further.
We only know what you tell us, and without knowing what you have, and why you want to do this, we don't stand a chance.

It will also tell you how to post code correctly here. Read it an go back and edit the first post and make the correction to the code posting method.

In simple terms: you are looking for a button manager.

it is as you desribe a buton manager beind called up ONCE viab the via the SETUP area of programming code

TO al of your replies Many Thanks Steve

Use a flag variable in the loop() function simply.

this whole programme is being used to to run a solar battery invertor.

the push buttons are to select a Day or night tariff which is only looked at once initialy before the main programme begins.

the HARDWARE SIDE of my project works but I am new to programing mini controllers

Thanks to all

void SELECT_TARRIF (void)
{
pushB2=digitalRead(pushB2In);
pushB3=digitalRead(pushB3In);
while ((pushB2==0) || (pushB3==0)){ // any push button operation
pushB2=digitalRead(pushB2In);
pushB3=digitalRead(pushB3In);
lcd.setCursor(0, 2);
lcd.print("SINGLE TARIFF = PB2");
lcd.setCursor(0, 3);
lcd.print(" DUAL TARIFF = PB3");
}
pushB2=digitalRead(pushB2In);
if (pushB2==1){
lcd.setCursor(0, 2);
lcd.print(" SINGLE TARIFF ");
lcd.setCursor(0, 3);
lcd.print(" To SELECT use PB4 ");
SINGLE_TARIFF_SELECTED ();
}
pushB3=digitalRead(pushB3In);
if (pushB3==1){
lcd.setCursor(0, 2);
lcd.print(" DUAL TARIFF ");
lcd.setCursor(0, 3);
lcd.print("SET TIMER press PB3");
PUSH_BUTTON_3_RELEASE ();
}
}

void setup() {
// put your setup code here, to run once:

SELECT_TARRIF ():

void loop() {
// put your main code here, to run repeatedly:

// I am now running my main program here

}void SELECT_TARRIF (void)
{
// HAS this come as code ???

How are the inputs debounced?

  • In loop(), have a TIMER that runs/expires every 50ms.
  • When the TIMER expires, look at the switches to see if they have changed state.
    If so and the states of the switches meet your requirements, set a flag.
    When the flag is set, do your stuff.
    When the flag is set prevent the checking of your switches . . .
1 Like

Hi @sdh1 ,

Then if you have the hardware side working, remember to include the usual factors of the hardware that might impact in your code:

  • Are the pushbuttons NO (normally open) or NC (normally closed).
  • The use of pull-up or pull-down resistors will affect if your button pushed provides a high or a low voltage level to your pin. And yes, they are a requirement to avoid a "floating value" input.
  • Debouncing process required.

If you have a specific interest in learning the ins and outs of push buttons develop the code for it, if you are not just use a library.
As much as you're using the LCD library, the advanced use of pushbuttons might become tricky enough to rely on a well tested library.

Good Luck!

Gaby.//

Hi, @sdh1
Welcome to the forum.

What model Arduino are you using?

Please post your entire code, what you have posted is not all of it.
Where are the pinMode statements and what pin numbers are pushB2In and pushB3In?

Please post a schematicv of how you have your hardware conected to the controller.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

HI FOLKS ... just to let you all .. Ive mange to make that part of my program work (after 3weeks)

it was the way I wrote the WHILE line then how the If statments lined up

Even lernt break the BREAK stament to exit the while loop

THANK YOU TO ALL
STEVE

Well, since we never saw your problem code, and now we don’t get to see your solved code, just what use is this thread to anyone else? Instead of closing it, I vote the moderators delete it.

Thanks, not!

I don't see code that handles button bounce.

I recommend do not do this. It will be "blocking code" which may cause problems later.

Using while loops in your code is ok provided they execute quickly and do not need to wait for some external event for the while loop to end. If the while loop cannot complete until after a button is pressed or a delay has passed, this is blocking code.