Hi everyone! First of all, sorry for my english '-.-
I'm new in Arduino programming and I need help. My project is about turning on a number of relays with one Arduino Mega, and two Nanos. An android app (made with app inventor) will send data to my Arduino Mega. This is the first question:
Can I send data through OTG with App Inventor? How? I was using Bluetooth for that, but there are signal problems.
Once Arduino Mega receive the data, it will process that information. Mega will send information to Nano1 and Nano2 connected by Serial ports (TX1-RX1 and TX2-RX2):
#include "SoftwareSerial.h"
int rele[] = {22,23,24,25}; // for example.... I have more than 10 relays
const int ledPin = 13;
void setup(){
Serial.begin(9600); // For USB debugging
Serial1.begin(9600); // nano1
Serial2.begin(9600); // nano2
pinMode(rele[0], OUTPUT);
digitalWrite(rele[0], LOW);
pinMode(rele[1], OUTPUT);
digitalWrite(rele[1], LOW);
pinMode(rele[2], OUTPUT);
digitalWrite(rele[2], LOW);
pinMode(rele[3], OUTPUT);
digitalWrite(rele[3], LOW);
pinMode(ledPin, OUTPUT);
}
void loop(){
if (mensaje == "17a") {
digitalWrite(rele[0], HIGH);
Serial.println("Relay 1 activated!");
delay(20);
digitalWrite(rele[1], HIGH);
Serial.println("Relay 2 activated!");
delay(20);
}
if (mensaje == "17b") {
digitalWrite(rele[2], HIGH);
Serial.println("Relay 3 activated!");
delay(20);
digitalWrite(rele[3], HIGH);
Serial.println("Relay 4 activated!");
delay(20);
}
if (mensaje == "nan1A"){
Serial1.write("1A");
}
if (mensaje == "nan2A"){
Serial1.write("2A");
}
if (mensaje == "nan1B"){
Serial1.write("1B");
}
if (mensaje == "nan2B"){
Serial1.write("2B");
}
}
When one of the Arduino Nano receives that "orders" with Serial.readString(); they will execute their sketch (or part of it).
The second question: Is my code wrong? Is the serial communication right?
Thanks in advance