Go Down

Topic: How to turn on/off LED using String? (Read 1 time) previous topic - next topic

jackwong0099

Hey guys, so I was assigned by my teacher to turn on/off a LED using String() command just wanted to know if there are any sources or reference available. Example: 
COM4 (On Off On Off) LEDs.

bubulindo

This... is a hobby.

Nick Gammon

I doubt he actually said that, so you are interpreting a bit, right? String functions deal with, well, strings. That is nothing to do with LEDs.

Would you by any chance have to send a command from one Arduino to another?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

zoomkat

Simple code that you might be looking for.

Code: [Select]

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3); 
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  }
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

jackwong0099

sorry for the late reply.
Code: [Select]
const int ledPin = 13;
String incomingByte; 
void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);

}

void loop() {
  if (Serial.available() > 0) {

    incomingByte = Serial.read();

    if (incomingByte == 'On') {
      digitalWrite(ledPin, HIGH);
    }
    if (incomingByte == 'OF') {
      digitalWrite(ledPin, LOW);
    }

  }
}

dxw00d

Code: [Select]
    incomingByte = Serial.read();

    if (incomingByte == 'On') {


A single byte is never going to be equal to 'On' or 'OF'.

Go Up