LED is stuck on until I restart serial monitor

I am reading an analog input (A0) to check for a trigger from an audio signal. trigger is a square pulse within a wav file being played out of my laptop. when the trigger is sensed by the analog input, I have an LED blink pattern out of pin 11.

I set up a serial buffer to print the value of A0 and the blink pattern interval (100 ms, 200 ms, etc). I have it successfully sense the trigger and go through the blink pattern but once it is finished, the LED stays on and the serial output prints -2560 and stops printing anything else. Ive seen it give a positive number also with some other configuration of my code. If i close the serial monitor and re-open it, the LED turns off and A0 pretty much starts sensing again because if I play the trigger, it will go into the LED blink pattern again.

Any ideas why the serial monitor is controlling the script like this?

Also my ultimate goal is to go through the LED blink routine and get ready for another trigger to set off another LED blink routine… So need help resetting the A0 pin.

Heres my code:

const int triggerPin = A0;     // the number of the trigger pin
const int ledPinR =  11;      // the number of the LED pin
const int ledPinY =  10;      // the number of the LED pin
int triggerState = 0;
int intervals[] = {100,200,300,400,300,200,100};
int triggerCheck = 1;
void setup() {
  // start serial comm
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPinR, OUTPUT);
  //digitalWrite(ledPinR, LOW);
}

void loop() {
  if (triggerCheck==1){ // check if trigger was triggered yet
    triggerState = analogRead(triggerPin);    
    Serial.println(triggerState);
    // check if the trigger is pressed.
    if (triggerState > 1) {
      triggerCheck = 0;      
    }
  }
  else{
    for (int x = 0; x < sizeof(intervals); x++){
      Serial.println(intervals[x]);
      digitalWrite(ledPinR,HIGH);
      delay(intervals[x]);
      digitalWrite(ledPinR,LOW);
      delay(100);
    }
  digitalWrite(ledPinR,LOW);
  digitalWrite(triggerPin, LOW);
  triggerState = analogRead(triggerPin);   
  }
}

Thanks
-Mike

Hi,

From 404 Page Not Found

the sizeof() operator does not give you the number of elements in an array, it gives you the number of bytes a thing occupies in memory

Since intervals is declared int, and ints are 2 bytes on arduino, you are overrunning the end of your array in your for loop.

best,
Michael

edit: BTW, the serial monitor restarts you sketch when opened.