Hi Everyone
This is my first post and I've started looking at Arduino for a few small projects whilst in rehab from a major accident. So I do have a bit of time on my hands, however that being said I have run into a bit of a problem that has resulted in this post (must be a little impatient I guess) hope someone can point me in the right direction.
I have looked at a number of tutorials relating to the Xbee and the Motor Shield I purchased. I am at a stage where I have my Xbees talking, I can control the required 2 Motors by passing a variable to the second Arduino with the motor shield. But I now have two more steps (questions)
Thanks in advance. Renwaar.
1, How can I make two variables pass to the second Arduino so that it will move my project in two directions at once i.e. horizontal and vertically resulting in a diagonal movement?
2, I wish to control the speed based on the Joystick position. I have this working fine in my head of course but just need a little help with the code if someone has the time?
Here is the code I have managed so far:
Joystick Arduino:
//Code from Sparkfun with a few tweeks
const byte PIN_ANALOG_X = 0;
const byte PIN_ANALOG_Y = 1;
const int X_THRESHOLD_LOW = 450; //I have the contrains real big so there's less chance for
const int X_THRESHOLD_HIGH = 550; //analog misinterpretation.
const int Y_THRESHOLD_LOW = 450;
const int Y_THRESHOLD_HIGH = 550;
int x_position;
int y_position;
int x_direction;
int y_direction;
void setup() {
Serial.begin(9600);
}
void loop () {
x_direction = 0;
y_direction = 0;
x_position = analogRead(PIN_ANALOG_X);
y_position = analogRead(PIN_ANALOG_Y);
if (x_position > X_THRESHOLD_HIGH) {
x_direction = 1;
} else if (x_position < X_THRESHOLD_LOW) {
x_direction = -1;
}
if (y_position > Y_THRESHOLD_HIGH) {
y_direction = 1;
} else if (y_position < Y_THRESHOLD_LOW) {
y_direction = -1;
}
if (x_direction == -1) {
if (y_direction == -1) {
Serial.println("left-down");
} else if (y_direction == 0) {
Serial.println("a"); // "a" is horiz pan right
} else {
// y_direction == 1
Serial.println("left-up");
}
} else if (x_direction == 0) {
if (y_direction == -1) {
Serial.println("s"); // "s" is verti pan up
} else if (y_direction == 0) {
Serial.println("f"); // "f" is for Stop until something else is sent
} else {
// y_direction == 1
Serial.println("w"); // "w" is verti pan down
}
} else {
// x_direction == 1
if (y_direction == -1) {
Serial.println("right-down");
} else if (y_direction == 0) {
Serial.println("d"); // "d" is horiz pan left
} else {
// y_direction == 1
Serial.println("right-up");
}
}}
Motor Shield Arduino:
#include <AFMotor.h>
AF_DCMotor horiz(1, MOTOR12_1KHZ);
AF_DCMotor verti(2, MOTOR12_1KHZ);
AF_DCMotor motors[2] = {horiz, verti};
void setup(void)
{
Serial.begin(9600);
}
void loop(void)
{
while (Serial.available() < 0) {} // Wait until a character is received
char val = Serial.read();
int Xspeed = 255; //255 is maximum speed
int Yspeed = 255;
//horiz.setSpeed(155);
//verti.setSpeed(255);
switch(val) // Perform an action depending on the command
{
case 'w'://Verti Pan Down
verti.setSpeed(Yspeed);
verti.run(FORWARD);
Serial.print("Pan Down");
break;
case 's'://Verti Pan Up
verti.setSpeed(Yspeed);
verti.run(BACKWARD);
Serial.print("Pan Up");
break;
case 'a'://Horiz Pan Right
horiz.setSpeed(Xspeed);
horiz.run(FORWARD);
Serial.print("Pan Right");
break;
case 'd'://Horiz Pan Left
horiz.setSpeed(Xspeed);
horiz.run(BACKWARD);
Serial.print("Pan Left");
break;
case 'f'://Stop
horiz.run(RELEASE);
verti.run(RELEASE);
break;
default:
break;
}
}