The idea is that I have a mega that's connected to micro that is connected to usb mini host that uses serial data.
I got the connection to work when it is PC<->Mega<->Micro<->PC. I can connect PC<->Mega<->USB (when connected to mega ofc) and PC<->Micro<->USB host.
Mega uses built-in serial and serial1. Micro uses built-in for connection to pc and softwareserial1 to mega and softwareserial2 to usb host.
Mega code
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600);
while(!Serial){}
Serial.println("Running");
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available()>0)
{
char in = Serial.read();
Serial1.write(in);
}
while(Serial1.available()>0)
{
char out=Serial1.read();
Serial.write(out);
}
}
Micro code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); //connection to Mega
SoftwareSerial mySerial2(8,9); //connection to MIni host
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial){}
mySerial.begin(9600);
mySerial2.begin(9600);
Serial.println("Running");
}
void loop() {
// put your main code here, to run repeatedly:
while(mySerial.available()>0)
{
char in = mySerial.read();
mySerial2.write(in);
Serial.write(in);
}
while(mySerial2.available()>0)
{
char out=mySerial2.read();
mySerial.write(out);
Serial.write(out);
}
}
When I send '?' or HELP to the usb mini host it responds with settings and information which I have been using to test the connection.
Like so:
Running
?
USB Host Ps3 Dual Shock Controller v1.02
SERIAL - Set Serial Data Output On/Off
(ON) - [ON|OFF]
HEX - Set Serial Data Output Hexadecimal On/Off
(ON) - [ON|OFF]
BAUD - Set Serial Port Baud Rate
[2400|4800|9600|14400|19200|38400|57600|115200]
I2C - Set I2C Address
(41 ) [1 - 127]
HELP or ? - display help
But as I said when I want to type into the Megas Serial and that should go to micro and then to the usb and back doesn't work. What gives?
The USB means a USB Mini host board, not USB Cable.
Used 2 software serials so I could also connect to the serial monitor on PC for troubleshooting.
When I get this to work I would like to replace wired connection between micro and mega to RF wireless. Or could I replace the whole micro with RF serial?
Should I switch software to connection between arduinos and hardware connect to the usb host?
tuomasjar:
Used 2 software serials so I could also connect to the serial monitor on PC for troubleshooting.
I tried to use hardware serial, but it didn't work either. I tried to switch to an UNO with same pins connected to no avail.
In the serial monitor it didn't show what I had sent when using micro/UNO as a middleman, but when I use MEGA it shows it double. And of course in other configurations it shows it once properly when testing connection.
I tried using hardware serial, but didn't get any response from micro to mega. Can I try to monitor the micro via USB while connected also to Mega? Or is there too much interference?
Should I use some other data type than char? or other metod than write?
First post was too long. Let me post a test at a time...
Test 1.
Connection basically is serial monitor - mega - micro - serial monitor
Code on the micro:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); //connection to Mega
void setup() {
// put your setup code here, to run once:
mySerial.begin(9600);
Serial.begin(9600);
while(!Serial){}
mySerial.println("Running Micro");
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available()>0)
{
char out=Serial.read();
mySerial.write(out);
}
while(mySerial.available()>0)
{
char in = mySerial.read();
Serial.write(in);
}
}
Code on the Mega:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600); //connection to Micro
// Serial2.begin(9600); //connection to USB Host
while(!Serial){}
Serial.println("Running MEGA");
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial1.available()>0)
{
char in = Serial1.read();
Serial.write(in);
}
while(Serial.available()>0)
{
char out=Serial.read();
Serial1.write(out);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //connection to computer
Serial1.begin(9600); //connection to Micro
Serial2.begin(9600); //connection to USB Host
while(!Serial){}
Serial.println("Running MEGA");
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial1.available()>0)
{
char in = Serial1.read();
Serial2.write(in);
Serial.write(in);
}
while(Serial2.available()>0)
{
char out=Serial2.read();
Serial1.write(out);
Serial.write(out);
}
}
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); //connection to Mega
void setup() {
// put your setup code here, to run once:
mySerial.begin(9600);
Serial.begin(9600);
while(!Serial){}
mySerial.println("Running Micro");
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available()>0)
{
char out=Serial.read();
mySerial.write(out);
}
while(mySerial.available()>0)
{
char in = mySerial.read();
Serial.write(in);
}
}
Mega Code:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600); //connection to Micro
// Serial2.begin(9600); //connection to USB Host
while(!Serial){}
Serial.println("Running MEGA");
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial1.available()>0)
{
char in = Serial1.read();
Serial.write(in);
}
while(Serial.available()>0)
{
char out=Serial.read();
Serial1.write(out);
}
}
On a Micro Serial is not connected to pins 0 and 1, it only works via the USB connector. Serial1 is what links via pins 0 and 1
...R
But it does mess up the upload if I have something connected to the pin 0 and 1. I mean when I try upload a sketch and I have something connected to the pins 0 and 1 for serial connection...
tuomasjar:
But it does mess up the upload if I have something connected to the pin 0 and 1. I mean when I try upload a sketch and I have something connected to the pins 0 and 1 for serial connection...
It's a while since I used my Micro but I'm pretty sure I was able to upload code with the Micro's pins 0 and 1 in use. I think it behaves like a Leonardo.