Basic Arduino serial communication - arduino

I'm just trying to get the very basics of serial communication started; I'm trying to use this example I found, from what I understand it should be working. I just want what I type into the serial monitor to be output back, so I can see how it works. I also tried removing the while serial.available in case the serial monitor doesn't trigger that condition.
Here is my code:
// Buffer to store incoming commands from serial port
String inData;
void setup() {
Serial.begin(9600);
Serial.println("Initialized\n");
}
void loop() {
while (Serial.available() > 0)
{
char received = Serial.read();
inData += received;
// Process message when new line character is received
if (received == '\n')
{
Serial.println("Arduino Received: ");
Serial.println(inData);
inData = ""; // Clear received buffer
}
}
}
It currently uploads fine, and prints "initialized", but it doesn't work if I try to "send" any data.

Serial.read() returns an int.
You need to cast to (char) in order to store it as a char.
char received = (char)Serial.read();
Maybe you are never receiving any data for some reason.
Let's try something super simple. Use serialEvent() as suggested by sohnryang and then print some text as soon as Serial.available() triggers:
while (Serial.available() > 0) {
Serial.println("Something has been received");
}
You should see this message every time you send something to Arduino.

Use SerialEvent. So the code will look like this.
String inData;
void setup() {
Serial.begin(9600);
Serial.println("Initialized\n");
}
void loop() {
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inData += inChar;
if (inChar == '\n') {
Serial.println("Arduino Recieved : ");
Serial.println("inData");
inData = "";
}
}
}

Related

How to make a response in Bluetooth module (Arduino)

