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
UPDATE: It seems it does not compare the string with "Marco", but "MarcoMarco" - why?
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?
/*
#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");
}
}