Combining servos & relays

Hello!

I built a pan/tilt mount for a webcam which is controlled by an arduino using the SkyDuino - Webcam Pan/tilt via Skype | Arduino Blog skyduino sketch. It works fine!
Now I want to switch on a 230V bulb to see whats going on in front of the cam when a key is pressed.
I don't have the experience to modificate the skyduino-program so I decided to use "C" (which moves the servos to default position) as the key to switch on the bulb.

I tried to combine the skyduino sketch with a relay sketch. The compiling and upload works, but when I use the Serial Monitor it doesn't.
The wirings are ok - if I try both sketches seperate they work.

Here's my code:

#include <Servo.h> 
#define RELAY_PIN 3

Servo xServo;
Servo yServo;

int stepSize = 10;

const int xInit = 90;
const int yInit = 90;
const int xMin = 0;
const int xMax = 180;
const int yMin = 0;
const int yMax = 180;

int x = xInit;
int y = yInit;
int xNew = x;
int yNew = y;

void setup()
{
  Serial.begin(9600);
  //establishContact();  // send a byte to establish contact until receiver responds 

  xServo.attach(5);  // attaches the servo on pin 5 to the servo object 
  yServo.attach(6);  // attaches the servo on pin 6 to the servo object 

  delay(1000);
  MoveServo(xServo, xServo.read(), x);
  MoveServo(yServo, yServo.read(), y);
  delay(100);
  pinMode(11, OUTPUT);
  pinMode(RELAY_PIN, OUTPUT);
}

void Beep()
{
  digitalWrite(11,HIGH);
  delay(250);
  digitalWrite(11,LOW);   
}

void loop()
{
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    Beep();
    char msg = Serial.read();

    if (msg == 'C') {xNew=xInit; yNew=yInit;}

    if (msg == 'R') xNew = x - stepSize;
    if (msg == 'L') xNew = x + stepSize;

    if (msg == 'D') yNew = y - stepSize;
    if (msg == 'U') yNew = y + stepSize;

    xNew = constrain(xNew, xMin, xMax);
    yNew = constrain(yNew, yMin, yMax);
  }
static int relayVal = 0;
  int cmd;

  while (Serial.available() > 0)
  {
    cmd = Serial.read();

    switch (cmd)
    {
    case 'C':
      {
        relayVal ^= 1; // xor current value with 1 (causes value to toggle)
      }
    }

    if (relayVal)
      digitalWrite(RELAY_PIN, HIGH);
    else
      digitalWrite(RELAY_PIN, LOW);
   }
  MoveServo(xServo, x, xNew);
  MoveServo(yServo, y, yNew);
  x = xNew;
  y = yNew;
}

void MoveServo(Servo servo, int moveFrom, int moveTo)
{
  if (moveFrom <= moveTo)
  {
    for (int c=moveFrom; c<=moveTo; c++)
    {
      servo.write(c);
      delay(50);
    }
  }

  if (moveFrom > moveTo)
  {
    for (int c=moveFrom; c>=moveTo; c--)
    {
      servo.write(c);
      delay(50);
    }
  }
}

Sorry for my bad english

Here's one thing:

if (relayVal)
digitalWrite(RELAY_PIN, HIGH);
else
digitalWrite(RELAY_PIN, LOW);

You need brackets { } on your 'if statements' that are on more than one line.
http://arduino.cc/en/Reference/Else

more reference info here: Arduino - Home

But the then part and the else parts are single statements, this is OK. I would make use of conditional expressions myself:

    digitalWrite(RELAY_PIN, relayVal ? HIGH : LOW);

MarkT:
I would make use of conditional expressions myself:

    digitalWrite(RELAY_PIN, relayVal ? HIGH : LOW);

Which I would reduce to:

    digitalWrite(RELAY_PIN, relayVal);

It would be even clearer if relayVal was declared boolean instead of int.

static boolean relayVal = false;
...
relayVal = !relayVal;

Thanks for your help!

I tried your suggestions and now it is working perfect - but only when I'm testing it with the serial monitor.
But when I use the Skyduino interface there is still a little problem. The bulb doesn`t switch on reliably. If I type in 2 "C" in the serial monitor it toggles. But when I doubleclick on the "C" button in the Skyduino interface sometimes the relay switches and sometimes it doesn't.

Any idea? :slight_smile:

Here's my code

static boolean relayVal = false;
  
  int cmd;

  while (Serial.available() > 0)
  {
    cmd = Serial.read();

    switch (cmd)
    {
    case 'C':
      {
        relayVal = !relayVal; // xor current value with 1 (causes value to toggle)
      }
    }

    digitalWrite(RELAY_PIN, relayVal);
   }

Your switchcase needs a default http://arduino.cc/en/Reference/SwitchCase

Maybe the relay is not getting enough power. Make sure you have pinMode(RELAY_PIN,OUTPUT); in the setup.

To get more power you could use an npn transistor and an external power source, (or maybe using power from the 5v pin switched through the transistor might work?)

Check the pin with an LED just to make sure that your code is working right, switching it on and off.