0

Here is my challenge: I have to create a sketch where I would have a prompt asking the user to enter an ID. If it is successful, it would turn on the green LED and response with "Hello, your name is so-so." If it failed, it would turn on the red LED and response with "Invalid username! Try again."

The code is here:

//Establish myID as CHAR data type
 char myID = 'tg123';
 //Establish testID to receive data to be tested against myID
 char testID;

 //Establish green and red LEDs to PWM pins
 const int gLED = 10;
 const int rLED = 5;

 void setup()
 {
  //Establishing baud rate at 9600
  Serial.begin(9600);
  pinMode(gLED, OUTPUT);
  pinMode(rLED, OUTPUT);  
 }

 void loop()
 {   
  //Act when there are data in the buffer
  while (Serial.available())
  { 
    testID = Serial.read();
    //If data = myID, turn on green LED and acknowledge
    if (testID == myID)
    {
      digitalWrite(gLED, HIGH);
      digitalWrite(rLED, LOW);
      Serial.println("Hello! Your name is Trevor.");
      delay(5000);
    }
    else
    {
      digitalWrite(rLED, HIGH);
      digitalWrite(gLED, LOW);
      Serial.println("Invalid ID! Try again.");
      delay(2000);
    }
  }
 }

Two problems I have had with this:

  1. The damn thing would print the line "Please enter your ID:" infinitely if it was inside the loop, so I got fed up and moved it to the set up, and
  2. no matter what I enter in the serial monitor, it does not do anything.

Update: After reading Arduino's website, I found I can predefine the char data type by adding char myID = 'tg123';. It is getting closer to achieving what I want, but it's exhibiting some strange behavior. When I type in exactly tg123 in the serial monitor, it would report the error message as defined in the sketch before passing.

  • Your sketch assumes that the ID is a single character. If that is the case then comparing with 'tgreen123' will not work very well. – Mikael Patel May 6 '17 at 21:40
  • I have updated the original post. – Trevor May 6 '17 at 23:22
  • Your question has been answered (twice). Please, accept one of them to close this question. – user31481 Nov 17 '17 at 9:11
1

You are assigning a string to a single character here.

char myID = 'tg123';

Instead you should make it a String variable. And compare it after all the characters are recieved.

String myID = "tg123";

And Here is the modified code you should test with. In this case I have assumed you simply write the ID (string) in serial terminal and hit enter.

N.B. If you want to enter each character one by one, then it will not be able to catch the whole string. For this case you have to add some time-out to wait for next character to arrive.

String recvData = "";
void loop()
{    
  while (!Serial.available());
  while (Serial.available())
  { 
    char ch = Serial.read();
    recvData += ch;
  }
  if (myID == recvData)
  {
    digitalWrite(gLED, HIGH);
    digitalWrite(rLED, LOW);
    Serial.println("Hello! Your name is Trevor.");
    delay(5000);
  }
  else
  {
    digitalWrite(rLED, HIGH);
    digitalWrite(gLED, LOW);
    Serial.println("Invalid ID! Try again.");
    delay(2000);
  }
  recvData = "";
 }
0

When you use the Arduino IDE to compile your code for an Uno, it will tell you

Sketch uses 2106 bytes (6%) of program storage space. Maximum is 32256 bytes.
Global variables use 237 bytes (11%) of dynamic memory, leaving 1811 bytes for local variables. Maximum is 2048 bytes.

as if there had been a clean and correct compilation. However, if you compile it via make and a makefile (see, eg, sudar's Arduino Makefile) you will see a warning like the following:

stringInit1.ino:2:14: warning: character constant too long for its type [enabled by default]
char myID = 'tg123';

where stringInit1 stands for whatever you call your sketch.

The char data type stands for a single character. To represent a text string, use either an array of characters (for example, char *myID = "tg123" or char myID[] = "tg123") or a String type (which, in general, is problematic on small-RAM embedded processors).

Then, to make the comparison, keep track of how many characters have been input so far, and as you input each character, compare it to the current character of myID, clearing a match-flag if characters don't match. Eg, if (testChar != myID[currentCount]) matchFlag = false;. Note, initialize currentCount to zero and matchFlag to true in setup() and before each retry.

  • I agree. Serial.read() will only bring in a single character. You do have the option of using Serial.readstring, but then you can't compare it to chars. I like @James' idea of just checking with each character whether or not you have the whole ID. Then you wouldn't even need to press enter when you have it right. But you could type the entire constitution of the US, newlines and all, and the program would do nothing. I also agree that you may have an uploading error because you say it prompts you for your ID but I don't see that in the code. Might be an older version that is running. – SDsolar May 7 '17 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.