Serial.println stops code from working after FOR LOOP

Hi,

I have a simple code that reads an analog voltage on A0 every split second and stores the voltage in an array. After my FOR LOOP, I want to print “DONE SCANNING” to the serial monitor, but the Serial.println command seems to be stopping my code. My digitalWrite command also does not work after the print command. Any idea what is going on?

//****   DECLARATIONS   ************************************************************************************************************************
//**********************************************************************************************************************************************
int sec = 1000;                                             //Define sec variable as 1000 (ms)
int rotateCCW = 4;                                          //Set CCW motor rotation = Pin 4
int rotateCW = 2;                                           //Set CW motor rotation = Pin 2
int scanAngle = 50;                                         //Angle that the solar panel will scan (Degrees)
float rotationSpeed = 17.7;                                 //Rotation speed of motor (Degrees/s)
float scanTimeCCW = scanAngle/rotationSpeed;                //Time that the solar panel will scan in CCW
float scanTimeCW = 2*(scanAngle/rotationSpeed);             //Time that the solar panel will scan in CW
float recordTime = scanTimeCCW/4;                           //Store recordTime = time between each value in voltageArray   
float voltageArray[9];                                      //Store voltageArray with 9 elements for Solar Panel's output
int i;                                                      //Variable for counter
float maxVoltage = 0;                                       //Store maximum value in voltageArray = maxVoltage. Initialize to 0.

//****   SETUP   *******************************************************************************************************************************
//**********************************************************************************************************************************************
void setup() {
Serial.begin(9600);                                         //Initialize baud rate for serial monitor

pinMode(rotateCW, OUTPUT);                                  //Set CW motor rotation pin = OUTPUT
pinMode(rotateCCW, OUTPUT);                                 //Set CCW motor rotation pin = OUTPUT
pinMode(A0,INPUT);                                          //Current sensor input = PIN A0

} //end SETUP


//****   LOOP   ********************************************************************************************************************************
//**********************************************************************************************************************************************
void loop() {

//---   INITIAL SCAN  & DATA AQUISITION ------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
digitalWrite(rotateCCW,HIGH);                               //Rotate CCW prior to scan
Serial.println("Rotate: CCW");
  delay(scanTimeCCW*sec);                                   //Allow motor rotation for scanTimeCCW
digitalWrite(rotateCCW,LOW);                                //Stop CCW rotation 
  delay(sec);                                               //Pause for 1 sec before moving CW
digitalWrite(rotateCW,HIGH);                                //Start CW rotation
Serial.println("Rotate: CW");

for (i=0; i <= 9; i++){
  int sensorValue = analogRead(A0);                         //Read value of voltage sensor
  voltageArray[i] = sensorValue * (5.0 / 1023.0);           //Set array element = voltage sensor value
    delay(recordTime*sec);                                  //Delay between each entry in array
   }
Serial.println("DONE SCANNING");   
digitalWrite(rotateCW,LOW);                                 //Stop CCW rotation

You’ve declared an array of 9 elements:

float voltageArray[9];

but this will write into elements 0, 1, 2 … ,8 ,9 which is ten elements.

for (i=0; i <= 9; i++){
  int sensorValue = analogRead(A0);                         //Read value of voltage sensor
  voltageArray[i] = sensorValue * (5.0 / 1023.0);

Either declare the array to have 10 elements or change “i <= 9” to be “i < 9”

Pete

Thank you Pete! That was driving me crazy.