Go Down

Topic: NOT getting how to use SERIAL PORT as a passkey INPUT (Read 517 times) previous topic - next topic

Adivijaya

Hello World ,
I am recently facing a problem in a program in which I have to take input from the user using the Serial port but i am not able to process the input properly
PLEASE HELP ME!!

Code: [Select]

String pass=0;
void setup()
{
   Serial.begin(9600);
   pinMode(13,OUTPUT);
}
void loop()
{
   if(Serial.available()>0)
   {
   pass=Serial.read();
    if(pass=="1234")
    {
    Serial.println("Correct passkey");
    digitalWrite(13,HIGH);
   }
   else if(pass!="1234")
  {
   Serial.println("Incorrect passkey");
  }
}


UKHeliBob

Serial.read(); reads a single character so what is read will never equal "1234"
Have a look at the Serial.readStringUntil() function

Even better, look at Serial input basics - updated and don't use Strings at all
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

zoomkat

Simple serial echo test code that captures characters and sends them back to the serial monitor.

Code: [Select]
//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(3);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() > 0) {
    Serial.println(readString);  //so you can see the captured String
    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

Go Up