Hello everyone i have this problem where i need to transmit very small amount of data but fast,
im using the Lora sx1278 e32 to do this. i need to transmit 10 bytes which need to take around 10.4ms at 9600 baud rate which means sending almost 100 times per second but in practice its only sending like once a second.
when i shorted the modules (RX to TX make it wired connection) it worked as it should.
so IMO the problem is with the modules but i dont know how to check it/solve it
these are my codes
thanks in advance
for the transmitter:
#include <SoftwareSerial.h>
#define TX 3
#define RX 2
#define M0 13
#define M1 12
#define AUX 11
#define communicationLed 8
unsigned long time;
byte myData[10];
SoftwareSerial ESerial(TX, RX);
void setup(){
Serial.begin(9600);
ESerial.begin(9600);
Serial.println("Starting Sender");
pinMode(4, INPUT_PULLUP); //button pull-up resistor
pinMode(5, INPUT_PULLUP); //button pull-up resistor
pinMode(6, INPUT_PULLUP); //button pull-up resistor
pinMode(7, INPUT_PULLUP); //button pull-up resistor
pinMode(communicationLed, OUTPUT); //communication Led config
pinMode(M0, OUTPUT); //lora module config
pinMode(M1, OUTPUT); //lora module config
digitalWrite(M0, LOW); //lora module config
digitalWrite(M1, LOW); //lora module config
//sync bytes
myData[9] = 0xff; // end byte
myData[0] = 0xfe; //start byte
}
void loop(){ //taking about 11.3ms for a loop
// Read all analog inputs and map them to one Byte value
// Convert the analog read value from 0 to 1023
//getTime(); // print loop duration
myData[1] = map(analogRead(A1), 0, 1023, 0, 180); //j1posX
myData[2] = map(analogRead(A0), 0, 1023, 0, 180); //j1posY
myData[3] = map(analogRead(A3), 0, 1023, 0, 180); //j2posY
myData[4] = map(analogRead(A2), 0, 1023, 0, 180); //j2posX
if (digitalRead(4)) myData[5] = 0; else myData[5]= 1;
if (digitalRead(5)) myData[6] = 0; else myData[6] = 1;
if (digitalRead(6)) myData[7] = 0; else myData[7] = 1;
if (digitalRead(7)) myData[8] = 0; else myData[8] = 1;
ESerial.write(myData, 10);
}
void getTime(){
Serial.print("Time: ");
time = millis();
Serial.println(time);
}
for the receiver:
boolean newData = false;
byte dataReceived[8];
int i=0;
unsigned long lastReceiveTime;
byte startByte = 0xfe, endByte = 0xff;
static boolean recvInProgress = false;
#define led1 12
#define debugLed 13
#include <Servo.h>
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
Servo servo3; // create servo object to control a servo
Servo servo4; // create servo object to control a servo
Servo esc; // create servo object to control a servo
//============
void setup() {
Serial.begin(9600);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(4, LOW); //lora module config
digitalWrite(5, LOW); //lora module config
pinMode(6, INPUT);
pinMode(led1, OUTPUT);
pinMode(debugLed, OUTPUT);
servo1.attach(7); // attaches the servo on pin 4 to the servo object
servo2.attach(8); // attaches the servo on pin 5 to the servo object
servo3.attach(9); // attaches the servo on pin 6 to the servo object
servo4.attach(10); // attaches the servo on pin 7 to the servo object
esc.attach(11); // attaches the servo on pin 7 to the servo object
resetData();
}
//============
void loop() {
/*
* The function is reading incoming bytes from the serial, working as long as we dont have new data,
* its start to read when its reading 0xfe as we declared its our start byte (for sync reasons)
* and every byte afterwards its placing into an array of data until it read the endByte and stops
* then updating newData => true and moving to run the motors
*/
recvData();
if(newData){runMotors();}
}
void resetData() {
// Reset the values when there is no radio connection - Set initial default values
dataReceived[1] = 90; //j1posX
dataReceived[2] = 0; //esc
dataReceived[3] = 90; //j2posY
dataReceived[4] = 90; //j2posX
dataReceived[5] = 0;
dataReceived[6] = 0;
dataReceived[7] = 0;
dataReceived[8] = 0;
}
void recvData(){
while (Serial.available() > 0 && newData == false){ //read data into array
byte currentByte = Serial.read();
if (recvInProgress == true) {
if (currentByte != endByte) {
dataReceived[i] = currentByte;
i++;
}
else {
recvInProgress = false;
i = 0;
newData = true;
}
}
else if (currentByte == startByte) {
recvInProgress = true;
}
}
}
void runMotors(){
servo1.write(dataReceived[1]);
//servo2.write(j1potX);
//servo3.write(j2potY);
//Serial.println(j2potX);
//servo4.write(j2potX);
esc.writeMicroseconds(map(dataReceived[0], 0, 180, 0, 2000));
if(dataReceived[4]) digitalWrite(led1,HIGH); else digitalWrite(led1,LOW);
//delay(3);
newData = false;
}