Arduino for loop not running - arduino

What I am trying to do is to convert a string from the serial input into individual words by detecting spaces between the words:
String input;
char split[]{};
String output;
String product;
int inputSize;
void setup() {
Serial.begin(9600);
Serial.println("ready");
}
void loop() {
if (Serial.available() > 0) {
input = Serial.readString();
input.toCharArray(split, 8);
inputSize = sizeof(input);
for (int i = 0; i < inputSize; i++){
if (isSpace(split[i])) {
product = output;
output = "";
Serial.println(product);
}else{
output = output + split[i];
}
}
}
}
I am trying to do is to convert a string from the serial input into
individual words by detecting spaces between the words it should
print all words sent to the serial monitor separated into different
strings.
However sometimes it sends one word from the input string and
sometimes it doesn't send and it piles up and sends multiple words
without spaces at once

Use the following code, you will get the string split into the words
String output;
void setup() {
Serial.begin(9600);
Serial.println("ready");
}
void loop() {
if (Serial.available() > 0) {
char input = Serial.read();
if((input == ' ')){
Serial.println(output);
output = "";
}else
output+=input;
}
}

Related

Arduino - Processing and serial communication

I need to get a serial signal from the Arduino board into a Processing sketch. I am sending values in string format, like "R12" "S40" "T80". When I capture those strings from Processing it will printing vertically (new line for each character). How can I get this as a string as it is what we sent?
I need code that can be put inside Processing's serialEvent method.
Arduino code
void loop() {
int r = random(10, 100);
int s = random(10, 100);
int t = random(10, 100);
Serial.print("R" + String(r));
Serial.print("S" + String(s));
Serial.print("T" + String(t));
delay(1000);
}
Processing Code
String data = "";
int R, S, T;
void serialEvent(Serial p) {
while(p.available() > 0) {
data = p.readString();
}
println(data);
}
I need to get the last received R S T values, respectively, to the R S T variables.
As mentioned previously, it is better to get the all the code to find the problem.
I suppose you did not add the function "bufferUntil()" in the Processing setup, so each time the serial received a value, it just displays it. I can propose you a solution. It is not the only one, but it seems to work.
In your Arduino code, instead of the Serial.print(), you should use Serial.println() to delimit your message.
void setup() {
Serial.begin(9600);
}
void loop() {
int r = random(10, 100);
int s = random(10, 100);
int t = random(10, 100);
Serial.println("R" + String(r));
Serial.println("S" + String(s));
Serial.println("T" + String(t));
delay(1000);
}
So you will send the message as
Rxx
Sxx
Txx
In Processing, you have to use the function "bufferUntil(lf)". It will save all messages from the serial port until it receives a "/n".
After that, you should use another function to associate your identifier with the value.
// Example by Tom Igoe
import processing.serial.*;
Serial myPort; // The serial port
PFont myFont; // The display font
String inString; // Input string from serial port
int lf = 10; // ASCII linefeed
String data = "";
int R, S, T;
void setup() {
size(400, 200);
// List all the available serial ports:
printArray(Serial.list());
// Open whatever port is the one you're using, mine is one.
myPort = new Serial(this, Serial.list()[1], 9600);
myPort.bufferUntil(lf); // Delimiter of serial buffer end line
}
void draw() {
background(0);
text("received: " + " R=" + R + " S=" + S + " T=" + T, 10, 50); // Display value on the window
}
void serialEvent(Serial p) {
while(p.available() > 0) {
String buf = p.readString(); // Read serial input
println(buf);
if(!buf.equals('\n')) // Delete null='/n' from arduino println()
data = buf; // Save buffer
}
/*
char id = data.charAt(0); // Get id from buffer
int value = parseInt(data.substring(1, 3)); // Get the value from buffer
identifyVariable(id, value); // Associate id and value
*/
}
void identifyVariable(char ID, int value) {
switch(ID) { // Associate id with value
case('R'):
R = value;
break;
case('S'):
S = value;
break;
case('T'):
T = value;
break;
default:
println("error " + ID + " " + value);
break;
}
}

Can not add line feed to string

