Vending project

i am using hc05 Module Bluetooth

and trying this code

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("waitning for input");

  if (Serial.available() > 0) {
    if (Serial.read() == 1) {
      Serial.println("pressed 1,kitkat");
      delay(2000);

    } else if (Serial.read() == 2) {
      Serial.println("pressed 2,kinder");
      delay(2000);

    } else if (Serial.read() == 3) {
      Serial.println("pressed 3,mentos");
      delay(2000);

    } else if (Serial.read() == 4) {
      Serial.println("wrong");
      delay(2000);

    } else {
      Serial.println("ERROR, INVALID KEY");
      delay(2000);
    }
  }
}

and when i press say 1 get 49

1
waitning for input
-1
waitning for input
-1
waitning for input
49
ERROR, INVALID KEY
waitning for input

press 2 and gets

waitning for input
-1
waitning for input
50
ERROR, INVALID KEY
waitning for input
-1
waitning for input
-1

thought the Serial.read() function will show the
appropriate number , 1 to 1 etc.
but in effect showing different number completely.

but even when i try to bypass

if (Serial.read() -48 == 1) {
      Serial.println("pressed 1,kitkat");
      delay(2000);
 }

i still get the error

1
waitning for input
-1
waitning for input
-1
waitning for input
49
ERROR, INVALID KEY
waitning for input

what am i missing?

yeah i am a moron just discovered this
but still has a problem
when press 1
get

waitning for input
pressed 1,kitkat
waitning for input
ERROR, INVALID KEY
waitning for input
ERROR, INVALID KEY
waitning for inpu

press 2

waitning for input
pressed 2,kinder
waitning for input
ERROR, INVALID KEY
waitning for input
ERROR, INVALID KEY
waitning for input

why the else statement gets executed?

49 - 48 = 1
50 - 48 = 2
     48 = 0

didnt fully get that

okay
so did ---no line ending
and still get that else block executed.

That is the CR/LF from the Serial Monitor. Close and re-open the Serial Monitor.
Remove "delay();" lines from your sketch, Serial.available() will wait for you.

Your sketch is reading the Serial line four times, expecting four inputs. You should assign a "char" variable to read Serial one time after Serial.available() > 0...

  if (Serial.available() > 0) {
    char a = Serial.read();

Then compare "a" to your keyboard values (49, 50, 51, 52)

If you put an extra Serial.read() before the error, you will take that CR/LF away...

    } else {
      Serial.read();
      // Serial.println("ERROR, INVALID KEY");
1 Like

hey
sorry for the misfire from before.
and again thanks. the sketch we worked on works.
also learned a lot in the process.
so again thanks.

and i will try that.

truth be told its a friend thing and he has a problem with the screen capture so
he sent this

language barrier
we working this bottom up. starting with BT.
basically thats it. for now.
and screen cap of the serial monitor including the code?

so we tried this

char datarecieved;
void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("waitning for input");
  if (Serial.available() > 0) {
    Serial.println(datarecieved);
    datarecieved = Serial.read();
    if (datarecieved == '1') {
      Serial.println("pressed 1,kitkat");


    } else if (datarecieved == '2') {
      Serial.println("pressed 2,kinder");

    } else if (datarecieved == '3') {
      Serial.println("pressed 3,mentos");

    } else if (datarecieved == '4') {
      Serial.println("wrong");


    } else {
      Serial.println("ERROR, INVALID KEY");
      Serial.read();
    }
  }
  delay(1000);
}

and getting

pressed 1,kitkat
waitning for input
1
ERROR, INVALID KEY
waitning for input
waitning for input
waitning for input

it was one less ERROR message , but still there.

tried one without the last delay, but the screen ran too fast to capture what happens.

  • remove the delay();
  • Verify your CR/LF is the correct setting in your IDE.
  • Move "char datareceived" into "loop()" to make it a local variable to loop()
  • Move "waiting for input" line into "setup()" because it only needs to be printed one time.
  • remove the else() block with INVALID KEY
  • remove this: Serial.println(datarecieved)
void setup() {
  Serial.begin(9600);
  Serial.println("waitning for input");
}

void loop() {
  char datarecieved;
  if (Serial.available() > 0) {
    // Serial.println(datarecieved);
    datarecieved = Serial.read();
    if (datarecieved == '1') {
      Serial.println("pressed 1,kitkat");


    } else if (datarecieved == '2') {
      Serial.println("pressed 2,kinder");

    } else if (datarecieved == '3') {
      Serial.println("pressed 3,mentos");

    } else if (datarecieved == '4') {
      Serial.println("wrong");

      // } else {
      //   Serial.println("ERROR, INVALID KEY");
      //   // Serial.read();
    }
  }
  // delay(1000);
}

you can't answer my simple question about where the 1, 2 , 3 is coming from.

its coming from the paired phone, if thats what you mean.
by

you can't answer my simple question about where the 1, 2 , 3 is coming from

I wanted to look at your serial monitor settings but I guess you're not going to show me that either.
thought you meant the log itself.
here the log with the setting


hope thats what you meant.

And now you've gone to changing random things and getting the issue even more confused.

thats because as you said in post 2, its reading characters.
so we fixed just that.

thats the result now

pressed 1,kitkat
pressed 2,kinder
pressed 3,mentos
wrong

works better.
what should replace the lase else statement, to indicate the key was wrong?
the 4 key suppose to be another item.

Verify your CR/LF is the correct setting in your IDE.
it currently on "no line ending".
how do i know the correct one.

Nothing happens when the wrong key is pressed. It just waits for another key.

That is because your code has "4 = wrong"

yeah, but shouldn't the user be aware that he pressing an out of range key?

That is because your code has "4 = wrong"
yeah,
thats was a mistake. there was suppose to be a forth item.

That is up to you and your code.

Hello zuczac12

Keep it simple as possible:

void setup() {
  Serial.begin(9600);
  Serial.println("waitning for input");
}

void loop()
{
  if (Serial.available() > 0)
  {
    switch (Serial.read())
    {
      case '1':
        Serial.println("pressed 1,kitkat");
        break;
      case '2':
        Serial.println("pressed 2,kinder");
        break;
      case '3':
        Serial.println("pressed 3,mentos");
        break;
      case '4':
        Serial.println("pressed 4,wrong");
        break;
    }
  }
}

hth

1 Like

yeah that was a mistake, he fixed it.
supposed to be a forth item there.

yeah .
the simpler. will try

If you want the "waiting for input" before every input, see how this sketch uses a function to be called after every key input...

void setup() {
  Serial.begin(9600);
  Serial.print("waitning for input: ");
}

void loop() {
  if (Serial.available() > 0) {
    switch (Serial.read()) {
      case '1': Serial.println("pressed 1, kitkat"); printWait(); break;
      case '2': Serial.println("pressed 2, kinder"); printWait(); break;
      case '3': Serial.println("pressed 3, mentos"); printWait(); break;
      case '4': Serial.println("pressed 4, wrong"); printWait(); break;
    }
  }
}

void printWait() {
  Serial.print("waitning for input: ");
}
2 Likes