Condition to run void loop from void setup

Hey there,
I am trying to exicute a project in which, I want to send numbers to multiple display.
I am using if else condition to select the display.
Now i want to exit the void loop and only enter in it if i enter something in serial monitor.

I tried to put everything in void setup but i cant send data to screens multiple times.

Welcome to the forum

Why not detect the user input in loop() and if it meets your criteria then run whatever code you want as many times and as often as you want ?

By trying to do everything in setup(), possibly multiple times, you are simply reinventing what the loop() function does

Perhaps if you gave a more concrete example of what you want to achieve it would help to clarify things

Hey there.. welcome to the forum.

Your question is hard to understand.

If English is not your first language try Google Translate.

Maybe ...

void loop()
{
  if (Serial.available() > 0)
  {
    // do the thing
  }
}

Hey there,
Thank you very much,

I want to control the numbers on the tft display via mobile or computer.
I am using ESP32 and i want to use bluetooth for wireless control.
I belive I am not able to send commands from my phone because sketch is looping.

Here is a sketch without bluetooth part.

#include <SPI.h>
#include <TFT_eSPI.h> // Hardware-specific library

#include "logo.c"
#define CS1 33
#define CS2 25
#define CS3 26

float Price1;
float Price2;
float Price3;
String msg0="Enter product name";
String msg="Enter price: ";
int serinput = 0; // for incoming serail date

TFT_eSPI tft = TFT_eSPI(); // Invoke custom library

void setup() {
Serial.begin(9600);

pinMode(CS1, OUTPUT);
digitalWrite(CS1, HIGH);

pinMode(CS2, OUTPUT);
digitalWrite(CS2, HIGH);

pinMode(CS3, OUTPUT);
digitalWrite(CS3, HIGH);

int x = 0, y = 0, w = 128, h = 128;

digitalWrite(CS1, LOW);
digitalWrite(CS2, LOW);
digitalWrite(CS3, LOW);

tft.init();
tft.setSwapBytes (true);
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.pushImage (0,0,128,128,logo);
tft.setTextSize(3);

digitalWrite(CS1, HIGH);
digitalWrite(CS2, HIGH);
digitalWrite(CS3, HIGH);

Serial.println(msg0);

}

void loop() {
Serial.println(msg0);
while (Serial.available() == 0 ) {

}
if (Serial.available() ) {
serinput = Serial.read();
if ((char)serinput == 'A' ) {
Serial.println(msg);
Serial.setTimeout(10000);
Price1=Serial.parseFloat();
digitalWrite(CS1, LOW);
tft.setRotation(0);
tft.setCursor(12,127);
tft.print(Price1);
digitalWrite(CS1, HIGH);
Serial.println(Price1);
Serial.println("Done");
}

      else if ((char)serinput == 'B' ) {
        Serial.println(msg);
        Serial.setTimeout(10000);
        Price2=Serial.parseFloat();
        digitalWrite(CS2, LOW);
        tft.setRotation(0);
        tft.setCursor(12,127);
        tft.print(Price2);
        digitalWrite(CS2, HIGH);
        Serial.println(Price2);
        Serial.println("Done");            
                                        }

      else if ((char)serinput == 'C' ) {
        Serial.println(msg);
        Serial.setTimeout(10000);
        Price3=Serial.parseFloat();
        digitalWrite(CS3, LOW);
        tft.setRotation(0);
        tft.setCursor(12,127);
        tft.print(Price3);
        digitalWrite(CS3, HIGH);
        Serial.println(Price3);
        Serial.println("Done");
                                      }

      else ((char)serinput == 0); {

        return;
                                  }   
  }

}

Hey there,
Thank you very much.
Maybe I need to rephrase my question but tell me which part you did not understand.

Hi, @shubham1chauhan
Welcome to the forum.

This should show you how to post your code.

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

consider this approach


float Price1;
String msg0="Enter product name";
String msg="Enter price: ";

void setup() {
    Serial.begin(9600);

    Serial.println(msg0);
}

void loop()
{
    if (! Serial.available())
        return;

    switch (Serial.read ())  {
    case 'A':
        Serial.println(msg);
        Serial.setTimeout(10000);
        Price1=Serial.parseFloat();
        // ...
        break;

    case 'B':
        // ...
        break;

    case 'C':
        // ...
        break;
    }

    Serial.println(msg0);
}

You can't, at least not that way. The "loop()" function (pls omit "void", it's the returned data type, non part of the function name...) is exactly what the name means: a piece of code continuously and automatically executed in loop, while "setup()" only once at startup. It's like a "main" program contains:

void main() {
  setup();
  while(1)
    loop();
}

So, run loop() from setup() doesn't make a great sense as it doesn't have any real advantage over the standard usage.
@red_car has shown what you could do to run some code only if something comes from the serial port, but it's just an embryo, you need to do a bit more coding inside (or better describe your requirement).

Hey there,

Thank you for your input. I was able to solve the issue.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.