Search code examples
arraysarduinoarduino-c++

How to store & call multiple letters in arduino?


Im new to programming, and I'm trying to make a program that adjusts LEDs according to the serial input.

#define Bathroom 13  //LED connected to pin13 named bathroom
#define Livingroom 9 //LED connected to pin9 named livingroom
char *myStrings[] = {"bathdim","bathbright","livingdim","livingbright","beddim","bedbright"};
void setup(){
 pinMode(Bathroom, OUTPUT);
 pinMode(Livingroom, OUTPUT);
 Serial.begin(9600);
}
void loop() {
if(Serial.read() == *myStrings[0]){
    Serial.println(*myStrings[0]);  // prints "bathdim" on the serial monitor
    digitalWrite(Bathroom,0);       // turns lights off
    delay(100);
  }
  if(Serial.read() == *myStrings[1]){ // prints "bathbright" on the serial monitor
    Serial.println(*myStrings[1]); // turns lights on
    digitalWrite(Bathroom,1);
    delay(100);
  }
  if(Serial.read() == *myStrings[2]){
    Serial.println(*myStrings[2]);
    digitalWrite(Livingroom,0);
    delay(100);
  }
  if(Serial.read() == *myStrings[3]){
    Serial.println(*myStrings[3]);
    digitalWrite(Livingroom,1);
    delay(100);
  }
}

When running the code, I typed in "bathbright" which should have turned the lights on, but I didn't work, and the Serial.println(*myStrings[1]) only prints letter "b", not "bathbright"

can anyone help?

EDIT

ok, I removed the * and now the code is as follows:

#define Bathroom 13  //LED connected to pin13 named bathroom
#define Livingroom 9 //LED connected to pin9 named livingroom
char *myStrings[] = {"bathdim","bathbright","livingdim","livingbright","beddim","bedbright"};
void setup(){
 pinMode(Bathroom, OUTPUT);
 pinMode(Livingroom, OUTPUT);
 Serial.begin(9600);
}
void loop() {
if(Serial.read() == *myStrings[0]){
    Serial.println(myStrings[0]);
    digitalWrite(Bathroom,0);
    delay(100);
  }
  if(Serial.read() == *myStrings[1]){
    Serial.println(myStrings[1]);
    digitalWrite(Bathroom,1);
    delay(100);
  }
  if(Serial.read() == *myStrings[2]){
    Serial.println(myStrings[2]);
    digitalWrite(Livingroom,0);
    delay(100);
  }
  if(Serial.read() == *myStrings[3]){
    Serial.println(myStrings[3]);
    digitalWrite(Livingroom,1);
    delay(100);
  }
}

The code works well initially but after a few inputs of bathbright and bathdim, he serial print does not print the correct words and the LEDs does not always respond...

FINAL CODE:

#define Bathroom 13  //LED connected to pin13 named bathroom
#define Livingroom 9 //LED connected to pin9 named livingroom
void setup(){
 Serial.begin(9600);
 pinMode(Bathroom, OUTPUT);
 pinMode(Livingroom, OUTPUT);
}

void loop(){
    String str = Serial.readString();
    if(str.indexOf("bathdim") > -1){
      Serial.println("dimming bathroom");
      digitalWrite(Bathroom,0);
    } 
    if(str.indexOf("bathbright") > -1){
      Serial.println("lighting up bathroom");
      digitalWrite(Bathroom,1);
    }
    if(str.indexOf("livingdim") > -1){
      Serial.println("dimming Livingroom");
      digitalWrite(Livingroom,0);
    } 
    if(str.indexOf("livingbright") > -1){
      Serial.println("lighting up Livingroom");
      digitalWrite(Livingroom,1);
    } 
}

Solution

  • Serial.read() will only read the first byte. You'll have to read the whole string in a loop (and a large enough buffer) and use a compare function to compare the strings.

    As an alternative for testing you could use a single byte code - for example 'a', 'b', 'c' and 'd'.

    To print the second string in the aray, you can use Serial.println(myStrings[1]).


    Here is an example using single chars:

    void loop() {
    
        if (Serial.available()) {
    
            int value = Serial.read();
    
            switch (value) {
                case 'a':
                    digitalWrite(Bathroom, 0);
                    break;
                case 'b':
                    digitalWrite(Bathroom, 1);
                    break;
                case 'c':
                    digitalWrite(Livingroom, 0);
                    break;
                case 'd':
                    digitalWrite(Livingroom, 1);
                    break;
                default :
                    break;
            }
    
            if (value >= 'a') {   // avoid delaying on newlines
                delay(100); 
            }
        }
    }