1
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
    What does your "Number received:" tell you when it fails? – Majenko Jun 7 at 19:46
  • When it fails it has a 0 value, but before sometimes I was getting -1. – Madison Chodikov Jun 7 at 19:48
  • What data type is mode? – Majenko Jun 7 at 19:51
  • Mode has the data type of char. I have it declared as a global variable above the loop – Madison Chodikov Jun 7 at 19:52
  • 1
    Tested it with a dummy implementation of sensor. Works as expected. The problem must be in a part of the code you are not showing. – Edgar Bonet Jun 7 at 20:28
0

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 Jun 11 at 18:39

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.