I connected my Bluetooth module to the mobile phone and made up a code to communicate between Arduino and mobile through Bluetooth (send messages from Bluetooth module to device and vice versa).
Now I want to make a response, which means that if I send from the mobile "hi" the arduino replies and says "Hello" or whatever.
I have tried tons of codes but none worked, so would anyone please help me?
#include <SoftwareSerial.h>
SoftwareSerial myserial (6,5);
void setup() {
myserial.begin(9600);
Serial.begin (9600);
}
void loop() {
if (myserial.available()) {
Serial.write(+ myserial.read());
}
if (Serial.available()) {
myserial.write(Serial.read());
}
}
Another code but making a loop without sending anything
#include <SoftwareSerial.h>
SoftwareSerial myserial(6,5); //Arduino: R:5,T:6; bluetooth: T:5, R:6;
void setup() {
myserial.begin(9600);
Serial.begin (9600);
}
void loop() {
if (myserial.available()) {
Serial.write(myserial.read());
}
if (Serial.available()) {
myserial.write(Serial.read());
}
for (int i = 0; i=2; i++) {
myserial.write("hello");
}
if (myserial.read() =="n") {
myserial.write("hello");
}
}
You need to build your serial string char by char using the SerialEvent() interrupt, then do a String comparison using the .equals method. Despite many people will tell you that String types are 'evil' (see this and this) it might be a good solution for making things clearer (and perhaps a bit easier if you don't want to mess with C char strcmp() functions and pointers. In the end, the String type is there for you and I see no reason for not using it in general projects.
Based on the SerialEvent() documentation [1], and the String reference [2], you could do something like:
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
//Then you compare the inputString with the word you want to detect using the .equals method from the String class
if(inputString.equals("Hi"){
Serial.println("Hello");
}
// clear the string for a new comparison:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
EDIT 1: Of course, you can replace the Serial object from this example code with your own myserial object from the BlueTooth communication.

Serial communication between two arduino

I am trying to write a Serial read function. That function will give me a data between '#' (start character) and '*' (end character). I tried to write it and it looks like it is kinda work but not perfectly. The problem is that:
I have two arduino. One of these send "MARCO" and other arduino read it. If the readed data is "MARCO" it is write to serial monitor "MARCOCORRECT" else it is write to serial monitor the readed data. Normally it must just write "MARCOCORRECT" because I only send "MARCO" but it don't. It writes something else too. I tried lower baud rate too but it is still same. How can I fix it?
Sender Code
#define BAUD_RATE 38400
void setup()
{
Serial.begin(BAUD_RATE);
}
String readed = "";
void loop()
{
String readed;
while (Serial.available() > 0)
{
readed += Serial.read();
}
Serial.println("#MARCO*");
}
Reader Code
#define BAUD_RATE 38400
#define MSG_START '#'
#define MSG_END '*'
String readed;
char readedChar;
bool msgStart = false;
String serialReadFunc()
{
readedChar = '0';
readed = "";
while (Serial.available() > 0 || msgStart)
{
if (readedChar == MSG_START)
{
msgStart = true;
}
readedChar = (char)Serial.read();
if (readedChar == MSG_END)
{
msgStart = false;
break;
}
if (msgStart)
{
readed += readedChar;
}
}
return readed;
}
void setup()
{
Serial.begin(BAUD_RATE);
}
void loop()
{
if (serialReadFunc() == "MARCO")
{
Serial.println("MARCOCORRECT");
}
else
Serial.println(readed);
}
Console Image On Proteus
Console Image Proteus
I suspect you're having synchronization issues. I may be wrong, though, and I'm unable to test it at the moment.
I'd recommend trying to insert a delay on the sender, like so:
String readed;
while (Serial.available() > 0)
{
readed += Serial.read();
}
delay(10);
Serial.println("#MARCO*");
It would also be interesting to see the return value of the reader Serial.available(). Again, not 100% sure, but I believe the buffer may be full (the buffer holds 64 bytes).

Arduino serial input value handling

If you enter M in the Arduino serial input, It has to wait to receive the new value in the corresponding if statement, but the contents inside the if statement will be executed without waiting.
void Mouse_control(int i){
Serial.print(i);
if( i ==1){
delay(5000);
Mouse.press(MOUSE_LEFT);
delay(100);
Mouse.release(MOUSE_LEFT);
delay(200);
}
}
void loop() {
// check for incoming serial data:
while (Serial.available() == 0) {}
char inChar = Serial.read();
// read incoming serial data:
if(inChar == 'M'){
while (Serial.available() == 0) {}
Mouse_control(Serial.parseInt());
}
}
I got a hint from comments and was able to solve the problem.
The problem was in dealing with \ n, which required processing at the input.
String char_process()
{
while(Serial.available()==0){}
String C;
String str = Serial.readStringUntil('\n');
C=str[0];
return C;
}
As you noticed, the problem is '\n'. For a more reliable code, use isspace in standard library to ignore all incoming unprintable characters:
char c;
do
{
c = Serial.read();
} while (isspace(c));
Then parse input characters into int.

Arduino Serial communication output

I have 2 Arduinos Leonardo and I want them to communicate itself, so I did the following code:
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
String outMessage = ""; // String to hold input
while (Serial.available() > 0) { // check if at least 1 char is available
char inChar = Serial.read();
outMessage.concat(inChar); // add inChar to outMessage
}
if (outMessage != "") {
Serial.println("Sent: " + outMessage); // View Arduino 1 in Serial Monitor 1
Serial1.print(outMessage); // Send to Arduino 2
}
while (Serial1.available() > 0) {
Serial.print("Received: "); // View Arduino 1 in Serial Monitor 2
Serial.print(Serial1.read()); // Received from Arduino 1
Serial.println();
}
}
I want to send a message from Arduino 1, print in Serial Monitor and send via TX1 to Arduino 2 and vice-versa. The problem is that I don't receive what I was expecting. For instance if I type test:
Arduino 1:
Sent: test
Arduino 2:
Received: t
Received: e
Received: s
Received: t
I also tryed to do the receiving side like the sending side and use Serial.write but with no sucess.
Is there a easier way to do that or to fix it?
Thanks
Has mentioned by Hans, you need a protocol.
This is what I use to consider a message in Arduino to be a complete message:
char inData[10];
int index;
boolean started = false;
boolean ended = false;
String message =("I am Arduino 1 and I am ready");
void setup(){
Serial.begin(9600);
Serial.println(message);
}
void loop()
{
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '>')
{
started = true;
index = 0;
inData[index] = '\0';
}
else if(aChar == '<')
{
ended = true;
}
else if(started)
{
inData[index] = aChar;
index++;
inData[index] = '\0';
}
}
if(started && ended)
{
int inInt = atoi(inData);
Serial.println(inInt);
}
// Get ready for the next time
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
So, basically a message is considered completed only if it is between the special characters ><, like this: >message<. Then you can do the same on reading.
It does not have to be too complicated. If you look carefully at your last whlie-loop you can see that the software does not get a chance to read more than one character each time it passes through the loop. So that is what you get: one character at a time.
In your first while-loop you did better: you collected all the incoming letters until nothing was available and then sent them all at once. So if you make your last loop look more like the first one, you'll get a better result.
As mentioned a protocol to frame messages is needed between devices. A quick way to do this is to use Bill Porter's EasyTransfer library which does exactly what you are trying to do, over either UART or I2C. It has several examples.
Serial.read() reads only one byte every time you use it. A simple solution would be to store each byte on a char array while Serial.available>0 and then print the String with the whole message that was sent.
char message[40];
int count = 0;
while(Serial.available()>0){
message[count++] = Serial.read();
}
Serial.println(message);

