VarSpeedServo not working with Serial Monitor

Hopefully this isn't too basic of a question but I'm having trouble getting the serial to write the degrees of two servos using VarSpeedServo - I'm mapping color values from a sensor into a processing sketch, and need to be able to map their locations by tracking the x and y servo degrees.

Right now it seems like the values for x and y are staying at 0 (like HEX, 0,0), even though the servos are rotating. The speed that it updates also seems slow - it could be that the servos begin the sketch at 0 each time and the serial only updates once per loop at the beginning?

Is there a better way to track the position of the servos and write the sensor val at each degree?

#include <VarSpeedServo.h>
#include <Wire.h>
#include "Adafruit_TCS34725.h"

VarSpeedServo xservo;
VarSpeedServo yservo;
VarSpeedServo zservo;

const int servoPin1 = 9;
const int servoPin2 = 10;
const int servoPin3 = 11;

String inputString = "";
bool stringComplete = false;

float lux;
#define commonAnode true
byte gammatable[256];

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

void setup() {

  xservo.write(0, 100, true);

  yservo.write(0, 100, true);

  zservo.write(0, 100, true);


  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);

  for (int i = 0; i < 256; i++) {
    float x = i;
    x /= 255;
    x = pow(x, 2.5);
    x *= 255;

    if (commonAnode) {
      gammatable[i] = 255 - x;
    } else {
      gammatable[i] = x;

  String hexColor = "ffffff";

void loop()
  int randRightDegree;
  int randLeftDegree;
  int randTotalDegree;
  int randSpeed;
  randSpeed = random(0,40);
  randRightDegree = random(90, 180);
  randLeftDegree = random(0, 90);
  randTotalDegree = random(0, 180);
  randSpeed = random(5, 25);

  uint16_t r, g, b, c, colorTemp, lux;

  tcs.getRawData(&r, &g, &b, &c);
  lux = tcs.calculateLux(r, g, b);
  float red, green, blue;

  int x, y, z;

  tcs.getRGB(&red, &green, &blue);

  tcs.setInterrupt(true);  // turn off LED

  //Serial.print("R:\t"); Serial.print(int(red));
  //Serial.print("\tG:\t"); Serial.print(int(green));
  //Serial.print("\tB:\t"); Serial.print(int(blue));

  //Serial.print("Lux: "); Serial.print(lux, DEC);

  Serial.print((int)red, HEX);
  Serial.print((int)green, HEX);
  Serial.print((int)blue, HEX);

  if (lux > 200) {

    x = x + 1;
    xservo.write(x, 20, true);
    if (x > 180)
      x = 180;

    y = y + 1;
    yservo.write(y, randSpeed, false);
    if (y > 180)
      y = 180;

  else {

This line:


  int x, y, z;

Creates three local variables. Since they are uninitialized, they will contain whatever value happens to be at that memory location at the time.

Next you print the values of these uninitialized local variables, which only tells you what happened to be in the memory they were allocated




I suspect what you actually want to do is make the x and y variables either global or static, which will cause them to be zero-initialized and also retain their values even after returning from the loop function. Please take some time to study this reference page:

And if you're curious about static:

That fixed it - thank you so much!

You're welcome. I'm glad to hear it's working now. Enjoy!