So in my code I need to send a number between 1 and 100. However, when i send a number over 9 it separates it into two different digits when it receives it. I cant understand how to fix this. Thanks in advance. Also, in the code I know i have set the max to 10 but that is for testing purposes only.
Great i saw this post in paid section. So I decided to make code and now i see, moderator closed that lol. So here you have code for which i spent my time. Happy school homework!
And also. Don't post half of codes. You haven't posted (in paid section) setup function, global variables, etc.
/*|----------------------------------------------------------|*/
/*|SKETCH FOR SIMPLE GUESSER |*/
/*|Author: Bc. MARTIN CHLEBOVEC (martinius96) |*/
/*|EMAIL: martinius96@gmail.com |*/
/*|WEB: https://arduino.php5.sk?lang=en |*/
/*|----------------------------------------------------------|*/
const int greenLed = 4;
const int redLed_1 = 5;
const int redLed_2 = 6;
const int yellowLed = 7;
int secretNumber;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("New Game!");
delay(700);
Serial.println("Goal: Guess the secret number.");
delay(700);
Serial.println("What is your guess?");
// initialize each of the digital pins as an output
pinMode(greenLed, OUTPUT);
pinMode(redLed_1, OUTPUT);
pinMode(redLed_2, OUTPUT);
pinMode(yellowLed, OUTPUT);
randomSeed(analogRead(A0)); // This seeds the secret number.
secretNumber = random(101); // Secret number from range: 0-100
Serial.println("SECRET NUMBER IS: ");
Serial.println(secretNumber);
while (Serial.available() <= 0) {
Serial.println(" GUESS THE NUMBER! ");
delay(1000);
}
}
void loop()
{
digitalWrite(redLed_1, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(redLed_2, LOW);
digitalWrite(yellowLed, LOW);
if (Serial.available() > 0) {
// read the input from keyboard (the guess)
long theGuess = Serial.parseInt(); //long because of parseInt is long!!!
// The Guess is printed to the serial monitor
Serial.print("You Guessed ");
Serial.println(theGuess);
if (theGuess == secretNumber) {
digitalWrite(greenLed, HIGH);
Serial.println("You won !");
while (1);
}
if (theGuess > secretNumber) {
digitalWrite(redLed_1, HIGH);
Serial.println("Your number is higher than secret number !");
}
if (theGuess < secretNumber) {
digitalWrite(redLed_2, HIGH);
Serial.println("Your number is lower than secret number !");
}
if (theGuess >= (secretNumber - 2)) {
digitalWrite(yellowLed, HIGH);
Serial.println("Your are so close! Keep trying");
}
}
}
Gurveer777:
So in my code I need to send a number between 1 and 100. However, when i send a number over 9 it separates it into two different digits when it receives it.
That's what Serial.print() is intended to do. For example "73" is the two characters '7' and '3'. If you want to send a byte value (i.e. a value between 0 and 255) use Serial.write().
However it makes debugging much easier if you send data as human readable text. I would only send binary data if it was the only way to achieve the required performance.
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.
You can send data in a compatible format with code like this (or the equivalent in any other programming language)
I think i wrote it wrong. There is section named Gigs & Collaborations. Where people write gigs about what they want to be created, etc... And then people offer them solution for money