Problem with Serial inputs to control

I’m using visual studio and or the idea serial input for commands to start different light sequences in my program everything works with a it remote but when I command it with a serial input instead of the ir remote it keeps looking through running the same sequence over and over instead of running it once and stopping like it dose with the ir remote could any one help me with this

if (Serial.available())
 {
 val = Serial.readString();
 }
 if (val == "IG")
 {
 instgreen = true;     // signal to stage start pin
 }
 if (val == "PT")
 {
 protree = true;
 }
 if (val == "ST")
 {
 sportsmantree = true;
 }

 if (irrecv.decode(&results))
 {
 if (results.value == 0xFF22DD)        // ir play button
 {
 instgreen = true;     // signal to stage start pin
 }
 if (results.value == 0xFF02FD)
 {
 protree = true;
 }
 if (results.value == 0xFFC23D)
 {
 sportsmantree = true;
 }
 irrecv.resume();
 }
if (instgreen == true)  // start tree signal from ir start pin
 {
 digitalWrite(stagelPin, LOW);          //left stage light on by ir
 digitalWrite(stagerPin, LOW);          //right stage light on by ir
 instgreen = false;
 delay(random(minDelay, maxDelay));
 timerStartState = true;
 }
 if (protree == true)  // start tree signal from ir start pin
 {
 digitalWrite(stagelPin, LOW);          //left stage light on by ir
 digitalWrite(stagerPin, LOW);          //right stage light on by ir
 protree = false;
 delay(2000);
 digitalWrite(yellow1Pin, LOW);
 digitalWrite(yellow2Pin, LOW);
 digitalWrite(yellow3Pin, LOW);
 delay(400);
 digitalWrite(yellow1Pin, HIGH);
 digitalWrite(yellow2Pin, HIGH);
 digitalWrite(yellow3Pin, HIGH);
 timerStartState = true;
 }
 if (sportsmantree == true)  // start tree signal from ir start pin
 {
 digitalWrite(stagelPin, LOW);          //left stage light on by ir
 digitalWrite(stagerPin, LOW);          //right stage light on by ir
 sportsmantree = false;
 delay(2000);
 digitalWrite(yellow1Pin, HIGH);
 Serial.println("1...");
 delay(500);
 digitalWrite(yellow1Pin, LOW);
 digitalWrite(yellow2Pin, HIGH);
 Serial.println("2...");
 delay(500);
 digitalWrite(yellow2Pin, LOW);
 digitalWrite(yellow3Pin, HIGH);
 Serial.println("3...");
 delay(500);
 digitalWrite(yellow3Pin, LOW);
 delay(500);
 timerStartState = true;
 }

3_Drag_Trees_and_Timing_comp_conrtol.ino (13.4 KB)

Without your code, I doubt anybody can help you.

i added some of the code the ir part works fine the serial just keeps looping and running the light sequence over and over

Have you tried clearing val after you're done with it or in the beginning of loop()? If val is a global variable so if you don't receive anything, it will still be remembered.

Note:
nobody except you knows if val is a global variable; that's why you should post complete code or a representative example.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

i have added a attachment to first post

Mikeh23:
i have added a attachment to first post

Why?

First, why did you add the attachment - what does it contain?

Second, why did you add it in the Original Post instead of in your Reply #5 so we would not have to go back to the beginning?

...R

Every one was saying they needed to she the whole code but it was to big to post in the message that's why I had only posted what I was having problems with to start with

Good, so val is a global variable and hence it is remembered. Clear it in the beginning of loop() before the first if statement.

And try to stay away from String (capital S); heavy use of String will eventually result in unexpected problems at run time.

I’m not quite sure how I read over all the serial stuff and also tried to set it to 0 but I’d wouldn’t comply

void loop()
{
  val = "";
  ...
  ...
}

Ok thanks I'll try that