I have a new project in my head but i have a really bad problem - i know nothing about the strings with the arduino. There is also not much to read on the Internet - i just want to "Serial.read" some string and parse it to use it for the needs of my project so someone please help me
Searching in the box above gives many hits including
http://forum.arduino.cc/index.php?topic=288234.0
Weedpharma
The examples in serial input basics should be useful.
It is not a good idea to use Strings (capital S) in the small memory of an Arduino. Just use strings (small s) which are char arrays with a 0 terminator.
...R
Thank you
i just want to "Serial.read" some string and parse it to use it for the needs of my project so someone please help me
Lot of String functions to chose from. Do you have an example of a string you want to capture and parse?
I tried many times, i read many forums but i understood nothing.I want to work with strings in arduino - strings, which come from the serial communication, i want to parse them and use every single character.Please give me some library download and some example code so i can make my project.
iliyancho57:
Please give me some library download and some example code so i can make my project.
See Reply #2
...R
It has nothing in cummon with what i am asking for
Try giving a detailed explanation of what you actually want to do.
Weedpharma
Very simple serial code that captures the character string sent from the serial monitor and evaluates what was captured.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
//A very simple example of sending a string of characters
//from the serial monitor, capturing the individual
//characters into a String, then evaluating the contents
//of the String to possibly perform an action (on/off board LED).
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if(readString.indexOf("on") >=0)
{
digitalWrite(ledPin, HIGH);
Serial.println("LED ON");
}
if(readString.indexOf("off") >=0)
{
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
}
readString="";
}
}
iliyancho57:
It has nothing in cummon with what i am asking for
How are we supposed to know ?
If you want help you have to explain what you are trying to do.
I linked to serial input basics so that it would give you a good starting point from which to develop your own code. I was not assuming it would solve your problem completely.
Have you tried it?
...R
Hi,
Can you draw a diagram, block type, showing what you want to connect to the arduino as an input, and what you want to output it to.
On the input tell us if its rs232 or what ever serial system, and from what device.
On the output tell us if its rs232 or what and what it will be connected to.
What is your electronics, programming, arduino, hardware experience?
Then we can let you know if its possible and some suggestions how.
Tom....
Ok.I am sorry for the bad explanation. I want to write something from the serial monitor.Than i want the arduino to recieve it and it will be something like "?F150/n".Than i want the arduino to parse it.The "/n" acts like an end so the arduino should know that the string ends. Then i want the arduino to have all the characters from the string in different variables so i can use the "if" funktion to check this charakters and use them for protocol purposes.I hope you can help me better now
Then all you need to know IS in the link Robin gave you. Only you need to Do something. Start to write some basic code and start expanding it.
Zoomkat, the String class really sucks... Better to use just c-type string (aka arrays of chars). Not as idiotic memory hungry as String.
iliyancho57:
it will be something like "?F150/n".Than i want the arduino to parse it.The "/n" acts like an end so the arduino should know that the string ends.
That is exactly what the second example in serial input basics is all about. Or, if the ? is a start marker rather than part of the message, use the 3rd example.
Then i want the arduino to have all the characters from the string in different variables so i can use the "if" funktion to check this charakters and use them for protocol purposes.I hope you can help me better now
Serial input basics will put your message in the array receivedChars[]. If the message is "?F150" you can get the ? with receivedChars[0], the F with receivedChars[1] etc. This is very basic use of array - not rocket science - just a little careful thinking.
...R
Hallo guys.I have been having a big problem with strings a long time already.I tried to write in this forum but everybody just gave me the serial input basic's tutorial which has not got anything in common with my needs.I also tried to store chars into arrey like this "char command[4]" but it didtn't work at all. The last thing i tried today - with having no idea if there is such code (because noone helped me with giving me some made code which i am sure that is very very simple).I just want to recieve a string with some chars, parse it and than use "if" to check every char and use it for example if it is B to do something and so on. I tried this:
#include<String.h>
#define led 13
String str;
void setup() {
Serial.begin(9600);
pinMode(led,OUTPUT);
}
void loop() {
str=Serial.read()
if(str[3] == 4){
digitalWrite(led,HIGH);
delay(1000);
digitalWrite(led,LOW);
}
else{
digitalWrite(led,HIGH);
delay(1000);
digitalWrite(led,LOW);
delay(1000);
digitalWrite(led,HIGH);
delay(1000);
digitalWrite(led,LOW);
}
}
It doesent work of course but i will try to figure it out no mather how wong it will take.If you will - please help me but DO NOT give me the useless serial input basic's link.Thank you
I have been having a big problem with strings
No, you're having problems with Strings.
str=Serial.read()
There's a missing semicolon for a start.
Then there's the problem that Serial.read returns a single character, and you're trying to assign that character to a String.
if(str[3] == 4){
And then you treat the String as an array
(you didn't check to see that there was something to read, but you read it anyway)
I strongly suggest you look at the serial basics tutorial.
And next time, please use code tags when posting code.
iliyancho57:
It doesent work of course but i will try to figure it out no mather how wong it will take.If you will - please help me but DO NOT give me the useless serial input basic's link.Thank you
OK, your code is complete nonsense.
You seem to know hardly anything about programming.
So please describe in plain English words:
- which commands do you want to send (examples) using Serial?
- what do you want the code to do when which command has been received?
What about starting initially with "short commands" that do not consitst of "many letters", but that are one-character-commands, just one char like '0' for 'Off', '1' for 'On' and '2' for 'Blink' of the LED?
What is the command you are trying to send? How does the receiver know where the command starts? What other commands are you likely to use in the final system? Are they different lengths or are they always exactly 4 chars?
if(str[3] == 4){
This compares an ASCII character to an integer. If you were looking for the digit '4' then this won't find it, since '4' is actually encoded as a different number which is not 4.
if(str[3] == '4'){
Some very basic serial control code.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
//A very simple example of sending a string of characters
//from the serial monitor, capturing the individual
//characters into a String, then evaluating the contents
//of the String to possibly perform an action (on/off board LED).
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if(readString.indexOf("on") >=0)
{
digitalWrite(ledPin, HIGH);
Serial.println("LED ON");
}
if(readString.indexOf("off") >=0)
{
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
}
readString="";
}
}