Hello all, I am a very beginnger and have a project which I want to control two servos wirelessly by using a keyboard (arrow keys) from my macbook pro. I have two codes for which the one arduino connected to pc and the other connected to the servos but I think something wrong and perhaps because of the code of the transmitter arduino code or I do not manage to work with the Processing code ??? Any help, suggesstion will be much aprreciated. By the way I am using Xbee s1 and they are working fine.
*************************************************** For the transmitter **************************************
char key = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
key = Serial.read();
if (key == byte(37))
{ // left
Serial.print("L");
delay(2000);
}
else if (key == byte(38))
{ // up
Serial.print("U");
delay(2000);
}
else
{
Serial.print("Z"); //doesn't matter what it is
}
}
}
***************************** Receiver Part, The one connected to the servos*************************
#include <Servo.h>
Servo myservo1;
int pos1 = 0;
int increment = 50;
int decrement = 50;
void setup() {
Serial.begin(9600);
myservo1.attach(10);
}
void loop() {
if (Serial.available()) {
int dataByte = Serial.read();
Serial.print(dataByte);
if(dataByte == 'L'){
pos1 = pos1 + increment;
myservo1.write(pos1);
}
else if(dataByte == 'U') {
pos1 = pos1 - decrement;
myservo1.write(pos1);
}
else //otherwise
{
pos1 = pos1 + 0;
myservo1.write(pos1);
}
}
}
*************************************************** Processing Code ************************
import processing.serial.*;
color fillVal = color(0,0,0);
Serial port;
void setup() {
size(100, 100);
println("Available serial ports:");
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
background(0,0,0);
if (keyPressed == true) {
fill(fillVal);
ellipse(50, 50, 50, 50);
}
else {
keyReleased();
}
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
fillVal = color(255,255,255);
} else if (keyCode == DOWN) {
fillVal = color(255,0,0);
} else if (keyCode == LEFT){
fillVal = color(0,255,0);
} else if (keyCode == RIGHT) {
fillVal = color(0,255,150);
}
} else {
fillVal = 0;
}
port.write(keyCode);
if (keyPressed == false) {
port.write('0');
}
}
void keyReleased() {
}