Go Down

Topic: "Read ASCII String" tutorial & expand it to do more (combine with another code) (Read 4129 times) previous topic - next topic

desibee

Hello, first of all let me say that I'm a noob on arduino and coding. This is a hobby for me. What I do is examining tutorials and sample codes and make some changes/add new functions to that codes or combine them to my needs. Sometimes I hit a wall.
I posted here because it's about controlling leds, also couldn't find serial communication section.

I looked at this tutorial to send ascii strings to arduino here.

In this example, arduino takes comma separeted values and parses them into integers for each led and leds respond to individual ints. OK so far. Upload the code, makes the circuit and it works as described.

What I'm trying to do is add new functions to that code.
Let say: if I send 255,0,255 in serial monitor(Newline) I get a color purple and so on...
What I want to add is to send a specific command via serial like "case1" and arduino to do that specific function i it gets that code.

I'm trying to combine these two codes:  (.ino attachments added.)

Code 1:
(working perfect)
send "LEDon" command via serial monitor(No line ending)and all leds are on,
send "LEDoff" and all are off

Code: [Select]


const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
String readString;

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  // test each led for 500 miliseconds:
  digitalWrite(redPin, HIGH);
  delay(500);
  digitalWrite(redPin, LOW);  
  digitalWrite(greenPin, HIGH);
  delay(500);
  digitalWrite(greenPin, LOW);  
  digitalWrite(bluePin, HIGH);
  delay(500);
  digitalWrite(bluePin, LOW);
  // start serial:
  Serial.begin(9600);
}

void loop()
{
    while (Serial.available() > 0)
    {
    delay(3);  
    char c = Serial.read();
    readString += c;
    }
        if (readString.length() > 0)
           {
               if (readString == "LEDon")    
                   {
                    digitalWrite(redPin, HIGH);
                    digitalWrite(greenPin, HIGH);
                    digitalWrite(bluePin, HIGH);
                    }
               if (readString == "LEDoff")
                    {
                    digitalWrite(redPin, LOW);
                    digitalWrite(greenPin, LOW);
                    digitalWrite(bluePin, LOW);
                    }                
                    readString="";
            }
 }





Code 2:
(working perfect) Original code here.
send RGB values via serial monitor(Newline) like 255,255,255 and leds respond to that color

Code: [Select]


// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

void setup() {
   // make the pins outputs:
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
  // test each led for 500 miliseconds:
  digitalWrite(redPin, HIGH);
  delay(500);
  digitalWrite(redPin, LOW);  
  digitalWrite(greenPin, HIGH);
  delay(500);
  digitalWrite(greenPin, LOW);  
  digitalWrite(bluePin, HIGH);
  delay(500);
  digitalWrite(bluePin, LOW);
  // start serial:
  Serial.begin(9600);}

void loop() {

  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int red = Serial.parseInt();
    // do it again:
    int green = Serial.parseInt();
    // do it again:
    int blue = Serial.parseInt();

    // look for the newline. That's the end of your
    // sentence:
    if (Serial.read() == '\n') {
      // constrain the values to 0 - 255 and invert
      // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
      red = constrain(red, 0, 255);
      green = constrain(green, 0, 255);
      blue = constrain(blue, 0, 255);

      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);
    
      // print RGB value:
      Serial.print(red);
        Serial.print(",");
      Serial.print(green);
         Serial.print(",");
      Serial.println(blue);

      
    }
  }
}




My combined code:
send "LEDon" command via serial monitor(No line ending) and all leds are on,
send "LEDoff" and all are off,
send "case1" and arduino waits for comma seperated RGB values,
send "case2" and arduino does another thing,
send "LEDon" again and arduino stops waiting for comma seperated RGB values etc.

(only LEDon and LEDoff commands work (No line ending) )

Code: [Select]


const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
String readString;

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  // test each led for 500 miliseconds:
  digitalWrite(redPin, HIGH);
  delay(500);
  digitalWrite(redPin, LOW);  
  digitalWrite(greenPin, HIGH);
  delay(500);
  digitalWrite(greenPin, LOW);  
  digitalWrite(bluePin, HIGH);
  delay(500);
  digitalWrite(bluePin, LOW);
  // start serial:
  Serial.begin(9600);
}

void loop()
{
    while (Serial.available() > 0)
    {
    delay(3);  
    char c = Serial.read();
    readString += c;
    }
        if (readString.length() > 0)
           {
               if (readString == "LEDon")    
                   {
                    digitalWrite(redPin, HIGH);
                    digitalWrite(greenPin, HIGH);
                    digitalWrite(bluePin, HIGH);
                    }
               if (readString == "LEDoff")
                    {
                    digitalWrite(redPin, LOW);
                    digitalWrite(greenPin, LOW);
                    digitalWrite(bluePin, LOW);
                    }
               if (readString == "case1")
                    {
                    case1();
                    }
               if (readString == "case2")
                    {
                    case2();
                    }
               if (readString == "case3")
                    {
                    case3();
                    }                    
                    readString="";
            }
 }
 
 void case1()
{
 //if there's any serial available, read it:
 while (Serial.available() > 0)
 {
 int red = Serial.parseInt();
    // do it again:
    int green = Serial.parseInt();
    // do it again:
   int blue = Serial.parseInt();

    // look for the newline. That's the end of your
    // sentence:
    if (Serial.read() == '\n')
    {
      // constrain the values to 0 - 255 and invert
      // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
      red = constrain(red, 0, 255);
      green = constrain(green, 0, 255);
      blue = constrain(blue, 0, 255);

      // fade the red, green, and blue legs of the LED:
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);

  }
}
}
 
void case2()
{
  //do case2
}

void case3()
{
  //do case3
}



What am I missing here? Any help, code, tutorial is appreciated. Thanks.

yck011522

Not very sure. I'm guessing here.

I think your if statement works. Please try make arduino do something simpler in case1() to confirm that the if statement is working. Say blink an LED or serial write something back to your PC. My guess is that its working.

What is not working is perhaps the while (Serial.available() > 0)  in your case1(). You send your "case1" text to your Arduino and it enters case1() sub, but it didn't really wait for you, because there is nothing in Serial buffer anymore. So it jumps out of case1() immediately. You need to add the actual wait.

Try send "case1255,0,255" Since you are saving the Serial char to your read buffer String readString; one char at a time. You will jump into the if statement once you detected the "case1" string in yout readString. The remaining "255,0,255" should still be in your hardware Serial buffer. So your case1() might work by detecting that Serial.available() > 0.

Go Up