combining servo & sensor

Hi!

I’d like to combine servo movement and sensor reading, but I’m stuck.
The basic idea is to loop the following:

  • wait for serial input
  • if serial input is ‘0’ put servo in position 0, perform 10x sensor readings and turn off the sensor leds
  • if serial input is ‘1’ put servo in position 180, perform 10x sensor readings and turn off the sensor leds

Hope someone can help…

So far I came up with:

#include “SparkFun_AS7265X.h” // it’s a multi-spectral sensor with 18 sensor readings
AS7265X sensor;

#include <Wire.h>
#include <Servo.h>
int servoPin = 11;
Servo Servo1;
int times = 10; //set n-repeats

void setup() {
Servo1.attach(servoPin);
Serial.begin(9600);
while(Serial.available() == false) {}
Serial.read();

if(sensor.begin() == false)
{
Serial.println(“Sensor does not appear to be connected. Please check wiring. Freezing…”);
while(1);

}

sensor.disableIndicator(); //disable indicator led on sensor

}

void (* resetFunc)(void) = 0;

void loop() {
if(Serial.available()) {
char c = Serial.read();

if (c==‘0’) {
Servo1.write(0); //servo to position 0
delay(1000);
while(times > 0) { //perform measurements 10x

//get sensor readings and print to serial

sensor.takeMeasurementsWithBulb();

Serial.print(sensor.getCalibratedA());
Serial.print(",");
Serial.print(sensor.getCalibratedB());
Serial.print(",");
Serial.print(sensor.getCalibratedC());
Serial.print(",");
Serial.print(sensor.getCalibratedD());
Serial.print(",");
Serial.print(sensor.getCalibratedE());
Serial.print(",");
Serial.print(sensor.getCalibratedF());
Serial.print(",");

Serial.print(sensor.getCalibratedG());
Serial.print(",");
Serial.print(sensor.getCalibratedH());
Serial.print(",");
Serial.print(sensor.getCalibratedI());
Serial.print(",");
Serial.print(sensor.getCalibratedJ());
Serial.print(",");
Serial.print(sensor.getCalibratedK());
Serial.print(",");
Serial.print(sensor.getCalibratedL());
Serial.print(",");

Serial.print(sensor.getCalibratedR());
Serial.print(",");
Serial.print(sensor.getCalibratedS());
Serial.print(",");
Serial.print(sensor.getCalibratedT());
Serial.print(",");
Serial.print(sensor.getCalibratedU());
Serial.print(",");
Serial.print(sensor.getCalibratedV());
Serial.print(",");
Serial.print(sensor.getCalibratedW());
Serial.print(",");
Serial.println();

times = times -1;

}}

if (c==‘1’) {
Servo1.write(180); //put servo to position 180
delay(1000);
while(times > 0) {

sensor.takeMeasurementsWithBulb();
Serial.print(sensor.getCalibratedA());
Serial.print(",");
Serial.print(sensor.getCalibratedB());
Serial.print(",");
Serial.print(sensor.getCalibratedC());
Serial.print(",");
Serial.print(sensor.getCalibratedD());
Serial.print(",");
Serial.print(sensor.getCalibratedE());
Serial.print(",");
Serial.print(sensor.getCalibratedF());
Serial.print(",");

Serial.print(sensor.getCalibratedG());
Serial.print(",");
Serial.print(sensor.getCalibratedH());
Serial.print(",");
Serial.print(sensor.getCalibratedI());
Serial.print(",");
Serial.print(sensor.getCalibratedJ());
Serial.print(",");
Serial.print(sensor.getCalibratedK());
Serial.print(",");
Serial.print(sensor.getCalibratedL());
Serial.print(",");

Serial.print(sensor.getCalibratedR());
Serial.print(",");
Serial.print(sensor.getCalibratedS());
Serial.print(",");
Serial.print(sensor.getCalibratedT());
Serial.print(",");
Serial.print(sensor.getCalibratedU());
Serial.print(",");
Serial.print(sensor.getCalibratedV());
Serial.print(",");
Serial.print(sensor.getCalibratedW());
Serial.print(",");

Serial.println();
times = times -1;

}}

else {

//turn off leds after measurement

sensor.disableBulb(0);
sensor.disableBulb(1);
sensor.disableBulb(2);
Serial.read();
resetFunc();
}
}
}

while(Serial.available() == false) {} Serial.available does not return a Boolean.

What seems to be the problem?

Please remember to use code tags when posting code.

times = times -1;

After times reaches 0 the first time through loop, it is never reset back to 10 so the loop only works once?