Carrying out more than 1 task in a void loop

Hi I’m currently working on a project where I am trying to get 3 force resistor sensors, to show their input results at the same time. I’ve tried to get this to work with two first, this has been done using a void loop and a while statement. However this will only show 1 fsr result then show the rest from the other sensor. I want to get it to continually show both sensors result. Below is the code I am working with.

//Declaring resistors
//Resistor 1
int fsrPin = 0; // Resistor connectected to A0
int fsrReading; // Anolog reading from resistor 1
//Resistor 2
int fsrPin1 = 1; // Resistor connectected to A1
int fsrReading1; // Anolog reading from resistor 2
//Resistor 3
int fsrPin2 = 2; // Resistor connectected to A2
int fsrReading2; // Anolog reading from resistor 3

void setup(void) {
// debugging information sent to the serial monitor
Serial.begin(9600);
Serial.println(“Testing left right and centre pressure sesnors”);
}

//Loop the sequence, so that results are always showing
void loop (void) {
fsrReading = analogRead(fsrPin);//analog reading for resistor 1 = seting to sensor 1

Serial.print("Sensor 1 = "); //what will be displayed before the result
Serial.print(fsrReading); // the raw analog reading

//determinations of pressure
if (fsrReading < 10) {
Serial.println(" - No pressure");
//under 10 = no pressure

} else if (fsrReading < 200) {
Serial.println(" - Faint Pressure");
//11 to under 200 = faint pressure

} else if (fsrReading < 500) {
Serial.println(" - Light pressure");
//201 to under 500 = Light pressure

} else if (fsrReading < 800) {
Serial.println(" - Medium pressure");
//501 to under 800 = Medium pressure

} else {
Serial.println(" - Large pressure");
//above 800 = large pressure
}

delay(1000);//time delay between each result

while(1){//while loop should read sensor 2 while looping sesnor 1
fsrReading1 = analogRead(fsrPin1);

Serial.print(“Sensor 2 = “);
Serial.print(fsrReading1); // the raw analog reading
//determinations of pressure
if (fsrReading1 < 10) {
Serial.println(” - No pressure”);
//under 10 = no pressure

} else if (fsrReading1 < 200) {
Serial.println(" - Faint pressure");
//11 to under 200 = faint pressure

} else if (fsrReading1 < 500) {
Serial.println(" - Light pressure");
//201 to under 500 = Light pressure

} else if (fsrReading1 < 800) {
Serial.println(" - Medium pressure");
//501 to under 800 = Medium pressure
} else {
Serial.println(" - Large pressure");
//above 800 = large pressure
}
delay(1000);//time delayed between new readings

}
}

Not compiled, not tested.

const byte fsrPins [3] = {A0, A1, A2};

 
void setup(void) 
{
  Serial.begin(9600);
  Serial.println(F("Testing left right and centre pressure sensors"));
}

 
void loop (void) 
{
  for (int i = 0; i < sizeof (fsrPins) / sizeof (fsrPins [0]); i++)
  {
    int fsrReading = analogRead(fsrPins [i]);
 
    Serial.print(F("Sensor "));
    Serial.print (i);
    Serial.print(F(" = "));
    Serial.print(fsrReading);     // the raw analog reading
 
    //determinations of pressure
    if (fsrReading < 10) {
      Serial.println(F(" - No pressure"));

    } else if (fsrReading < 200) {
      Serial.println(F(" - Faint Pressure"));
   
    } else if (fsrReading < 500) {
    Serial.println(F(" - Light pressure"));
   
    } else if (fsrReading < 800) {
    Serial.println(F(" - Medium pressure"));
   
    } else {
      Serial.println(F(" - Large pressure"));
    }
    delay(1000);//time delay between each result
  }
}

Again, uncompiled and untested, but slightly better factored.

const byte fsrPins [3] = {A0, A1, A2};
 
void setup(void) 
{
  Serial.begin(9600);
  Serial.println(F("Testing left right and centre pressure sensors"));
}
 
void loop (void) 
{
  for (int i = 0; i < sizeof (fsrPins) / sizeof (fsrPins [0]); i++)
  {
    int fsrReading = analogRead(fsrPins [i]);
 
    Serial.print(F("Sensor "));
    Serial.print (i);
    Serial.print(F(" = "));
    Serial.print(fsrReading);     // the raw analog reading
 
    Serial.println(F(" - "));
    if (fsrReading < 10) {
      Serial.println(F("No"));

    } else if (fsrReading < 200) {
      Serial.println(F("Faint"));
   
    } else if (fsrReading < 500) {
    Serial.println(F("Light"));
   
    } else if (fsrReading < 800) {
    Serial.println(F("Medium"));
   
    } else {
      Serial.println(F("Large"));
    }
    Serial.println(F(" pressure"));
    delay(1000);//time delay between each result
  }
}

And also, please remember to use code tags when posting code.

Would it make sense to perform the sensor reads in one "While" loop into an array of fsrReadings, and the "Serial.prints" in a second "While" loop reading from the array of fsrReadings? That would provide more closely-spaced readings relative to each other (may or may not be a requirement depending on how fast the forces are changing) and possibly make the code easier to follow.

Not clear if the "show" in "show their input results at the same time" means to "read" at the same time or "print" at the same time, but this approach would tend to do both.

Perhaps the demo Several Things at a Time illustrates the sort of thing you need.

...R

rickso234:
Would it make sense to perform the sensor reads in one "While" loop into an array of fsrReadings, and the "Serial.prints" in a second "While" loop reading from the array of fsrReadings? That would provide more closely-spaced readings relative to each other (may or may not be a requirement depending on how fast the forces are changing) and possibly make the code easier to follow.

Not clear if the "show" in "show their input results at the same time" means to "read" at the same time or "print" at the same time, but this approach would tend to do both.

With multiple tasks in loop() you want to avoid any task from taking "long" (my rule of thumb for AVR's, 200 microseconds is "long".) in any one pass through loop(). An analog read takes about 105. For some perspective, delay(1) wastes 16000 cpu cycles (1000 micros) that could run a lot of processing.

Instead of a task doing everything in one pass through loop(), do a little in many passes. If you keep the per-loop() load light you can get over 50,000 loop()s per second, the work gets done a little slower but the other tasks get to run on millisecond time every time and it's all smoooooth. As long as you keep loop() running fast and you have code space, you can add Yet Another Task.

If some task only needs to run once per second, you give it a time check so it only runs then but does not hold the other tasks up like delay(1000) does. Most of what needs to be done only needs doing once in a while anyway, serial chars at 115200 baud arrive 1388 cycles apart. Don't waste cycles and you will be amazed at all you can make happen.

Robin's link/thread will show you how to achieve that. So will the tutorial at the address of the first link in my signature space below and no I'm not Nick, I just like his very complete, commonsense simple lessons of which there are many.

Using delay() or other "wait for" code

You're getting lots of excellent advice on how to improve your code, but no one has yet addressed what is wrong with the code you've written.

The first problem is the while loop. it isn't looping sensor 1, it's looping sensor 2 forever.
Get rid of it you don't need it.

The second problem is the serial monitor itself. it wont let you format how things are printed there is only Serial.print and Serial.println.

The only way to get what I think you want is to use Serial.print for everything but the last print on the last sensor then use Serial.println for that last print. So it looks like this:

Sensor 1 = 111 - Faint Pressure, Sensor 2 = 555 - Medium pressure, Sensor 3 = 999 - Large pressure.

put a delay() only after the last sensor.

That will print one line with the info for all 3 sensors once a second.

As others have pointed out there are better ways to do this, but this will get your code working