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 . . .

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.

Instead of using a while loop inside of loop(), make that an if() inside of loop() to do one go-round per loop().
A for-next loop can be replaced with an index variable and code for advancing the index at the end of loop().

Code that does not block runs fast in general.
It lets functions run as tasks, easy to make and add.
You will get lots of support should you want to learn.
It starts with Blink Without Delay in your IDE Examples.

If your button pins are INPUT_PULLUP and the button is to GND, it is no-floating grounded. Button down pin is 0V LOW, button up pin is 5V HIGH.

INPUT_PULLUP moded pins source 5V as through 20K to 50K Ohms resistance, very weak safe 5V from a pin that can be read.

  • Further to Post #18

Hi @GoForSmoke ,

Thank you for commenting on my answer to the OP, but please check the OP so maybe you get where I was pointing:

  • The OP says he has the harware ready.
  • The OP's original code shows he expects a 1 for the pushbuttons to be detected as pressed...
  • The OP doesn't provide the input pins setup... that must obviously INPUT (are there pulldowns provided) or INPUT_PULLDOWN if his MCU provides the resource.

As for myself, I'm pretty done with dealing with raw MPBs signals, as they usually need some treatment before being useful for my projects, so I developed my own library, focusing in what I needed well beyond debouncing and deglithching, ButtonToSwitch library and an ESP32 version, but as I said, it's there to share something I've built with my own needs in mind...

Good Luck!

Gaby.//