I am working on project that involves a master arduino sending data to a slave arduinoMega. Each of them has sensors that detect stuff.
The code on the master senses an event and sends data via serial to the slave. The slave runs its own code (reading the sensors), but I need it to able read the incoming data from the master and do something with. I tried using serialEvent, but it does not work.
The slave code runs the normal routine of the reading the sensors, but does not run the case where it needs to do something with the serial data from the master.
Attached below are the codes.
Which serial port of the slave is used to receive data from the master ?
Unless it is the main Serial port then serialEvent() will not be run. For other serial ports on the mega there are corresponding serialEvent() functions named serial1Event() etc
However, I have never seen the point of the serialEvent functions. Why not just check whether data is available yourself and act on it ?
// The Event Report
if(millis() - start <= 30000){
That if statement will be true for the first 30 seconds after reset and false forever after that. Is that what you intend? The start variable is never updated. See the blink without delay example to see how to use millis() for timing.
The serial input basics thread has information that will help you to send the data and reliably receive them on the other end without using the String class.
I am using the main serial on the mega, not the other ports...
I recommend that you use one of the extra serial ports on the Mega if you can. Having the main Serial to print debug messages to serial monitor is very valuable.
Many people think that serial event is an interrupt type of function. It is not. Putting serial.available in loop() so that it checks the serial buffer every time through loop() does exactly the same thing.
I had it at the beginning of the loop though, I will change it and run again. Thanks.
Beginning or end of loop() should make no difference. In fact it can be anywhere in loop() or a function called from loop() as long as free running of the code is not prevented.
I see in your code that you have delay()s so free running of the code is being prevented and you also use Serial.readString() which I believe is a blocking function
May I suggest that you write a small sketch that only sends some hard coded data from one Arduino to the other. That removes all of the non-essential code and makes debugging easier. Once that you have the serial comms working, add in the code for sending real data a piece at a time. It is much easier to troubleshoot when adding code incrementally. Then you know when it goes south and have a better idea of what caused the problem.
Please look at the serial input basics tutorial. Using the start/end marker method will make communication more reliable. And not using the String class will make the program smaller and more reliable in the long term.
groundFungus:
May I suggest that you write a small sketch that only sends some hard coded data from one Arduino to the other. That removes all of the non-essential code and makes debugging easier. Once that you have the serial comms working, add in the code for sending real data a piece at a time. It is much easier to troubleshoot when adding code incrementally. Then you know when it goes south and have a better idea of what caused the problem.
Please look at the serial input basics tutorial. Using the start/end marker method will make communication more reliable. And not using the String class will make the program smaller and more reliable in the long term.
Thanks for the recommendation...
The issue is the slave Mega further sends the data into LabVIEW, thats why I am using strings, for it is easier that way in LabVIEW.
Chidore:
What class of data can I use as value1...value2..., strings, chars, or int...
Any of them.
But it would help to know what you want to do when you receive the data. Post an example of the sort of message you want to send and explain how it will be used.
But it would help to know what you want to do when you receive the data. Post an example of the sort of message you want to send and explain how it will be used.
...R
Thanks @ Robin2,
I want to send a location (x,y) and a flag (1 or 0). Something like this
Sorry I used pseudocode...
On the other side, I want to receive these data piece by piece, in sequential order, x_loc, y_loc and the flags.
I having been using strings because, when I receive the data in the second arduino, and further send them into LabVIEW where strings are the most preferred way to receive data.
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. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
It will be very much simpler if you send all the data in a single message. The example I posted in Reply #2 was this