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