void loop()
{
if (sensor.begin() == true)
{
Serial.print("Current Conversion Mode: ");
Serial.println(sensor.getConversionMode());
Serial.println("Enter your mode of Conversion (number 0 - 3): ");
while (Serial.available() == 0); // Waits for the user input
mode = Serial.read(); // Reads the input string from serial port
Serial.print("Number recieved: ");
Serial.println(mode);
delay(500);
if (mode == '0' | mode == '1' | mode == '2' | mode == '3')
{
sensor.setConversionMode(mode);
Serial.println();
delay(500);
}
else
{
Serial.println("Conversion mode unsuccessfully set - Please enter a number 0 - 3");
}
delay(1000);
}
else // Runs when the device was unable to setup properly
{
Serial.println("Device failed to setup");
}
}
I'm currently using the SparkFun red board to run this code on. I have been trying to get this to read from the serial monitor to take an input to send to the library I wrote to change the value of the conversion mode register of a temperature sensor. When I run the code, I can only have it run once correctly, then it jumps directly to the else statement for the inputs, saying that 0-3 was not an input. I tried the other methods that people have suggested from this website and the Arduino forums but none of them have worked for me yet. I'm not sure if adding more delays would help - that was one of the most common suggestion I found for this specific issue and it wasn't that consistent. Please help! Thanks.
1 Answer 1
Using a slightly modified version of your sketch, I receive the following output in the serial monitor when I enter the number 2.
Current Conversion Mode: Enter your mode of Conversion (number 0 - 3): Number recieved: 2
Current Conversion Mode: Enter your mode of Conversion (number 0 - 3): Number recieved:
Conversion mode unsuccessfully set - Please enter a number 0 - 3 Current Conversion Mode: Enter your mode of Conversion (number 0 - 3): Number recieved:
Conversion mode unsuccessfully set - Please enter a number 0 - 3 Current Conversion Mode: Enter your mode of Conversion (number 0 - 3):
If you change mode
to an int
then retest, you'll see the second and third "Number recieved" are 10 and 13. That would be the New Line and Carriage Return characters. When you send data via the serial monitor, do you have "No Line Ending" selected or something else?
Here is a test sketch:
char mode;
void setup(){
Serial.begin(9600);
}
void loop()
{
if (true)
{
Serial.print("Current Conversion Mode: ");
//Serial.println(sensor.getConversionMode());
Serial.println("Enter your mode of Conversion (number 0 - 3): ");
while (Serial.available() == 0); // Waits for the user input
mode = Serial.read(); // Reads the input string from serial port
Serial.print("Number recieved: ");
Serial.println(mode);
delay(500);
if (mode == '0' | mode == '1' | mode == '2' | mode == '3')
{
//sensor.setConversionMode(mode);
Serial.println();
delay(500);
}
else
{
Serial.println("Conversion mode unsuccessfully set - Please enter a number 0 - 3");
}
delay(1000);
}
else // Runs when the device was unable to setup properly
{
Serial.println("Device failed to setup");
}
}
-
Thank you for the suggestion, it worked! I just changed the the value to an int and then changed the serial monitor to "No Line Ending" and it worked. I never changed that setting before or really realized it was there. Thanks again!Madison Chodikov– Madison Chodikov2019年06月11日 18:39:50 +00:00Commented Jun 11, 2019 at 18:39
Explore related questions
See similar questions with these tags.
mode
?sensor
. Works as expected. The problem must be in a part of the code you are not showing.