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