Arduino has to respond on string from serial

Hi there!

I have been tinkering with Arduino and trying to make it respond to a command sent by serial (this is meant for use with Delphi, so it can communicate).

It does respond to the command the first time, but afterwards it doesn't "recognize" the command as if the string comparison only runs once.

 void loop() {
  char charArray[256];
  char c;
  int  i = 0;
  pinMode(LED, OUTPUT);
  charArray[0] = 0; 

  Serial.begin(SERIAL_SPEED);

  while (Serial.available() == 0);  // do nothing - wait for first char

  c = Serial.read();
  
  while (c != '#') //uses # as return
  {
    if (c != '\n')                    // skip new lines
    {
      charArray[i++] = c;             // store the character, and then increment our index
      charArray[i] = 0;               // terminate the string
    }
    while (Serial.available() == 0);  // wait for next char
    c = Serial.read();
  }

  if (strcmp(charArray, "Marco")){
    Serial.println("Polo!");
  } 

 }

I'm a noob at Arduino, but you have to start somewhere :slight_smile:

UPDATE: It seems it does not compare the string with "Marco", but "MarcoMarco" - why?

I don't know if it will help with the problem but these two lines of code need to be moved to setup...

  pinMode(LED, OUTPUT);
  Serial.begin(SERIAL_SPEED);

"strcmp" returns zero for a string match, so the sense of your comparison is wrong.

if (strcmp(charArray, "Marco") [glow]== 0[/glow]){

(can I suggest you take a look at the "do ..while" loop?)

Ah, I forgot that strcmp returns zero when true :slight_smile:

But now, when I send "Marco" several times, it sometimes will respond with "Polo!" followed by what seems like "...".
I can't show you, because it disappears when I copy it - could this simply be a flaw in the serial prompt?

Did you move the "Serial.begin" to "setup()"?

Now I have - and it works! Hurray!

Thx everybody.

Another thing:

I now try to send an analog reading to the serial port when it receives a certain command.
Here's a snippet:

  if (strcmp(charArray, "v")==0){ 
    Serial.print(voltage,8);
    Serial.println(" volt");
  }

However it takes another command or two before the analog reading has stabilized (I turn the potentiometer and then send the command). How is that?

Please ask if you need more of the code.

Please ask if you need more of the code

Yup.

/*
#include <WString.h> //Den skal vi bruge til sammenligning af strenge
#define LED  13
#define SERIAL_SPEED  9600 //Sæt serielhastigheden her.

void setup()
{
  Serial.begin(SERIAL_SPEED);
}
 
 void loop() {
  float analogValue = analogRead(0); //Læs analog signal i port 0
  float maxVol = 5; //Det er den maksimale spænding, der kan måles. Det er lig med V = 5v på Arduino strømforsyning.
  float res = 1023; //Opløsningen på 8 bit.
  float voltage = ((maxVol / res) * analogValue); //Beregn spændingen som float
  char charArray[256]; //Lav char som tekstarray på max 256 karakterer
  char c; //Definer tegnet c (fx c = "a")
  int  i = 0; //Så vi kan tælle
  charArray[0] = 0; //charArray starter med at være tom

  while (Serial.available() == 0);  //Gør intet før der læses noget fra serielport

  c = Serial.read(); //Læs det første tegn, der kommer fra porten
  
  while (c != '#')  //Afslut kommando med # - skal rettes til /r hvis Arduino skal kommunikere med fx Delphi
  {
    if (c != '\n')                    // skip new lines
    {
      charArray[i++] = c;             // store the character, and then increment our index
      charArray[i] = 0;               // terminate the string
    }
    while (Serial.available() == 0);  // wait for next char
    c = Serial.read();
  }

  if (strcmp(charArray, "v")==0){ //Hvis Arduino læser "v#" fra serielporten, så send spændingen på porten tilbage
    Serial.print(voltage,8);
    Serial.println(" volt");
  } 

 }

Why don't you read the voltage only when you've got the command?
Your voltage could be very old.

You are completely right!
Moved it to the send-sequence and now it updates correctly. Thanks!

This is what we Danes would call an errorcode 40 (the error sits 40 cm from the screen ;))

we Danes would call an errorcode 40

I like! ;D

I was wondering about a bit from my code:

while (Serial.available() == 0);

does this really do anything? I copied some of the code from a working example. I think it would make more sense, if it was:

if (Serial.available() > 0) {
(Do something)
}

if I only wanted the Arduino to work when it has received a byte from the serialport.

The first one says to nothing until there is data to process. The second one says to do nothing unless there is data to process.

Depending on what else is going on, one method might be preferred over the other. Your call.