Passing a string with multiple variables to anrduino using serial port - arduino

Im trying to pass a string generated using processing to the Arduino serial port but it doesn't seem to work.
Arduino Code:
String readString; //main captured String
String MotorChoice;
String AngleRange;
String FrequencyIN;
int ind1;
int ind2;
int ind3;
int MC;
int AR;
float FIN;
void setup() {
Serial.begin(9600);
Serial.println("Input command in the form of Motor Choice,Angle Range,Frequency*");
}
void loop()
{
if (Serial.available()) {
char c = Serial.read();
if (c == '*') {
Serial.println();
Serial.print("captured String is : ");
Serial.println(readString);
ind1 = readString.indexOf(','); //finds location of first ,
MotorChoice = readString.substring(0, ind1); //captures first data String
ind2 = readString.indexOf(',', ind1+1 ); //finds location of second ,
AngleRange = readString.substring(ind1+1, ind2); //captures second data String
ind3 = readString.indexOf(',', ind2+1 );
FrequencyIN = readString.substring(ind2+1);
//convert sring to int
MC = MotorChoice.toInt();
AR = AngleRange.toInt();
FIN = FrequencyIN.toFloat();
Serial.print("Motor Selected = ");
Serial.println(MC);
Serial.print("Angle Range = ");
Serial.println(AR);
Serial.print("Frequency Required = ");
Serial.println(FIN);
Serial.println();
Serial.println();
readString=""; //clears variable for new input
MotorChoice="";
AngleRange="";
FrequencyIN="";
}
else
{
readString += c; //makes the string readString
}
}
}
Processing Code:
import processing.serial.*;
Serial myPort;
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
int M =1;
int A =90;
float F =2.5;
String sM = str(M);
String sA = str(A);
String sF = str(F);
String sb;
String s1;
sb = sM +","+ sA+"," + sF+"*";
s1 = sb;
println(s1);
myPort.write(s1);
The above Arduino code has previously been tried out where an input was fed using the serial monitor in the format of motor,angle,frequency*
Any help would greatly be appreciated.

