I'll get right to the point, or at least try to XD.
My problem is that this code is theoretically made for 4 Servos, but I only have 2, I was wondering how I would get this one to work..Im stuck...
Because the slave code receives the Y-Axis and X-Axis Values wirelessly at the same time, so for example (once you look at the code you'll understand this), when val is 'f' it is also 'o', values 'a' - 'k' are meant for back and forward, while the rest is left and right, how do I make it so when val is 'l', 'm', 'n' it does not also equal 'f'(which is the resting point of the servo) having it a resting point as well as moving for 'l' 'm' and 'n' obviously makes the servos keep shaking...(I hope this posted right)
I'll post the master first.
/*
The following code is what will be uploaded to the Master Device
In this case the Master Device is the Arduino Uno, with the DFR0008 Joystick Shield.
This code reads the specified INPUTS entered by the user,
and it stores them to be transmitted via the WL-320 Wireless Transceiver.
*/
#include <SoftwareSerial.h> //Include to allow Serial Communication on other digital pins.
SoftwareSerial Transceiver(2, 3); /*(Rx , Tx) Arduino Uno shows Rx and Tx as Pins 0 and 1...
...instead hook-up to Digital Pins 2 and 3, than connect to Rx(2) to Tx on the Transceiver, and Vice-Versa for Tx.*/
#define x_Axis A3 //Declares Joysticks X-Axis as Pin A3
#define y_Axis A2 //Declares Joysticks Y-Axis as Pin A2
void setup() {
Transceiver.begin(9600);//Sets Comminication Baud rate for Transceiver.
Serial.begin(9600);//Sets Comminication Baud rate for Serial Port.
}
void loop() {
int val1 = analogRead(x_Axis); // Declares "val1" as equal to Joysticks X-Axis.
int val2 = analogRead(y_Axis); // Declares "val2" as equal to Joysticks Y-Axis.
int xval = map(val1, 0, 1023, -3, 4); /*Converts Joyticks X-Axis values to a range of (3,-3)
and stores new value in "xval."*/
int yval = map(val2, 0, 1023, -5, 5);/*Converts Joyticks Y-Axis values to a range of (5,-3)
and stores new value in "yval."*/
Serial.print(yval);//-----
Serial.print(" "); // |
Serial.print(xval);// }->Prints the New Joystick Values in the Serial Monitor.
Serial.println(" ");// |
Serial.print(" "); //-----
/*
The following Switch-Case Statements proc based on the values emmitted when the User moves/or...
...doesn't move the Joystick...
...When a case is triggered it writes a single character 'char' to the Serial Port/Tranceiver to store.
*/
switch (xval) { //Switch-Case for X-Axis.
case -3:
{
Transceiver.write('l');
break;
}
case -2:
{
Transceiver.write('m');
break;
}
case -1:
{
Transceiver.write('n');
break;
}
case 0:
{
Transceiver.write('o');
break;
}
case 1:
{
Transceiver.write('p');
break;
}
case 2:
{
Transceiver.write('q');
break;
}
case 3:
{
Transceiver.write('r');
break;
}
case 4:
{
Transceiver.write('s');
break;
}
}
switch (yval) {//Switch-Case for Y-Axis.
case -5:
{
Transceiver.write('a');
break;
}
case -4:
{
Transceiver.write('b');
break;
}
case -3:
{
Transceiver.write('c');
break;
}
case -2:
{
Transceiver.write('d');
break;
}
case -1:
{
Transceiver.write('e');
break;
}
case 0:
{
Transceiver.write('f');
break;
}
case 1:
{
Transceiver.write('g');
break;
}
case 2:
{
Transceiver.write('h');
break;
}
case 3:
{
Transceiver.write('i');
break;
}
case 4:
{
Transceiver.write('j');
break;
}
case 5:
{
Transceiver.write('k');
break;
}
}
delay(10); // delays the whole switch statement in intervals of 10ms.
}
/*The following code will be uploaded to the Slave Device
In this case the Device used is the ATMEGA328 with an Arduino Uno bootloaded on it.
This code will receive and store Data sent over the Serial Port.
*/
#include <SoftwareSerial.h>//Include to allow Serial Communication on other digital pins.
#include <Servo.h>//Include to use Servo Library.
Servo rightWheel;//Declares name for Servo
Servo leftWheel;//Declares name for Servo
SoftwareSerial Receiver(2, 3);/*(Rx , Tx) Arduino Uno shows Rx and Tx as Pins 0 and 1...
...instead hook-up to Digital Pins 2 and 3, than connect to Rx(2) to Tx on the Receiver, and Vice-Versa for Tx.*/
//*NOTE* on the ATMEGA328 Digital Pins 2 & 3 are in Pin position 4 & 5 respectively.
void setup() {
Receiver.begin(9600); //Sets Comminication Baud rate for Receiver.
Serial.begin(9600);//Sets Comminication Baud rate for Serial Port.
leftWheel.attach(5);//Initalizes rightWheel to Digital Pin 5.
rightWheel.attach(11);//Initalizes rightWheel to Digital Pin 11.
//*NOTE* on the ATMEGA328 Digital Pins 5 & 11 are in Pin position 11 & 17 respectively.
}
void loop() {
if (Receiver.available() > 0) { // Checks if Data is available.
char val = Receiver.read(); // reads the Data and stores it in "val" as a char.
Serial.print(val); // Prints to Serial Monitor, for confirmation.
Serial.println(" ");
/*the following takes the Stored Char data and places them into Switch-Case-Statements.
based on the Data received and stored in val, the case statements will perform either a clockwise or..
a counter-clockwise rotation of the servos.
*/
switch (val) {
/*---> cases e-a read the joysticks Y-Axis and perform a forward motion, turing rightWheel Clockwise and
leftWheel Counter Clockwise.because its a continuous servo the values indicate speed rather than the angle.
---> case f is the Resting point of both servos.
---> cases g-k perform a backwards motion, turing leftWheel Clockwise and rightWheel Counter Clockwise.
*/
case 'a':
{
rightWheel.write(0);
leftWheel.write(180);
break;
}
case 'b':
{
rightWheel.write(20);
leftWheel.write(160);
break;
}
case 'c':
{
rightWheel.write(40);
leftWheel.write(140);
break;
}
case 'd':
{
rightWheel.write(60);
leftWheel.write(120);
break;
}
case 'e':
{
rightWheel.write(80);
leftWheel.write(100);
break;
}
case 'f'://RESTING POINT OF SERVOS
{
rightWheel.write(90);
break;
}
case 'g':
{
rightWheel.write(100);
leftWheel.write(80);
break;
}
case 'h':
{
rightWheel.write(120);
leftWheel.write(60);
break;
}
case 'i':
{
rightWheel.write(140);
leftWheel.write(40);
break;
}
case 'j':
{
rightWheel.write(160);
leftWheel.write(20);
break;
}
case 'k':
{
rightWheel.write(180);
leftWheel.write(0);
break;
}
//--------------------------------------------------
case 'l':
{
leftWheel.write(180);
break;
}
case 'm':
{
leftWheel.write(180);
break;
}
case 'n':
{
leftWheel.write(180);
break;
}
//RESTING POINT
case 'o':
{
leftWheel.write(90);
break;
}
case 'p':
{
rightWheel.write(140);
break;
}
case 'q':
{
rightWheel.write(160);
break;
}
case 'r':
{
rightWheel.write(180);
break;
}
case 's':
{
rightWheel.write(180);
break;
}
}
}
}