I created the following code by copy pasting...
But i get an error message and i cannot find out why or how...
This is the code
//serial input
char incomingByte;
char FileName;
void setup() {
Serial.begin(115200);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it starts with IMG then set it as FileName
if (incomingByte.startsWith("IMG")) {
Serial.println("File Read");
FileName=incomingByte;
}
}
delay(1000);
}
I want if the serial string starts with IMG that FileName is set to the serial input.
I understand but i took the text startswith from one site
some serial code from another and some pieces of an example and some from arduino pages.
but the code is no at all one a site i made it myself
so i dont think the site makes any sense...
but like all ways most people speak here about the mistakes you make in the posting and not about the mistakes you make in the code
Hello, little example here, this is non blocking Serial.read's, what you type in serial monitor is stored in the char array 'input', that you can use for whatever you want, here strncmp, comparing the first 3 letters of the input.
The line ending character setting (in the serial monitor) must be set to Carriage return.
void loop()
{
if ( Serial.available() > 0 )
{
static char input[64];
static uint8_t i;
char c = Serial.read();
if ( c != '\r' && i < 64-1)
input[i++] = c;
else
{
input[i] = '\0';
i = 0;
if ( !strncmp( input, "IMG", 3 ) )
{
Serial.println("File Read");
}
}
}
}
That guix code is better for Arduino than examples that use C++ Strings. There are too many of those on the official site... do I hear an Amen out there?
Rogierv, Serial data is slow as snails compared to Arduino which may loop 100's of times waiting between characters to arrive. You are writing real time code with no operating system dragging the feet. It is a matter of understanding the scale (1 character at a time, usable processor speed enormously faster than characters arrive) and modeling how the information should flow on that scale.
Me personally, I compare the characters each as they come in and have time to check many alternatives (If I have many) when a single character does not match and still empty cycles through loop() before the next at 115200 baud. But I think best you practice what guix gave until you have a better overview or strong need to save ram space.
rogierv:
but like all ways most people speak here about the mistakes you make in the posting and not about the mistakes you make in the code
The reason is that when you ask a question poorly, it is much harder - sometimes even impossible - to answer. The sticky "Read this before posting a programming question" does have quite a lot of helpful advice about how to ask a question well. Is it fair to say you didn't read this before posting a programming question?
Simple test code that captures a string and checks the contents of that string for a substring that will turn the arduino board LED on and off.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
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(10);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if(readString.indexOf("on") >=0)
{
digitalWrite(ledPin, HIGH);
}
if(readString.indexOf("off") >=0)
{
digitalWrite(ledPin, LOW);
}
readString="";
}
}