If I am interpreting your code correctly you are using the * character as a way of signifying the end of the list. But in your Arduino code you are checking for the * then you are printing the output string. Thus, the rest of your array is long gone by that point. Also, you set a variable at the beginning called readString but you never set it to anything. That's why you you're not seeing any output.
First I would start by changing your variable name from readString to something else (for the sake of clarity) then use Serial.readString() to get that string from the serial port. Then check the last character of that string for the * character.
if (Serial.available()) {
String str = Serial.readString(); // get string
char c = str[strlen(str)-1]; // find *
if (c == '*') {
Serial.print("Output: ");
Serial.println(str);
//The rest of your code
}

Related

Error in sending entire variable from raspberrypi (python) to arduino

I want to transfer a number"500" stored in a variable "int3" from python to arduino via serial communication.
The arduino reads the data using Serial.read(), but prints only "5".
Thanks in advance.
I have int3, byte 2 and byte 3 to be sent from python, but want arduino to print the value of int3.
import serial
import time
ser=serial.Serial('/dev/ttyACM0',9600)
int3 = 500
int3 = b'%d' %int3
while (1):
ser.write(int3)
ser.write(b'2')
ser.write(b'3')
#print type(ser.write)
time.sleep(1)
print(int3)
String r;
void setup(){
Serial.begin(9600);
// while(!Serial)
// {
// ;
// }
}
void loop(){
if(Serial.available() > 0){
r =(Serial.read() - 0); //conveting the value of chars to integer
Serial.print(r[0]);
delay(100);
//while(Serial.available () !=0) r=Serial.read();
}
}
Replace the if with a while loop:
String fromRaspberry;
void loop(){
char c;
while (Serial.available() > 0) {
c = Serial.read(); //read char
fromRaspberry += c; //add the read char to string
}
int x = fromRaspberry.toInt(); //convert string to int
Serial.println(x);
// clear the string for new input:
fromRaspberry = "";
}

Convert "String" above 255 to exact "Integer" or "Long" type in Arduino IDE

Appreciate your time.
I am trying to convert "String" read from serial port in serialEvent() of Arduino IDE to integer values with exact representation.
For eg, if String myString = 200 then int myInt should be 200.
I have been somewhat successful but unable to convert String to exact int representation beyond 255.
Solutions I have tried:
1) used .toInt() function in Arduino.
2) used "atoi" and "atol" functions.
3) Serial.parseInt() in loop().
All of these methods start recounting from 0 after every 255 values.
I can't use parseInt since it only works inside loop(). My application requires to store variable value permanently until another value is given through port. For this Arduino Due's flash memory has been used.
The memory storing code seems to work only inside serialEvent().
Code snippet is as below:
#include <stdlib.h>
#include <DueFlashStorage.h>
DueFlashStorage memory;
String x = " ";
int x_size;
int threshold;
void setup(){
Serial.begin(115200);
}
void loop{
Serial.println(memory.read(0));
}
void serialEvent(){
while(Serial.available()){
x = Serial.readStringUntil('\n');
x_size = x.length();
char a[x_size+1];
x.toCharArray(a, x_size+1);
threshold = atoi(a);
memory.write(0, threshold);
}
}
1) Function .toInt() returns LONG and you want INT (I don't know why honestly but it is in documentation)... you need to cast it like this (tried on Arduino ATMEGA and it worked):
#include <stdlib.h>
String x = "1000";
int x_ = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
x_ = (int) x.toInt();
Serial.println(x_);
delay(1000);
}
2) I’m not professional ... but serilEvent() is really old way of doing things and it isn't recommended to use it. You can do it "manually" in the loop() function.
You're only converting 1 character a time, that's why the limit is 255.
If you're not doing anything else, you could stay in a serial.read-loop until all characters are read. For example:
void loop() {
if(Serial.available()) {
byte count = 0;
char number[10]; // determine max size of array
bool wait = true;
while(wait) { // stay in this loop until newline is read
if(Serial.available()) {
number[count] = Serial.read();
if (number[count] == '\n') {
wait = false; // exit while loop
}
count++;
}
}
int threshold = atoi(number);
memory.write(0, threshold);
}
}
For the lack of a good function in Arduino IDE for char/String type to int type conversion (has a limit of 255), I wrote my own conversion code which seems to work perfectly.
int finalcount=0;
void setup(){
Serial.begin(115200);
}
void loop(){
if(Serial.available()) {
int count = 0;
char number[5]; // determine max size of array as 5
bool wait = true;
while(wait) { // stay in this loop until newline is read
if(Serial.available()) {
number[count] = Serial.read();
if (number[count] == '\n') {
finalcount = count; //max array size for integer; could be less than 5
wait = false; // exit while loop
}
count++;
}
}
int val[finalcount]; //array size determined for integer
int placeValue;
int finalval[finalcount];
int temp=0;
int threshold;
for(int i = 0; i<finalcount; i++){
val[i] = (int)number[i]-48; //convert each char to integer separately
placeValue = pow(10,(finalcount-1)-i); //calculate place value with a base of 10
finalval[i] = val[i]*placeValue; //update integers with their place value
temp += finalval[i] ; //add all integers
threshold = temp; //resulting number stored as threshold
}
Serial.println(threshold); //prints beyond 255 successfully !
}
}
I solved the problem using highByte and lowByte functions of Arduino. Works flawlessly.
#include <DueFlashStorage.h>
DueFlashStorage m;
byte a1,a2;
int val;
void setup() {
Serial.begin(115200); //start the serial communication
}
void loop()
{
if (Serial.available()>0)
{
val = Serial.parseInt(); //read the integer value from series
if(val>0){
a1 = highByte(val); //get the higher order or leftmost byte
a2 = lowByte(val); //get the lower order or rightmost byte
m.write(0,a1); //save in arduino due flash memory address 0
m.write(1,a2); //save in arduino due flash memory address 1
}
int myInteger;
myInteger = (m.read(0)*256)+m.read(1); //convert into the true integer value
Serial.println(myInteger);
}

Sending array data from Processing to Arduino

I successfully managed to send a single integer from processing to Arduino but now I want to send an array of three integers and I can't get it working. I want to create a buzzer feedback with Arduino which processing will control which buzzer to activate. For example, the data send from processing should be [1,0,1] meaning sensor 1 and 3 should start working. The buzzers should be able to be activated simultaneously in case that [1,1,1] goes through.
This is the code I have so far:
I am trying to understand what data is being sent back to Arduino to know how to use it and I keep getting either a null value or a random integer.
I'm trying to learn how to do this so apologies if the code is bad.
Arduino
void setup(){
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop(){
if (Serial.available()){
const char data = Serial.read();
char noteBuzzer[] = {data};
for (int i = 0 ; i < sizeof(noteBuzzer); i++) {
}
Serial.print(noteBuzzer[1]);
}
}
Processing
import processing.serial.*;
String notes[];
String tempo[];
Serial myPort;
String val;
void setup(){
size(200,200);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
notes = loadStrings("data/notes.txt");
tempo = loadStrings("data/tempo.txt");
}
void draw() {
if (keyPressed == true)
{
if (key == '1') {
println("Start");
readNotes();
}
}
}
void readNotes(){
for (int i = 0 ; i < notes.length; i++) {
println(notes[i]);
//println(tempo[i]);
myPort.write(notes[i]);
delay(int(tempo[i])); //this will be the tempo?
if ( myPort.available() > 0)
{
val = myPort.readStringUntil('\n');
println("Arduino",val);
}
}
}
If you're data is an array that always has 3 items and each of those items are always either 1 or 0 (bits), you could store that whole data in a single byte (and still have 5 more bits to spare). Sending and receiving a byte is pretty simple with Arduino.
Here's a basic sketch that shows you how to flip 3 bits in a single byte:
// the state as a byte
byte buzzerState = 0B000;
void setup(){
textFont(createFont("Courier New",18),18);
}
void draw(){
background(0);
text("DEC: " + buzzerState +
"\nBIN:" + binary(buzzerState,3),10,40);
}
void keyPressed(){
if(key == '1'){
buzzerState = flipBit(buzzerState,0);
}
if(key == '2'){
buzzerState = flipBit(buzzerState,1);
}
if(key == '3'){
buzzerState = flipBit(buzzerState,2);
}
}
// flips a bit at a given index within a byte and returns updated byte
byte flipBit(byte state,int index){
int bit = getBitAt(state,index);
int bitFlipped = 1 - bit;
return setBitAt(state,index,bitFlipped);
}
// returns the integer value of a bit within a byte at the desired index
int getBitAt(byte b,int index){
index = constrain(index,0,7);
return b >> index & 1;
}
// sets an individual bit at a desired index on or off (value) and returns the updated byte
byte setBitAt(byte b,int index, int value){
index = constrain(index,0,7);
value = constrain(value,0,1);
if(value == 1) b |= (1 << (index));
else b &= ~(1 << (index));
return b;
}
Use keys '1','2' and '3' to flip the bits.
Note that in keypress we're always updating the same byte.
The text will display the decimal value first and the binary value bellow.
This is the most efficient way to send your data and the simplest in terms of serial communication. On the Arduino side you can simply use bitRead() on the byte you get from Serial.read(). For more on binary/bits/bytes be sure to read the BitMath Arduino tutorial. Binary may seem intimidating at first, but it's really not that bad once you practice a bit and it's totally worth knowing.
Here's an updated version of the code above that sends the byte to Arduino on the first available serial port (be sure to change Serial.list()[0] with what makes sense for your setup and press 's' to send an update to Arduino:
import processing.serial.*;
// the state as a byte
byte buzzerState = 0B000;
Serial port;
void setup(){
textFont(createFont("Courier New",18),18);
try{
port = new Serial(this,Serial.list()[0],9600);
}catch(Exception e){
e.printStackTrace();
}
}
void draw(){
background(0);
text("DEC: " + buzzerState +
"\nBIN:" + binary(buzzerState,3),10,40);
}
void keyPressed(){
if(key == '1'){
buzzerState = flipBit(buzzerState,0);
}
if(key == '2'){
buzzerState = flipBit(buzzerState,1);
}
if(key == '3'){
buzzerState = flipBit(buzzerState,2);
}
if(key == 's'){
if(port != null){
port.write(buzzerState);
}else{
println("serial port is not open: check port name and cable connection");
}
}
}
// flips a bit at a given index within a byte and returns updated byte
byte flipBit(byte state,int index){
int bit = getBitAt(state,index);
int bitFlipped = 1 - bit;
return setBitAt(state,index,bitFlipped);
}
// returns the integer value of a bit within a byte at the desired index
int getBitAt(byte b,int index){
index = constrain(index,0,7);
return b >> index & 1;
}
// sets an individual bit at a desired index on or off (value) and returns the updated byte
byte setBitAt(byte b,int index, int value){
index = constrain(index,0,7);
value = constrain(value,0,1);
if(value == 1) b |= (1 << (index));
else b &= ~(1 << (index));
return b;
}
And here's a super basic Arduino sketch:
byte buzzerState;
void setup() {
Serial.begin(9600);
//test LEDs setup
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop() {
if(Serial.available() > 0){
buzzerState = Serial.read();
bool bit0 = bitRead(buzzerState,0);
bool bit1 = bitRead(buzzerState,1);
bool bit2 = bitRead(buzzerState,2);
//test LEDs update
digitalWrite(10,bit0);
digitalWrite(11,bit1);
digitalWrite(12,bit2);
}
}
If you connect 3 LEDs to pins 10,11,12 you should them toggle as you press keys '1','2','3' then 's' in Processing
One way around binary in Processing could be using a String representation of your data (e.g. "00000101" for [1,0,1]) and unbinary() to convert that String to an integer value you can write to serial, but it will be a bit annoying to getting and setting a character at an index (and potentially parsing that char to it's integer value and back)
When you need to send more than a byte things get a bit more complicated as you need to handle data corruption/interruptions, etc. In these situations it's best to setup/design a communication protocol based on your needs and this isn't easy if you're just getting started with Arduino, but not impossible either. Here's an example, there are many more online.
One quick and dirty thing you could try is sending that data as string terminated by a new line character (\n) which you could buffer until in Arduino then read 4 bytes at a time, discarding the \n when parsing:
e.g. sending "101\n" from Processing, representing [1,0,1] then on the Arduino side use Serial.readStringUntil('\n') and a combination of charAt() and toInt() to access each integer within that that string.
Here's an example Processing sketch:
import processing.serial.*;
// the state as a byte
String buzzerState = "010\n";
Serial port;
void setup(){
textFont(createFont("Courier New",18),18);
try{
port = new Serial(this,Serial.list()[0],9600);
}catch(Exception e){
e.printStackTrace();
}
}
void draw(){
background(0);
text(buzzerState,30,50);
}
void keyPressed(){
if(key == '1'){
buzzerState = flipBit(buzzerState,0);
}
if(key == '2'){
buzzerState = flipBit(buzzerState,1);
}
if(key == '3'){
buzzerState = flipBit(buzzerState,2);
}
if(key == 's'){
if(port != null){
port.write(buzzerState);
}else{
println("serial port is not open: check port name and cable connection");
}
}
}
String flipBit(String state,int index){
index = constrain(index,0,2);
// parse integer from string
int bitAtIndex = Integer.parseInt(state.substring(index,index+1));
// return new string concatenating the prefix (if any), the flipped bit (1 - bit) and the suffix
return state = (index > 0 ? state.substring(0,index) : "") + (1 - bitAtIndex) + state.substring(index+1);
}
And an Arduino one based on Arduino > File > Examples > 04.Communication > SerialEvent:
/*
Serial Event example
When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.
A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.
Created 9 May 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SerialEvent
*/
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
// test LEDs setup
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// process string
bool bit0 = inputString.charAt(2) == '1';
bool bit1 = inputString.charAt(1) == '1';
bool bit2 = inputString.charAt(0) == '1';
//test LEDs update
digitalWrite(10,bit0);
digitalWrite(11,bit1);
digitalWrite(12,bit2);
// clear the string:
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;
}
}
}
Note this is more prone to error and used 4 times as much data as the the single byte option.

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;
}
}

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.

Resources