How to transmit a String on Arduino?

I want 2 Arduinos Leonardo to communicate, send a string for instance, so I have to use Serial1 to communicate via RS232 on pins 0 (RX) and 1 (TX).
I need to write binary data in that pins, the problem is how can I send a String using Serial1.write. Serial1.print works without errors but I think it does not do what I want. Any suggestion?
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
while (!Serial); // while not open, do nothing. Needed for Leonardo only
}
void loop() {
String outMessage = ""; // String to hold input
while (Serial.available() > 0) { // check if at least one char is available
char inChar = Serial.read();
outMessage.concat(inChar); // add Chars to outMessage (concatenate)
}
if (outMessage != "") {
Serial.println("Sent: " + outMessage); // see in Serial Monitor
Serial1.write(outMessage); // Send to the other Arduino
}
}
this line Serial1.write(outMessage); is giving me the error
"no matching function for call to 'HardwareSerial::write(String&)'"
You're using the String object(Wiring/C++). The function is using C strings: Serial.write(char*). To turn it into a C string, you use the toCharArray() method.
char* cString = (char*) malloc(sizeof(char)*(outMessage.length() + 1);
outMessage.stoCharArray(cString, outMessage.length() + 1);
Serial1.write(cString);
If we do not allocate the memory for our C string with malloc, we will get a fault. The following code WILL crash.
void setup() {
Serial.begin(9600);
String myString = "This is some new text";
char* buf;
Serial.println("Using toCharArray");
myString.toCharArray(buf, myString.length()+1); // **CRASH** buf is not allocated!
Serial.println(buf);
}
void loop() {
// put your main code here, to run repeatedly:
}
In the Serial Monitor the only message we will get is: Using toCharArray. At that point execution stops. Now if we correct the problem and use malloc() to allocate memory for our buffer and also use free() when done....
void setup() {
Serial.begin(9600);
String myString = "This is some new text";
char* buf = (char*) malloc(sizeof(char)*myString.length()+1);
Serial.println("Using toCharArray");
myString.toCharArray(buf, myString.length()+1);
Serial.println(buf);
Serial.println("Freeing the memory");
free(buf);
Serial.println("No leaking!");
}
void loop() {
// put your main code here, to run repeatedly:
}
The output we see in the Serial Monitor is:
Using toCharArray
This is some new text
Freeing the memory
No leaking!
Use toCharArry(), write() uses char*, not string, here is what i mean:
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
while (!Serial);
}
void loop() {
String outMessage = "";
while (Serial.available() > 0) {
char inChar = Serial.read();
outMessage.concat(inChar);
}
if (outMessage != "") {
Serial.println("Sent: " + outMessage);
char* CharString; //
outMessage.toCharArray(cString, outMessage.length()) // My Changes Are Here
Serial1.write(CharString); //
}
}

Resources