ok so here is the code that i tried. This is a little more involved but i also tried sending a simple string or an integer with the Serial.read() and Serial.write() commands and i still dont see anything on the receiving side.
Code for Sender
- Code: Select all
//int inByte = 0; // incoming serial byte
// Serial Variables
const int packetLength = 4;
byte packet[(packetLength - 1)];
int checksum;
// Light Button Variables
const int lightsbuttonPin = 2; // the number of the pushbutton pin
int lightsbuttonState = 0; // variable for reading the pushbutton status
// Servo Motor Joystick Variable
const int joyH = 4; // Joystick Horizontal Axis, Analog, Pin 4
const int joyV = 5; // Joystick vertical Axis, Analog, Pin 5
// Servo Motor Variables
int servoValH; // Variable to read the input value from the analog joystick
int servoValV; // Variable to read the input value from the analog joystick
// Motor Joystick Pin Variable
int forwardreversepotPin = 0;
int motorSpeedControlandDir;
int forwardreversepotValue=0;
void setup() {
Serial.begin(9600); // Inizialize Serial
// initialize the pushbutton pin as an input:
pinMode(lightsbuttonPin, INPUT);
}
void loop(){
// Read Light Button State
lightsbuttonState = digitalRead(lightsbuttonPin);
// Read the Horizontal joystick value (value between 0 and 1023)
servoValH = analogRead(joyH);
servoValH = map(servoValH, 0, 1023, 0, 180); // scale it to use it with the servo (result between 0 and 180)
// Read the Vertical joystick value (value between 0 and 1023)
servoValV = analogRead(joyV);
servoValV = map(servoValV, 0, 1023, 0, 180); // scale it to use it with the servo (result between 0 and 180)
// Read the Rear Motor Joystick value
motorSpeedControlandDir = analogRead(forwardreversepotPin)/4; // scale it to 0-FF
analogWrite(4,forwardreversepotValue);
// Send Light Button State value
createPacket(0x00, lightsbuttonState);
sendSerialData(packet);
// Send Horizontal Joystick value
createPacket(0x01, servoValH);
sendSerialData(packet);
// Send Vertical Joystick value
createPacket(0x02, servoValV);
sendSerialData(packet);
// Send Motor Speed & Direction Joystick value
createPacket(0x03, motorSpeedControlandDir);
sendSerialData(packet);
// Read Serial Data In, if it is available
//if(Serial.available() > 0){
//inByte = Serial.read(); // get incoming byte
// Serial.println(inByte);
//}
delay(100); // Delay loop
}
// Create Packet Data
void createPacket(byte cmd, byte data){
packet[0] = 0x7E;
packet[1] = cmd;
packet[2] = data;
// Create Checksum Value, (cmd + data) = 0xFF - (cmd + data)
checksum = cmd + data;
checksum = 0xFF - checksum;
checksum = 0xFF & checksum;
packet[(packetLength - 1)] = checksum;
return;
}
// Send Packet Serially
void sendSerialData(byte thePacket[3]){
Serial.print(thePacket[0], BYTE);
Serial.print(thePacket[1], BYTE);
Serial.print(thePacket[2], BYTE);
Serial.print(thePacket[3], BYTE);
return;
}
Code for Receiver
- Code: Select all
#include <Servo.h>
// Serial Variables
int inByte = 0; // incoming serial byte
int serialData[5];
int arrayCount = 0;
int checksum;
// Servo Motor Pin Variables
const int servo1 = 7
; // first servo, PWM, Pin 3
const int servo2 = 2; // second servo, PWM, Pin 2
// Servo Motor Variables
int servoValH; // variable to read the value from the analog pin
int servoValV; // variable to read the value from the analog pin
// Servo Motor Object Variables
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
// Motor Speed & Direction Pin Variables
int Motpin = 12;
int Joystickpin = 0;
int Relay = 52;
void setup() {
// Attach the Servo to the Object
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
// Set up Pin for Motor Direction
pinMode(Relay, OUTPUT);
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
// Read Serial Data In, if it is available
if(Serial.available() > 0){
inByte = Serial.read(); // get incoming byte
// Load Serial data into a packet until 4 bytes is reached.
// Verify the the beginning of the packet with 0x7E
if(inByte == 0x7E || arrayCount > 0 && arrayCount < 4){
serialData[arrayCount] = inByte; // Receive Data From Serial Port
arrayCount++; // Increment the Array Counter
}
// Verify Checksum if a packet is received
if(arrayCount == 4){
arrayCount = 0; // Reset Data Packet Counter
checksum = serialData[1] + serialData[2]; // Add Bytes 1 & 2 for checksum
checksum = 0xFF - checksum; // Subtract checksum from 0xFF
checksum = 0xFF & checksum;
if(checksum == serialData[3]){ // Checksum is good
Serial.print("good");
switch(serialData[1]){
case 0x00: // LED
//serialData[2];
break;
case 0x01: // Servo H
moveServoH(serialData[2]);
break;
case 0x02: // Servo V
moveServoV(serialData[2]);
break;
case 0x03: // Servo V
rearMotorControl(serialData[2]);
break;
}
}
else{ // Checksum Failed
Serial.println("Bad Packet");
}
}
}
//delay(15); // waits for the servo to get there
}
void moveServoH(int servoValH){
// Horizontal Servo
myservo2.write(servoValH); // sets the servo position according to the scaled value
return;
}
void moveServoV(int servoValV){
// Vertical Servo
myservo1.write(servoValV); // sets the servo position according to the scaled value
return;
}
void rearMotorControl(int motorSpeedDir){
int PWMval;
if(motorSpeedDir >= 125){
PWMval = (1.6538 * motorSpeedDir) - 166.7;
analogWrite(Motpin, PWMval);
digitalWrite(Relay, LOW);
Serial.println("RELAY OFF");
}
else if(motorSpeedDir < 125){
PWMval = 1.6538 * (255 - motorSpeedDir) - 166.7;
analogWrite(Motpin, PWMval);
digitalWrite(Relay, HIGH);
Serial.println("RELAY ON");
}
}
As far a pictures i dont have any, but i have them connected with wires comming out of the adafruit shield the folowing way. RX-to-Arduino Rx, Tx-to-arduino Tx, 5V-to-Arduino 5V out and Gnd-to-arduino Gnd. (same configuration on both arduinos)
Ps: Do you have an idea of a more simple code for me to at least test them?