I have an Arduino Leonardo and trying to use it as a serial to USB converter. On Serial1 I have a string ending on a number. This number I'm trying to get via USB to the PC. It works very fine but I need a '\n' at the end and I don't know how. When I try it in the line Keyboard.println or Keyboard.write, I get a various number of lines with the expected number in splitted.
#include <Keyboard.h>
String myEAN ="";
const int myPuffergrosse = 50;
char serialBuffer[myPuffergrosse];
void setup() {
Keyboard.begin();
Serial1.begin(9600);
delay(1000);
}
String getEAN (char *stringWithInt)
// returns a number from the string (positive numbers only!)
{
char *tail;
// skip non-digits
while ((!isdigit (*stringWithInt))&&(*stringWithInt!=0)) stringWithInt++;
return(stringWithInt);
}
void loop() {
// Puffer mit Nullbytes fuellen und dadurch loeschen
memset(serialBuffer,0,sizeof(myPuffergrosse));
if ( Serial1.available() ) {
int incount = 0;
while (Serial1.available()) {
serialBuffer[incount++] = Serial1.read();
}
serialBuffer[incount] = '\0'; // puts an end on the string
myEAN=getEAN(serialBuffer);
//Keyboard.write(0x0d); // that's a CR
//Keyboard.write(0x0a); // that's a LF
}
}
Since myEAN is a String, simply add the character...
myEAN += '\n';
Or, for a full carriage return/line feed combination:
myEAN += "\r\n";
See the doc: https://www.arduino.cc/en/Tutorial/StringAppendOperator
I suggest you use String in your getEAN function as well...
String getEAN(String s)
{
// returns the first positive integer found in the string.
int first, last;
for (first = 0; first < s.length(); ++first)
{
if ('0' <= s[first] && s[first] <= '9')
break;
}
if (first >= s.length())
return "";
// remove trailing non-numeric chars.
for (last = first + 1; last < s.length(); ++last)
{
if (s[last] < '0' || '9' < s[last])
break;
}
return s.substring(first, last - 1);
}

Device not responding to UART commands

