Hi ,
I'm trying to control a PTZ camera wirelessly using 2 nanos and LoRa transceivers. I have a joystick and from the a-d's with a little maths I'm able to generate the correct 7 bytes for a varity of camera actions.
I can send these commands from one nano to the other, however they seam to drop the leading zeros of any byte so for example FF 01 is transmitted as 2551. at the moment im sending the 7 bytes using a for loop however I have tried sending 1 at a time also without sucsess.
I want to be able to store the incomming bytes in an array or set of variables so I can send thes to the serial port.
Can anyone advise?
Below is my sending code ill replay on my other machine with my recieving code.
Thank you for reading
Paul
#include <SPI.h>
#include <LoRa.h>
#define joypan A1
#define joytilt A2
#define joyzoom A3
int counter = 0;
int input8 =3; //button on pin 3
int input4 =4; //button on pin 4
int input2 =5; //button on pin 5
int input1 =6; //button on pin 6
int panValue;
int tiltValue;
int zoomValue;
byte msg[7];
int panbyte4;
int tiltbyte4;
int zoombyte4;
int panSpeed;
int tiltSpeed;
//int byte4=(panbyte4+tiltbyte4+zoombyte4);
void setup() {
pinMode(input8 , INPUT_PULLUP);
pinMode(input4 , INPUT_PULLUP);
pinMode(input2 , INPUT_PULLUP);
pinMode(input1 , INPUT_PULLUP);
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Sender");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void sendMessage() {
Serial.print("Message1");
// send packet
LoRa.beginPacket();
for (byte i = 0; i<7; i++) {
LoRa.print (msg[i],HEX);
}
LoRa.endPacket();
}
void movement(){
Serial.print("moving");
Serial.println ();
Serial.print("panValue: ");
Serial.println(panValue);
if (panValue >520)
panbyte4 = 0x04;
panSpeed = (panValue-520)/2;
if (panValue <480)
panbyte4 = 0x02;
panSpeed = (480-panValue)/2;
Serial.print("tiltValue: ");
Serial.println(tiltValue);
if (tiltValue >520)
tiltbyte4 =0x08;
tiltSpeed = (tiltValue-520)/2;
if (tiltValue <480)
tiltbyte4 =0x10;
tiltSpeed = (480-tiltValue)/2;
Serial.print("zoonValue: ");
Serial.println(zoomValue);
if (zoomValue >520)
zoombyte4 =0x20; // these values are wrong check and change
if (zoomValue < 480)
zoombyte4 =0x40; // these values are wrong check and change
msg[0] =0xFF;
msg[1] =0x01;
msg[2] =0x00;
msg[3] = byte(panbyte4 + tiltbyte4 + zoombyte4);
msg[4] = byte(panSpeed);
msg[5] = byte(tiltSpeed);
msg[6] = byte((msg[1] + msg[2] + msg[3] + msg[4] + msg[5])%256);
Serial.print (msg[0]);
Serial.println ();
Serial.print (msg[1]);
Serial.println ();
Serial.print (msg[2]);
Serial.println ();
Serial.print (msg[3]);
Serial.println ();
Serial.print (msg[4]);
Serial.println ();
Serial.print (msg[5]);
Serial.println ();
Serial.print (msg[6]);
Serial.println ();
for (byte i = 0; i<7; i++) {
Serial.print (msg[i]);
}
Serial.println ();
sendMessage();
msg[0] =0xFF;
msg[1] =0x01;
msg[2] =0x00;
msg[3] =0x00;
msg[4] =0x00;
msg[5] =0x00;
msg[6] =0x01;
delay(500);
sendMessage();
delay(2000);
}
void loop() {
msg[0] =0x00;
msg[1] =0x00;
msg[2] =0x00;
msg[3] =0x00;
msg[4] =0x00;
msg[5] =0x00;
msg[6] =0x00;
//msg[7] =0x00;
//for (byte i = 0; i<7; i++) {
// Serial.print (msg[i]);
//}
Serial.println ();
Serial.print("looping: ");
Serial.println(counter);
panValue = analogRead(joypan);
tiltValue = analogRead(joytilt);
zoomValue = analogRead(joyzoom);
if (panValue >520)
movement();
if (panValue <480)
movement();
if (tiltValue >520)
movement();
if (tiltValue <480)
movement();
if (zoomValue >520)
movement();
if (zoomValue <480)
movement();
//Serial.println (msg[1]);
delay(2000);
if ((digitalRead(input8)== LOW )&& (digitalRead(input4)== LOW )&& (digitalRead(input2)== LOW )&& (digitalRead(input1)== LOW )) {// this line checks for 1111 or F
sendMessage();
}
counter++;
delay(250);
}