I have the following problem.
I am working with a LoRa Ra-02 Module, and I am using it as a transmitter receiver, the receiver has the task of moving 6 motres DC of 12V, for this I am using a 16-channel relay module, and with the relay modules, I am creating H bridges that allow me to control the rotation of the engine
Well, the relay module, to activate its coils, takes care of 12v, so the module has its own source of 12, likewise, the motors have their 12V power supply and the arduino is connected by the computer
What happens is the following, the relay module, without connecting the motors, works perfectly, it is controlled by the Arduino with the LoRa and without losing data, however, once the power source of the motors has been connected, and when trying To control the motors, the LoRa module loses connection and I don't explain myself because, if the module has a part of power that isolates the Arduino. It should be noted that these are linear motors.
Idk if it is problem of the code or the LoRa, maybe should be the relay module.
The code i am using is the next:
#include <Arduino.h>
#include <LoRa.h>
#include <SPI.h>
#include <Wire.h>
int rly1 = 14;
int rly2 = 15;
bool ON = 0;
bool OFF = 1;
char direc = 'r';
String outgoing;
byte msgCount = 0;
String Mymessage = "";
byte localAddress = 0xBB;
byte destination = 0xFF;
unsigned long previousMillis = 0;
const unsigned long interval = 1000;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
LoRa.setPins(53, 49); //NSS, RST
if (!LoRa.begin(433E6))
{
Serial.println("LoRa init failed. Check your connections.");
while (true);
// if failed, do nothing
}
Serial.println("LoRa init succeeded");
LoRa.setTxPower(13);
LoRa.setSignalBandwidth(250E3);
LoRa.setSpreadingFactor(7);
LoRa.setCodingRate4(5);
LoRa.setPreambleLength(12);
pinMode(rly1, OUTPUT);
pinMode(rly2, OUTPUT);
digitalWrite(rly1, OFF);
digitalWrite(rly2, OFF);
}
void sendMessage(String outgoing)
{
LoRa.beginPacket(); // start packet
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address
LoRa.write(msgCount); // add message ID
LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket(); // finish packet and send it
msgCount++; // increment message ID
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++)
{
if (data.charAt(i) == separator || i == maxIndex)
{
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void eje1(char direccion){
if(direccion == 'r'){
digitalWrite(rly1, OFF);
digitalWrite(rly2, ON);
}else if(direccion =='l'){
digitalWrite(rly2, OFF);
digitalWrite(rly1, ON);
}else{
digitalWrite(rly2, OFF);
digitalWrite(rly1, OFF);
}
}
/*
void eje2(char direccion){
if(direccion == 'r'){
digitalWrite(rly3, OFF);
digitalWrite(rly4, ON);
}else if(direccion =='l'){
digitalWrite(rly4, OFF);
digitalWrite(rly3, ON);
}else{
digitalWrite(rly4, OFF);
digitalWrite(rly3, OFF);
}
}
void eje3(char direccion){
if(direccion == 'r'){
digitalWrite(rly5, OFF);
digitalWrite(rly6, ON);
}else if(direccion =='l'){
digitalWrite(rly6, OFF);
digitalWrite(rly5, ON);
}else{
digitalWrite(rly6, OFF);
digitalWrite(rly5, OFF);
}
}
void eje4(char direccion){
if(direccion == 'r'){
digitalWrite(rly7, OFF);
digitalWrite(rly8, ON);
}else if(direccion =='l'){
digitalWrite(rly8, OFF);
digitalWrite(rly7, ON);
}else{
digitalWrite(rly8, OFF);
digitalWrite(rly7, OFF);
}
}
void eje5(char direccion){
if(direccion == 'r'){
digitalWrite(rly9, OFF);
digitalWrite(rly10, ON);
}else if(direccion =='l'){
digitalWrite(rly10, OFF);
digitalWrite(rly9, ON);
}else{
digitalWrite(rly10, OFF);
digitalWrite(rly9, OFF);
}
}
void eje6(char direccion){
if(direccion == 'r'){
digitalWrite(rly11, OFF);
digitalWrite(rly12, ON);
}else if(direccion =='l'){
digitalWrite(rly12, OFF);
digitalWrite(rly11, ON);
}else{
digitalWrite(rly12, OFF);
digitalWrite(rly11, OFF);
}
}
*/
void onReceive(int packetSize)
{
if (packetSize == 0){
return; // if there's no packet, return
}
// read packet header bytes:
int recipient = LoRa.read(); // recipient address
byte sender = LoRa.read(); // sender address
byte incomingMsgId = LoRa.read(); // incoming msg ID
byte incomingLength = LoRa.read(); // incoming msg length
String incoming = "";
while (LoRa.available())
{
incoming += (char)LoRa.read();
}
if (incomingLength != incoming.length())
{ // check length for error
Serial.println("error: message length does not match length");
return; // skip rest of function
}
// if the recipient isn't this device or broadcast,
if (recipient != localAddress && recipient != 0xBB)
{
Serial.println("This message is not for me.");
return; // skip rest of function
}
Serial.println("Message: " + incoming);
//-----------Lógica del programa--------------
//Para poder obtener el valor que se ocupa, se utiliza la función:
//<String> = getValue(incoming, ',', LugarDeDato);
//Después se tiene que pasar a enteron con:
//<int> = <String>.toInt();
String p0=getValue(incoming, ',',0);
String p1=getValue(incoming, ',',1);
String p2=getValue(incoming, ',',2);
String p3=getValue(incoming, ',',3);
String p4=getValue(incoming, ',',4);
String p5=getValue(incoming, ',',5);
String p6=getValue(incoming, ',',6);
String p7=getValue(incoming, ',',7);
String p8=getValue(incoming, ',',8);
String p9=getValue(incoming, ',',9);
String p10=getValue(incoming, ',',10);
String p11=getValue(incoming, ',',11);
String p12=getValue(incoming, ',',12);
String p13=getValue(incoming, ',',13);
String p14=getValue(incoming, ',',14);
String p15=getValue(incoming, ',',15);
String p16=getValue(incoming, ',',16);
String p17=getValue(incoming, ',',17);
String p18=getValue(incoming, ',',18);
int SlicePot = p0.toInt();
int joy1_V = p1.toInt();
int joy1_H = p2.toInt();
int db7 = p3.toInt();
int ub7 = p4.toInt();
int ub8 = p5.toInt();
int db8 = p6.toInt();
int db6 = p7.toInt();
int ub6 = p8.toInt();
int ub5 = p9.toInt();
int db5 = p10.toInt();
int db1 = p11.toInt();
int ub1 = p12.toInt();
int db2 = p13.toInt();
int ub2 = p14.toInt();
int db3 = p15.toInt();
int ub3 = p16.toInt();
int db4 = p17.toInt();
int ub4 = p18.toInt();
//--> EJE 1 BRAZO <--//
/*if(db1 == HIGH && ub1 == LOW){
eje1('r');
}
else if(ub1 == HIGH && db1 == LOW){
eje1('l');
}
else{
eje1('s');
}
//--> EJE 2 BRAZO <--//
if(db2 == HIGH){
eje2('r');
}
else if(ub2 == HIGH){
eje2('l');
}
else{
eje2('s');
}
//--> EJE 3 BRAZO <--//
if(db3 == HIGH){
eje3('r');
}
else if(ub3 == HIGH){
eje3('l');
}
else{
eje3('s');
}
//--> EJE 4 BRAZO <--//
if(db4 == HIGH){
eje4('r');
}
else if(ub4 == HIGH){
eje4('l');
}
else{
eje4('s');
}
//--> EJE 5 BRAZO <--//
if(db5 == HIGH){
eje5('r');
}
else if(ub5 == HIGH){
eje5('l');
}
else{
eje5('s');
}
//--> EJE 6 BRAZO <--//
if(db6 == HIGH){
eje6('r');
}
else if(ub6 == HIGH){
eje6('l');
}
else{
eje6('s');
}
*/
}
void loop()
{
onReceive(LoRa.parsePacket());
unsigned long currentMillis = millis(); // Obtiene el tiempo actual en milisegundos
// Verifica si ha pasado el intervalo de tiempo deseado
if (currentMillis - previousMillis >= interval) {
// Guarda el tiempo actual como referencia para la próxima vez
previousMillis = currentMillis;
// Llama a la función con la dirección actual
eje1(direc);
// Cambia la dirección para la siguiente acción
if (direc == 'r') {
direc = 'l'; // Cambia a 'l' si estaba en 'r'
} else if (direc == 'l') {
direc = 's'; // Cambia a 's' si estaba en 'l'
} else {
direc = 'r'; // Cambia a 'r' si estaba en 's' (o cualquier otro valor)
}
}
}