I am using an Arduino mega2560 and an EZO EC(Electrical Conductivity) and am trying to send a command using the Serial.print() function. I am using the Arduino IDE 1.6.7.
I have some code that seems to work fine which I found online. But I want to know why my code will not work. The EC sensor does not seem to get the data I am sending. No data seems to being sent.
I know it is not my connection as I have tested the setup with the code that works it runs fine and as expected.
Here is the code I found online that works:
String inputstring = "";
String sensorstring = "";
boolean input_string_complete = false;
boolean sensor_string_complete = false;
void setup() {
Serial.begin(9600);
Serial3.begin(9600);
inputstring.reserve(10);
sensorstring.reserve(30);
}
void serialEvent() {
inputstring = Serial.readStringUntil(13);
input_string_complete = true;
}
void serialEvent3() {
sensorstring = Serial3.readStringUntil(13);
sensor_string_complete = true;
}
void loop() {
float wt = 28.9;
String tempCal = "T,";
tempCal += wt;
if (input_string_complete == true) {
Serial3.print(inputstring);
Serial3.print("\r");
inputstring = "";
input_string_complete = false;
}
if (sensor_string_complete == true) {
if (isdigit(sensorstring[0]) == false) {
Serial.println(sensorstring);
}
else
print_EC_data();
}
sensorstring = "";
sensor_string_complete = false;
}
}
void print_EC_data(void) {
char sensorstring_array[30];
char *EC;
char *TDS;
char *SAL;
char *GRAV;
float f_ec;
sensorstring.toCharArray(sensorstring_array, 30);
EC = strtok(sensorstring_array, ",");
TDS = strtok(NULL, ",");
SAL = strtok(NULL, ",");
GRAV = strtok(NULL, ",");
Serial.print("EC:");
Serial.println(EC);
Serial.print("TDS:");
Serial.println(TDS);
Serial.print("SAL:");
Serial.println(SAL);
Serial.print("GRAV:");
Serial.println(GRAV);
Serial.println();
//f_ec= atof(EC);
}
Here is my code:
void setup() {
Serial.begin(9600);
Serial3.print(9600);
}
void loop() {
Serial3.print("R/r");
Serial.print("R/r");
delay(2000);
}
The Serial3.print just doesn't get sent to the sensor. But the other code also sends a string using the Serial3.print() function an it works fine. I do not know what I am doing wrong.
I understand that I need to write a procedure to take in anything that comes in from the sensor. But nothing seems to be even sent to the sensor in the first place!
Any help would be greatly appreciated. Thank you
You're using slash, not backslash. Change this
Serial3.print("R/r");
to this:
Serial3.print("R\r");
And don't use the String class. It'll mess you up. :) Just use char arrays, and fill them up as characters are available in loop. When the \r finally arrives, process the array:
char inputString[16];
int inputStringIndex = 0;
void loop()
{
if (Serial.available()) {
char c = Serial.read();
if (c == '\r') {
inputString[ inputStringIndex ] = '\0'; // NUL-terminate string
inputStringIndex = 0; // reset for next time
Serial3.print( inputString );
Serial3.print( '\r' );
} else if (inputStringIndex < sizeof(inputString)-1) {
inputString[ inputStringIndex++ ] = c;
}
}
Do something similar for the response line. This will also avoid blocking inside a SerialEvent. :P
You have an error in your setup() block: before you can send data over a serial connection you need to configure the connection with a begin() statement:
Serial3.begin(9600)
But in the code in your question you have
Serial3.print(9600)
And make sure that you have the EZO connected to the correct pins for Serial3 (14,15).
Also you need to use the "\" for printing control characters.

Arduino serial input string spread over several lines

First of all, sorry for the confusing title. I'm trying to make a simple program on my arduino that echos the serial input received from the serial monitor. My code is this:
String string= "";
String string2 = "";
void setup()
{
Serial.begin(9600);
}
void loop() {
string = "";
while(Serial.available() > 0)
{
string += (char) Serial.read();
Serial.flush();
}
if(string != "")
{
Serial.println(string);
}
}
But when I upload it and open the serial monitor, and input anything it is spread over several lines, as so:
Input: Why are you doing this?
W
hy
are y
ou doin
g this?
I've been stuck on this for hours now. My device is the Arduino Uno (Offical), I'm running on windows 7. Thanks in advance for any help.
Edit: Serial.print(string) returns nothing, leaves the console screen blank.
Use Serial.print(string); instead of println()
println() Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').
Ref: http://arduino.cc/en/Serial/Println
Just make a little delay in your while loop
so the code is:
String string= "";
String string2 = "";
void setup()
{
Serial.begin(9600);
}
void loop() {
string = "";
while(Serial.available() > 0)
{
string += (char) Serial.read();
Serial.flush();
delay(10);
}
if(string != "")
{
Serial.println(string);
}
}

Convert serial.read() into a usable string using Arduino

I'm using two Arduinos to sent plain text strings to each other using NewSoftSerial and an RF transceiver.
Each string is perhaps 20-30 characters in length. How do I convert Serial.read() into a string so I can do if x == "testing statements", etc.?
Unlimited string readed:
String content = "";
char character;
while(Serial.available()) {
character = Serial.read();
content.concat(character);
}
if (content != "") {
Serial.println(content);
}
From Help with Serial.Read() getting string:
char inData[20]; // Allocate some space for the string
char inChar = -1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
void setup() {
Serial.begin(9600);
Serial.write("Power On");
}
char Comp(char* This) {
while (Serial.available() > 0) // Don't read unless there
// you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
if (strcmp(inData, This) == 0) {
for (int i=0; i<19; i++) {
inData[i] = 0;
}
index = 0;
return(0);
}
else {
return(1);
}
}
void loop()
{
if (Comp("m1 on") == 0) {
Serial.write("Motor 1 -> Online\n");
}
if (Comp("m1 off") == 0) {
Serial.write("Motor 1 -> Offline\n");
}
}
You can use Serial.readString() and Serial.readStringUntil() to parse strings from Serial on the Arduino.
You can also use Serial.parseInt() to read integer values from serial.
int x;
String str;
void loop()
{
if(Serial.available() > 0)
{
str = Serial.readStringUntil('\n');
x = Serial.parseInt();
}
}
The value to send over serial would be my string\n5 and the result would be str = "my string" and x = 5
I was asking the same question myself and after some research I found something like that.
It works like a charm for me. I use it to remote control my Arduino.
// Buffer to store incoming commands from serial port
String inData;
void setup() {
Serial.begin(9600);
Serial.println("Serial conection started, waiting for instructions...");
}
void loop() {
while (Serial.available() > 0)
{
char recieved = Serial.read();
inData += recieved;
// Process message when new line character is recieved
if (recieved == '\n')
{
Serial.print("Arduino Received: ");
Serial.print(inData);
// You can put some if and else here to process the message juste like that:
if(inData == "+++\n"){ // DON'T forget to add "\n" at the end of the string.
Serial.println("OK. Press h for help.");
}
inData = ""; // Clear recieved buffer
}
}
}
This would be way easier:
char data [21];
int number_of_bytes_received;
if(Serial.available() > 0)
{
number_of_bytes_received = Serial.readBytesUntil (13,data,20); // read bytes (max. 20) from buffer, untill <CR> (13). store bytes in data. count the bytes recieved.
data[number_of_bytes_received] = 0; // add a 0 terminator to the char array
}
bool result = strcmp (data, "whatever");
// strcmp returns 0; if inputs match.
// http://en.cppreference.com/w/c/string/byte/strcmp
if (result == 0)
{
Serial.println("data matches whatever");
}
else
{
Serial.println("data does not match whatever");
}
The best and most intuitive way is to use serialEvent() callback Arduino defines along with loop() and setup().
I've built a small library a while back that handles message reception, but never had time to opensource it.
This library receives \n terminated lines that represent a command and arbitrary payload, space-separated.
You can tweak it to use your own protocol easily.
First of all, a library, SerialReciever.h:
#ifndef __SERIAL_RECEIVER_H__
#define __SERIAL_RECEIVER_H__
class IncomingCommand {
private:
static boolean hasPayload;
public:
static String command;
static String payload;
static boolean isReady;
static void reset() {
isReady = false;
hasPayload = false;
command = "";
payload = "";
}
static boolean append(char c) {
if (c == '\n') {
isReady = true;
return true;
}
if (c == ' ' && !hasPayload) {
hasPayload = true;
return false;
}
if (hasPayload)
payload += c;
else
command += c;
return false;
}
};
boolean IncomingCommand::isReady = false;
boolean IncomingCommand::hasPayload = false;
String IncomingCommand::command = false;
String IncomingCommand::payload = false;
#endif // #ifndef __SERIAL_RECEIVER_H__
To use it, in your project do this:
#include <SerialReceiver.h>
void setup() {
Serial.begin(115200);
IncomingCommand::reset();
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (IncomingCommand::append(inChar))
return;
}
}
To use the received commands:
void loop() {
if (!IncomingCommand::isReady) {
delay(10);
return;
}
executeCommand(IncomingCommand::command, IncomingCommand::payload); // I use registry pattern to handle commands, but you are free to do whatever suits your project better.
IncomingCommand::reset();
Here is a more robust implementation that handles abnormal input and race conditions.
It detects unusually long input values and safely discards them. For example, if the source had an error and generated input without the expected terminator; or was malicious.
It ensures the string value is always null terminated (even when buffer size is completely filled).
It waits until the complete value is captured. For example, transmission delays could cause Serial.available() to return zero before the rest of the value finishes arriving.
Does not skip values when multiple values arrive quicker than they can be processed (subject to the limitations of the serial input buffer).
Can handle values that are a prefix of another value (e.g. "abc" and "abcd" can both be read in).
It deliberately uses character arrays instead of the String type, to be more efficient and to avoid memory problems. It also avoids using the readStringUntil() function, to not timeout before the input arrives.
The original question did not say how the variable length strings are defined, but I'll assume they are terminated by a single newline character - which turns this into a line reading problem.
int read_line(char* buffer, int bufsize)
{
for (int index = 0; index < bufsize; index++) {
// Wait until characters are available
while (Serial.available() == 0) {
}
char ch = Serial.read(); // read next character
Serial.print(ch); // echo it back: useful with the serial monitor (optional)
if (ch == '\n') {
buffer[index] = 0; // end of line reached: null terminate string
return index; // success: return length of string (zero if string is empty)
}
buffer[index] = ch; // Append character to buffer
}
// Reached end of buffer, but have not seen the end-of-line yet.
// Discard the rest of the line (safer than returning a partial line).
char ch;
do {
// Wait until characters are available
while (Serial.available() == 0) {
}
ch = Serial.read(); // read next character (and discard it)
Serial.print(ch); // echo it back
} while (ch != '\n');
buffer[0] = 0; // set buffer to empty string even though it should not be used
return -1; // error: return negative one to indicate the input was too long
}
Here is an example of it being used to read commands from the serial monitor:
const int LED_PIN = 13;
const int LINE_BUFFER_SIZE = 80; // max line length is one less than this
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.print("> ");
// Read command
char line[LINE_BUFFER_SIZE];
if (read_line(line, sizeof(line)) < 0) {
Serial.println("Error: line too long");
return; // skip command processing and try again on next iteration of loop
}
// Process command
if (strcmp(line, "off") == 0) {
digitalWrite(LED_PIN, LOW);
} else if (strcmp(line, "on") == 0) {
digitalWrite(LED_PIN, HIGH);
} else if (strcmp(line, "") == 0) {
// Empty line: no command
} else {
Serial.print("Error: unknown command: \"");
Serial.print(line);
Serial.println("\" (available commands: \"off\", \"on\")");
}
}
String content = "";
char character;
if(Serial.available() >0){
//reset this variable!
content = "";
//make string from chars
while(Serial.available()>0) {
character = Serial.read();
content.concat(character);
}
//send back
Serial.print("#");
Serial.print(content);
Serial.print("#");
Serial.flush();
}
If you want to read messages from the serial port and you need to deal with every single message separately I suggest separating messages into parts using a separator like this:
String getMessage()
{
String msg=""; //the message starts empty
byte ch; // the character that you use to construct the Message
byte d='#';// the separating symbol
if(Serial.available())// checks if there is a new message;
{
while(Serial.available() && Serial.peek()!=d)// while the message did not finish
{
ch=Serial.read();// get the character
msg+=(char)ch;//add the character to the message
delay(1);//wait for the next character
}
ch=Serial.read();// pop the '#' from the buffer
if(ch==d) // id finished
return msg;
else
return "NA";
}
else
return "NA"; // return "NA" if no message;
}
This way you will get a single message every time you use the function.
Credit for this goes to magma. Great answer, but here it is using c++ style strings instead of c style strings. Some users may find that easier.
String string = "";
char ch; // Where to store the character read
void setup() {
Serial.begin(9600);
Serial.write("Power On");
}
boolean Comp(String par) {
while (Serial.available() > 0) // Don't read unless
// there you know there is data
{
ch = Serial.read(); // Read a character
string += ch; // Add it
}
if (par == string) {
string = "";
return(true);
}
else {
//dont reset string
return(false);
}
}
void loop()
{
if (Comp("m1 on")) {
Serial.write("Motor 1 -> Online\n");
}
if (Comp("m1 off")) {
Serial.write("Motor 1 -> Offline\n");
}
}
If you're using concatenate method then don't forget to trim the string if you're working with if else method.
Use string append operator on the serial.read(). It works better than string.concat()
char r;
string mystring = "";
while(serial.available()){
r = serial.read();
mystring = mystring + r;
}
After you are done saving the stream in a string(mystring, in this case), use SubString functions to extract what you are looking for.
I could get away with this:
void setup() {
Serial.begin(9600);
}
void loop() {
String message = "";
while (Serial.available())
message.concat((char) Serial.read());
if (message != "")
Serial.println(message);
}
Many great answers, here is my 2 cents with exact functionality as requested in the question.
Plus it should be a bit easier to read and debug.
Code is tested up to 128 chars of input.
Tested on Arduino uno r3 (Arduino IDE 1.6.8)
Functionality:
Turns Arduino onboard led (pin 13) on or off using serial command input.
Commands:
LED.ON
LED.OFF
Note: Remember to change baud rate based on your board speed.
// Turns Arduino onboard led (pin 13) on or off using serial command input.
// Pin 13, a LED connected on most Arduino boards.
int const LED = 13;
// Serial Input Variables
int intLoopCounter = 0;
String strSerialInput = "";
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(LED, OUTPUT);
// initialize serial port
Serial.begin(250000); // CHANGE BAUD RATE based on the board speed.
// initialized
Serial.println("Initialized.");
}
// the loop routine runs over and over again forever:
void loop()
{
// Slow down a bit.
// Note: This may have to be increased for longer strings or increase the iteration in GetPossibleSerialData() function.
delay(1);
CheckAndExecuteSerialCommand();
}
void CheckAndExecuteSerialCommand()
{
//Get Data from Serial
String serialData = GetPossibleSerialData();
bool commandAccepted = false;
if (serialData.startsWith("LED.ON"))
{
commandAccepted = true;
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
}
else if (serialData.startsWith("LED.OFF"))
{
commandAccepted = true;
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
}
else if (serialData != "")
{
Serial.println();
Serial.println("*** Command Failed ***");
Serial.println("\t" + serialData);
Serial.println();
Serial.println();
Serial.println("*** Invalid Command ***");
Serial.println();
Serial.println("Try:");
Serial.println("\tLED.ON");
Serial.println("\tLED.OFF");
Serial.println();
}
if (commandAccepted)
{
Serial.println();
Serial.println("*** Command Executed ***");
Serial.println("\t" + serialData);
Serial.println();
}
}
String GetPossibleSerialData()
{
String retVal;
int iteration = 10; // 10 times the time it takes to do the main loop
if (strSerialInput.length() > 0)
{
// Print the retreived string after looping 10(iteration) ex times
if (intLoopCounter > strSerialInput.length() + iteration)
{
retVal = strSerialInput;
strSerialInput = "";
intLoopCounter = 0;
}
intLoopCounter++;
}
return retVal;
}
void serialEvent()
{
while (Serial.available())
{
strSerialInput.concat((char) Serial.read());
}
}
This always works for me :)
String _SerialRead = "";
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) //Only run when there is data available
{
_SerialRead += char(Serial.read()); //Here every received char will be
//added to _SerialRead
if (_SerialRead.indexOf("S") > 0) //Checks for the letter S
{
_SerialRead = ""; //Do something then clear the string
}
}
}

Resources