How to loop a case, if i use serial read? Solved

Hello,
I’m after help, I would like to loop a case statement for 5mins than go back and wait for a serial read to start again so that I can choice a different case or same one and than run again for 5mins,i have 3 case I want to run This is my code, Thank you very much for any help in doing this.
/*

The circuit:

  • LCD RS pin to digital pin 12
  • LCD Enable pin to digital pin 11
  • LCD D4 pin to digital pin 5
  • LCD D5 pin to digital pin 4
  • LCD D6 pin to digital pin 3
  • LCD D7 pin to digital pin 2
  • LCD R/W pin to ground
  • 10K resistor:
  • ends to +5V and ground
  • motor control wire Digitalpin 9
  • sensor wire pin A0
    */
    // include the library code:
    #include <LiquidCrystal.h>
    const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
const int inputPin = A0; //inputpin A0 for sensor
const int motor = 9; //ditigal pin9
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int inByte = 0;
void setup() {
// initialize serial communication with computer:
Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(4, 0);
lcd.print(“TRGSML X1”);
delay(2000);
lcd.clear();
lcd.print(“Pegasus”);
lcd.setCursor(0, 1);
lcd.print(“Calibration”);
delay(3000);
// clear the screen
lcd.clear();
// set the display to automatically scroll:
lcd.setCursor(4, 0);
lcd.print(“System”);
lcd.setCursor(1, 1);
lcd.print(“Initialiseing”);
delay(1000);
lcd.clear();
lcd.print(“Waiting for”);
lcd.setCursor(0, 1);
lcd.print(“Command”);

}
void loop()
{
if (Serial.available() > 0) {
int inByte = Serial.read();

switch (inByte){
case ‘a’://motor only
lcd.clear();
lcd.print(“Motor run only”);
analogWrite(motor,0);
delay(5000);
analogWrite(motor,255);
delay(5000);
analogWrite(motor,0);
delay(5000);
break;

case ‘b’://motor and sensor
lcd.clear();
lcd.print(“Full Test”);
delay(2000);
lcd.clear();
lcd.print(“Angle 60 - 120”);
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we’re at the end of the array…
if (index >= numReadings)
// …wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer (as ASCII digits)
average = map(average, 0, 1023, 60, 120);
lcd.setCursor(6, 1); // set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.print(average, DEC);
Serial.println(average, DEC);
delay(500);
average = map(average, 60, 120, 0, 255); //change back for motor
analogWrite(motor,average);

case ‘c’:// test sensor only
lcd.clear();
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we’re at the end of the array…
if (index >= numReadings)
// …wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer (as ASCII digits)
average = map(average, 0, 1023, 60, 120);
lcd.print(“Angle 60 - 120”);
lcd.setCursor(6, 1); // set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.print(average, DEC);
Serial.println(average, DEC);
delay(500);
break;
}
}
}

working_test_runv3.pde (3.9 KB)

What you can do is call the millis() function and use a variable to store the millis value...Now use the condition while(value>300000) //time is 5 mins { your case statement here; } try developing on this line....hope it helped

Thank you so much for your help firenitin :) that worked what you said.