Issues with using serial.avaliable to start a loop

Hi guys, I'm having an issue where I want to use serial.avaliable to print the state of the system and then loop every two seconds.

Currently, when 's' is typed, there is a delay of two seconds but it does not loop every seconds but rather needs another 's' input in order to print again.

#define State1             0
#define State2             1
#define State3             2
#define State4             3
#define State5             4
#define State6             5
int state                = 0;
int serialState          = 1;
int counter              = 0;
unsigned long previousTime;
bool oldButtonState;
bool runStates;
char recentChar = 's';


const int redPeda        = 11;
const int greenPeda      = 6;
const int potent         = A3;
const int photoresist    = A0;
const int red            = 9;
const int green          = 10;
const int buttonPIN      = 12;
const int Streetlight    = A1;

int redstate           = 0;
int greenstate         = 0;
int redPedastate       = 0;
int greenPedastate     = 0;
int yellowstate        = 0;

int brightness;           
int potentValue;
int sensorValue;


int segAtoGpinout [7] = {5,4,3,2,7,13,8}; //A-G excluding DP led 


                          //{a,b,c,d,e,f,g}
int seg_numbers [10][7] =  {{1,1,1,1,1,1,0}, //0
                            {0,1,1,0,0,0,0}, //1
                            {1,1,0,1,1,0,1}, //2
                            {1,1,1,1,0,0,1}, //3                                       
                            {0,1,1,0,0,1,1}, //4
                            {1,0,1,1,0,1,1}, //5
                            {1,0,1,1,1,1,1}, //6
                            {1,1,1,0,0,0,0}, //7
                            {1,1,1,1,1,1,1}, //8
                            {1,1,1,0,0,1,1}};//9
  
void numWrite(int);
void WritetoSSD(int *);

void setup()
  
{
  previousTime = millis();
  for (int i=0; i < 7 ;i++) {
  pinMode(segAtoGpinout[i], OUTPUT);
  }
  
  pinMode(buttonPIN, INPUT_PULLUP);
  pinMode(redPeda, OUTPUT);
  pinMode(greenPeda, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(Streetlight, OUTPUT);
  pinMode(photoresist, INPUT);
  Serial.begin(9600);
   redstate           = 0;
   greenstate         = 1;
   yellowstate        = 0;
   redPedastate       = 1;
   greenPedastate     = 0;
}

void loop()
{
  unsigned long currentTime = millis();
  digitalWrite(greenPeda, LOW);
  potentValue = analogRead(potent); //read and store value from potentiometer
  sensorValue = analogRead(photoresist);
  brightness = map(potentValue, 0, 1023, 0, 255);
  analogWrite(Streetlight, sensorValue);
  analogWrite(red, redstate*brightness);
  analogWrite(green, greenstate*brightness);
  analogWrite(redPeda, redPedastate*brightness);
  analogWrite(greenPeda, greenPedastate*brightness);
  
   if (Serial.available() > 0){
   char recentChar = Serial.read();
   if (recentChar == 's')
   {
    {
      if (currentTime - previousTime >= 2000)
      {
        previousTime = currentTime;
        SerialMonitor();        
      }
     }
    }
   }

  bool buttonState = digitalRead(buttonPIN);
   if (oldButtonState != buttonState)
      {
        oldButtonState = buttonState;
        if (buttonState == LOW)
        {
          SerialMonitor();
          if(!runStates){
          	previousTime = millis();
          	runStates = true;
          }
       }
      }
  
  if (runStates){
    changeLights();
  }
}

void SerialMonitor()
{
 Serial.print("Potentiometer LED Brightness: ");
 Serial.println(brightness);
 Serial.print("Traffic: Red: ");
 Serial.print(redstate);
 Serial.print(", Green: ");
 Serial.print(greenstate);
 Serial.print(", Yellow: ");
 Serial.println(yellowstate);
 Serial.print("Pedestrian: Red: ");
 Serial.print(redPedastate);
 Serial.print(", Green:");
 Serial.println(greenPedastate);
 Serial.print("Streetlights: ");
 Serial.println(sensorValue);
 Serial.print("Button: Pressed ");
}

When you receive the 's' set a boolean to true and only print when the boolean is true

A tip for you:

                          //{a,b,c,d,e,f,g}
int seg_numbers [10][7] =  {{1,1,1,1,1,1,0}, //0
                            {0,1,1,0,0,0,0}, //1
                            {1,1,0,1,1,0,1}, //2
                            {1,1,1,1,0,0,1}, //3                                       
                            {0,1,1,0,0,1,1}, //4
                            {1,0,1,1,0,1,1}, //5
                            {1,0,1,1,1,1,1}, //6
                            {1,1,1,0,0,0,0}, //7
                            {1,1,1,1,1,1,1}, //8
                            {1,1,1,0,0,1,1}};//9

This uses 140 bytes of precious ram memory. It only needs to use 10 bytes!

                          //abcdefg
byte seg_numbers [10] =  {0b1111110, //0
                          0b0110000, //1
                          ...
                          ...
                          0b1110011};//9

To access the segment values, instead of "seg_numbers[d][s]" use "bitRead(seg_numbers[d], s)"

Hi Bob, thanks for the suggestion, I've tried to implement this into the code, but I don't think I understand when to state the bool as true. Can you give me an example if you don't mind?

Thanks for the suggestion Paul, thats a nice tip.

Post the code that you tried

if (Serial.available() > 0){
   char recentChar = Serial.read();
   if (recentChar == 's')
   {
     bool example = true;
    {
      if (currentTime - previousTime >= 2000 && example = true)
      {
        previousTime = currentTime;
        SerialMonitor();        
      }
     }
    }
   }

Try this - UNTESTED

bool printingOK = false;

void loop()
{
  currentTime = millis();
  if (Serial.available() > 0)
  {
    char recentChar = Serial.read();
    if (recentChar == 's' && !printingOK)
    {
      printingOK = true;
      previousPrintTime = currentTime;
    }
  }
  if (currentTime - previousPrintTime >= 2000 && printingOK == true)
  {
    SerialMonitor();
    previousPrintTime = currentTime;
  }
}

That works.
Thanks Bob.

That's good

Note the subtle changes to the names of the variables to make the code more readable

If you want the loop() function to do nothing until you enter the 's' then you could just put the "wait for 's' code" in setup()

I noticed those changes, will keep that in mind for future projects. I think I'll leave it how it is, thanks for the suggestion.