serial monitor arduino

Helloo Guys :slight_smile:

I want ask about serial monitor . If i want to control Led by used string word like "#LEDON%" to turn on the LED , and "#LEDOFF%" to turn off the LED , with out delay .Just want arduino take order that when Message start with # he have to start check the work and when he saw % he stop and take action ?

void addToInstruction(int incomingByte){
if(readingInput){
char receivedCharacter = (char) incomingByte;
instruction += receivedCharacter;
}

void executeInstruction(){
readingInput = false;
if (instruction == "#SET_BOTTOM_LED_ON%"){
digitalWrite(readingLed, HIGH);
}
else if (instruction == "#SET_BOTTOM_LED_OFF%"){
digitalWrite(readingLed, LOW);
}

And what happens when you try that?

(Please remember to post all of your code in code tags)

Ok i will post all code

when i try do my code it not working and LED do not Response , if some one can send me maybe tutorial or help me to fix my code it wil happy for this

when i just do char 'i' to on and 'h' to off it work but when i try to do sting and with out delay nothing work and LED not Response

const long connectionSpeed = 9600;
const int readingLed = 6;
const int instructionLed = 7; // SET_BOTTOM_LED_ON . LED ON .SET_BOTTOM_LED_OFF
const int stringDelay = 5; // Variabele die

int incomingByte = 0; // byte
boolean readingInput = false; // Boolean
String instruction = ""; // string holder

void setup(){
Serial.begin(connectionSpeed);
Serial.flush();

pinMode(buttonPin, INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(10, INPUT);

digitalWrite(buttonPin, HIGH);
}

void loop(){
if(calculateDelay(stringDelay, stringMillis)){
talkSerial();
}

void talkSerial(){
Serial.println("Instruction: " + instruction);
if (Serial.available() > 0) {
incomingByte = Serial.read();
switch(incomingByte){
case 35:
openInstruction();
break;
case 37:
executeInstruction();
break;
default:
addToInstruction(incomingByte);
break;
}
digitalWrite(instructionLed, HIGH);
}else{
digitalWrite(instructionLed, LOW);
}

// to add char together
void addToInstruction(int incomingByte){
if(readingInput){
char receivedCharacter = (char) incomingByte;
instruction += receivedCharacter;
}

}
// functie make the string empty
void openInstruction(){
readingInput = true;
instruction = "";
}

// take action
void executeInstruction(){
readingInput = false;
if (instruction == "SET_BOTTOM_LED_ON"){
digitalWrite(readingLed, HIGH);
}
else if (instruction == "SET_BOTTOM_LED_OFF"){
digitalWrite(readingLed, LOW);
}

That's a lot of code to have written without any debug aids.
Or code tags.

Hi send88,

Insert some Serial.println debug printouts in your code and take a closer look at the variables to see if they are actually set to what you think they are set to.

Also, strings "x" and "X" are different and won't be equal.

Let us know how this works.

Pat.

patduino:
Hi send88,

You can't compare strings like that

But you can compare Strings like that.

how i can compare strings together then ?

send88:
how i can compare strings together then ?

To compare strings, use strcmp.
But you're using Strings, so == is OK.

u mean like that

command = "#LEDON";
if (strcmp(command, "LEDON") == 0)
{
// do LED ON

}

Asuuming "command" is a char pointer, yes.

thx :slight_smile: .
i will do test and see