Hello everyone,
apologies if this has come up before but all I could find on the forum was either vague and fuzzy or punching way above my weight.
I am trying to send and receive serial data from a few Arduino for a project. In the project, which is to be dealt with later, the Arduinos will be daisy chained, like in this project by Scott Lawrence:
so the communication should be via serial, not via USB.
I have so far written a tiny sketch to check just if the Arduinos are connected and respond on the serial monitor. I am using an Arduino Uno and an ATtiny167 o a breakout board. The serial communication is a Arduino USB2Serial.
(this one: Accessories — Arduino Official Store)
As you can see in the little sketch below, all that happens is an echo command, and a simple counting up, so the number it goes up should match the number of chain elements (if all goes well).
So far, both chain elements work perfectly well individually, but in the daisy chain, I do not get any output on the serial monitor anymore.
Does anyone have an idea what is going on?
Thanks a lot for taking your time on this and for all answer.
String instruction;
String processed;
int number;
void setup() {
Serial.begin(19200);
}
void loop() {
while (Serial.available() == 0) {
//do nothing
}
if (Serial.available() > 0) {
instruction = Serial.readStringUntil('\n');
if (instruction == "echo") {
Serial.println("echo");
}
else if (instruction.startsWith("count ") ) {
processed = instruction;
processed.remove(0, 6);
number = processed.toInt();
number++;
Serial.print("count ");
Serial.println(number);
}
}
}
Edit: You can unsee the while loop, I thought the Arduinos might miss my commands, but it didn't help.
while (Serial.available() == 0) {
//do nothing
}
if (Serial.available() > 0) {
What is going to cause the while loop to end? The arrival of at least one byte of serial data, of course. So, why on earth is it necessary to test that that happened?
The above diagram shows how four Arduino boards are wired for this.
No, the above diagram shows a blizzard on a cloudy day.
How DO you have the Arduinos wired?
I am building something with a similar requirement at the moment and I am connecting the master Tx to the RXs of all the slaves so they all receive the same data and there is no need for the complexity of Arduino B passing a message on to Arduino C.
With a few extra external components I can also connect all the slave TX pins to the master Rx and have 2-way communication.
In my system the master will send a message that includes a slave ID and when the slave detects its ID in the message {A} it knows the message is for it and {B} it knows it has permission to reply.
but in the daisy chain, I do not get any output on the serial monitor anymore.
I don't understand that. What exactly have you tried - how are things connected?
I presume you are aware that the Rx and Tx pins on an Uno are used as part of the USB communication with the PC.
Also it is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
...R
Serial Input Basics - simple reliable ways to receive data.
Robin2:
I am building something with a similar requirement at the moment and I am connecting the master Tx to the RXs of all the slaves so they all receive the same data and there is no need for the complexity of Arduino B passing a message on to Arduino C.
With a few extra external components I can also connect all the slave TX pins to the master Rx and have 2-way communication.
In my system the master will send a message that includes a slave ID and when the slave detects its ID in the message {A} it knows the message is for it and {B} it knows it has permission to reply.
I don't understand that. What exactly have you tried - how are things connected?
I presume you are aware that the Rx and Tx pins on an Uno are used as part of the USB communication with the PC.
Also it is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
...R
Serial Input Basics - simple reliable ways to receive data.
He doesn't know what is doing, so don't ask him questions.
He is parroting the "design" in the linked project which the person never completed!
.
As I said, you can unsee the while loop, it was a hopeless attempt to do... something I guess.
Also, that Site I am pointing to is not by me, but by a certain Scott Lawrence.
Apparently, he made that contraption work, although he seems to be the only one, given every google search ends up on that site somehow.
I connected my Arduinos exactly like his on that picture (not sure what is unclear about it to you?), so
Tx from Serial Port goes to Rx of uC1, Tx of uC1 to Rx of uC2, Tx of uC2 to Rx of the Serial Port.
In theory the microcontroller should read the serial command, pick out what is meant for it, and pass on the rest. The last in the chain communicates back to the user, as the unknown/mistyped commands all end up here.
Robin2:
I am building something with a similar requirement at the moment and I am connecting the master Tx to the RXs of all the slaves so they all receive the same data and there is no need for the complexity of Arduino B passing a message on to Arduino C.
That is not going to work for me, because I need some Arduinos to pass communicate "downstream".
Robin2:
I presume you are aware that the Rx and Tx pins on an Uno are used as part of the USB communication with the PC.
The USB is disconnected on the Arduinos, and with one single Arduino, connecting pins 0, 1 and Gnd to the Serial Adapter, it works fine.
Robin2:
Also it is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
Can I perform string operations on those? Mainly the 3 things you see in the sketch are needed: starts with, chop off the beginning and filter out the numbers. I remember string operations from University, but that was "real" C++ (not Arduino), and it was tought more or less badly.
You seem to be creating a primitive "token ring network". To get it to work the way you want, you need to pass a "token" around your ring so each Arduino can know when to send a message it is originating.
Paul
Perlmarmelade:
Can I perform string operations on those?
I gave you a link to documentation for the functions that work on cstrings.
...R
Robin2:
I gave you a link to documentation for the functions that work on cstrings.
...R
Thank you, very much appreciated