2

so I need to make a arduino program, generates 2 random numbers, makes a + calculation with them and asks user the right answer. If the answer is right AKA Calculation==User Answer, then led is set to LOW, and serialprint sent. If its wrong, Led lights up and serialprint sent again. It works if i set the variables manually etc. But the I think the problem is with my serialRead, somehow it doesnt set the value it got from the user as the value of the variable (Luku4)

Here is the code, I hope you can help me.

int Luku1 = 0;
int Luku2 = 0;  
int Luku3 = 0;  //Summa
int Luku4 = 0; //Vastaus
const int ledPin = 13;
byte i = 0;

void setup() {

  for (i=0; i<4; i++){
pinMode(ledPin, OUTPUT); 
}

randomSeed(analogRead(0));
Serial.begin(9600);
Serial.println("Arvotaan 2 satunnaislukua ja summataan ne yhteen. Kerro oikea vastaus");
}



void loop() {
  Serial.println("Arvotaan 2 satunnaislukua");
  Luku1 = random(1,5);
  Luku2 = random(1,5);
  Serial.println(Luku1);
  Serial.println("+");
  Serial.println(Luku2);
  Luku3 = Luku1 + Luku2;
  delay(100);
  Serial.println("Mika on vastaus?");
  Luku4 = Serial.read();
    while(Serial.available() == 0){}

  if (Luku4==Luku3) {
    digitalWrite(ledPin, LOW);
    Serial.println("Oikea vastaus");
    delay(2000);
  }

  else{
    digitalWrite(ledPin, HIGH);
    Serial.println("Vaarin");
    delay(2000);
    digitalWrite(ledPin, LOW);

  }
}
  • 1
    You should move the Serial.read to after the "wait for serial input"-line. Secondly. Serial.read will return a character, not a number. So you have to convert the ascii value of the letters "0"-"9" to the integer value of 0-9. This can be done by subtracting 48, as "0" is ascii code nr 48. – Gerben Apr 24 '18 at 12:37
  • Oh, never heard of that. Thanks alot, it worked! – Kalle Perälä Apr 24 '18 at 12:48
  • 1
    but it still works only for one digit and not for a number. use parseInt arduino.cc/reference/en/language/functions/communication/serial/… – Juraj Apr 24 '18 at 13:06
  • These could all by byte, vs int, as they will not be more than 255:int Luku1 = 0; int Luku2 = 0; int Luku3 = 0; //Summa int Luku4 = 0; //Vastaus const int ledPin = 13; – CrossRoads Apr 24 '18 at 13:13
  • And why do you need to set this pin to an output 4 times? Once is enough for (i=0; i<4; i++){ pinMode(ledPin, OUTPUT); } – CrossRoads Apr 24 '18 at 13:15
2

You are comparing a byte which was read through uart with a number. If you send number '5' through uart, you won't get number 5, but character '5'. To convert single character to number, you can do following:

Luku4 = Serial.read(); // '0' .. '9'
Luku4 = Luku4 - '0'; // 0..9

But this will work only for single digit numbers, for more robust solution please refer to readString function to see how you can read strings from UART and to function atoi which converts string to number.

1

You are reading Serial before you check that there is anything valid to read:

Luku4 = Serial.read();
while(Serial.available() == 0){}

Try swapping those two lines.

  • Swapped, Still not working. Only gives the "else", no matter what I input. Thanks for the help still. – Kalle Perälä Apr 24 '18 at 12:38
0

Try changing these lines

Luku4 = Serial.read();
while(Serial.available() == 0){}

to these lines

while(Serial.available() == 0){}
Luku4 = Serial.parseInt();


As shown in the Arduino documentation, Serial.parseInt() will read a number from Serial. If a non-number character is entered it will return 0.

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.