Need help with my programming. Reads a LM335 and controls a LED.

I have attached both my schematic and my coding for my sketch.

Purpose of sketch:
To control through a serial input command using either a 1, 2, or 3 as an input integer that would either taking a one time reading from the LM335 or turn the led on or off.

Eventually I would like to be able to have the user input actual words to create these actions, such as an answer of “yes” rather then a “1” so any input on that also would be appreciated.

Problem:
Using the code below when I upload the sketch and go to the serial monitor and I get the following.

What would you like to do?
1 = Read Temp
2 = Turn Light On
3 = Turn Light Off

Any input of 1, 2, or 3 all result in the above being printed again but with no temp reading or action from the LED. Afterwards the above does not repeat and the 1, 2, 3 inputs don’t even garner a response.

Once I get this working I will be constantly building off of it with a more options. This code is to control a model home with lights, sensors, servos, etc. Any input for the future of this code would be appreciated also.

int tsensePin = 0; //Pin that the LM335 data is sent to
int temp_sense_input = 0;
int message = 0;
int prompt1 = 0;
int LED = 9; //Pin the LED is connected to
void setup()
{
  Serial.begin(9600);
  pinMode(LED,OUTPUT);
  Serial.println("What would you like to do?"); 
  Serial.println("1 = Read Temp"); 
  Serial.println("2 = Turn Light On"); 
  Serial.println("3 = Turn Light Off");
  

}
void loop() 
{ 
   prompt1 = Serial.read();//Attempt at fixing problem by using this instead of whats in the 2 /* */ Locations
    if(Serial.available() > 0) 
    { 
    /*prompt1 = readSerial();*/                  //Replaced this and its function below with the prompt1 above
        if(prompt1 = 1)
        {
          readtemp();
        }
        if(prompt1 = 2)
        {
          light_on();
        }
        if(prompt1 = 3)
        {
          light_off();
        }
  Serial.println("What would you like to do?"); 
  Serial.println("1 = Read Temp"); 
  Serial.println("2 = Turn Light On"); 
  Serial.println("3 = Turn Light Off");
    }
}


/*int readSerial()     //reads the "1" from the serial to start the readtemp function
  {
  int msg = 0;            
  msg = Serial.read();   
  delay(1);
  return msg;
}
 */

void readtemp()
{                                      //function starts
int tempsense = analogRead(tsensePin); //reads voltage on Pin A0
float millivolts = (tempsense / 1024.0) * 5000;//convert pin A0 to millivolts
float celsius_value = (millivolts / 10.0) - 273.15; //sensor output is 10mV per degree Celsius
float fahrenheit_value = ((celsius_value)*(9.0/5.0)) + 32.0;//convert to fahrenheit

Serial.println();                         //print out the temps     
Serial.print("Celsius:");
Serial.println(celsius_value);
Serial.print("Fahrenheit:");
Serial.println(fahrenheit_value);
}

void light_on()
{
      digitalWrite(LED,HIGH);
    Serial.println("Light is ON");
}

void light_off()
{
      digitalWrite(LED,LOW);
    Serial.println("Light is Off");
}

Senior Project Schematic.fzz (12.2 KB)

LM335_With_LED.ino (1.93 KB)

   prompt1 = Serial.read();//Attempt at fixing problem by using this instead of whats in the 2 /* */ Locations
    if(Serial.available() > 0) 
    {

Read something, and then see if there was something to read. Oh, yeah. Great idea.

        if(prompt1 = 1)
        {
          readtemp();
        }
        if(prompt1 = 2)
        {
          light_on();
        }

This is not right. That assigns, not tests. Thus after the first line prompt1 will definitely be 1.

if(prompt1 = 1)

1 and '1' are two different values, muck like = and == or two different operators.

   prompt1 = Serial.read();//Attempt at fixing problem by using this instead of whats in the 2 /* */ Locations
    if(Serial.available() > 0)

If one byte was available initially, the Serial.read will consume it. Then the test for "if available" will fail.

Sorry still a beginner at this and I hate to bother people on forums but I am really stuck on this. I have 2 seperate sketches that control either the LED or the LM335, however I need them combined and this is what I came up with in one day, while learning as I go.

if(prompt1 == 1)
        {
          readtemp();
        }

Ive changed my if statements to use == because I realize now I wasnt comparing, but setting the values; however this still did not fix my problem.

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

Also I have switched where the prompt1 from above the if statement to underneath. I see what PaulS was saying about it, however I still don’t fully understand how those functions work so I am not sure if this helped or did anything at all. I will continue to do some research and check other topics on the forum to see if I can figure it out.

Forgot to add that I put this in Serial.print(readSerial()); after the 3 if statements and the 4 println statements prompting the user what they would like to do to see what exactly my prompt1 value is. In the serial monitor I am seeing a -1 return value.

If you've edited your code, post the updated code. From the looks of it though, you ignored my post.

Currently messing around with it trying different things to learn and attempt to fix it. From the Serial.println(readSerial()); in the for loop() I get a reading in the monitor of the corresponding ASCII number except when I insert the prompt1 = readSerial(). Still trying to manipulating things and see what the response is.

I see what you mean now Arrch about the 1 not equaling '1' and I am reading about that as well.

int tsensePin = 0; //Pin that the LM335 data is sent to
int temp_sense_input = 0;
int message = 0;
int prompt1 = 0;
int LED = 9; //Pin the LED is connected to
void setup()
{
  Serial.begin(9600);
  pinMode(LED,OUTPUT);
  /*Serial.println("What would you like to do?"); 
  Serial.println("1 = Read Temp"); 
  Serial.println("2 = Turn Light On"); 
  Serial.println("3 = Turn Light Off");
  Serial.println(readSerial());
*/
}
void loop() 
{ 
   
    if(Serial.available() > 0) 
    { 
      
  Serial.println("What would you like to do?"); 
  Serial.println("1 = Read Temp"); 
  Serial.println("2 = Turn Light On"); 
  Serial.println("3 = Turn Light Off");
  Serial.println(readSerial());
    //prompt1 = readSerial();  this line results in the Serial.println(readSerial()) giving me a -1                 
        
       
        if(readSerial() == '1')
        {
          readtemp();
        }
        
        if(prompt1 == 2)
        {
          light_on();
        }
        if(prompt1 == 3)
        {
          light_off();
        }
    

    }
}


int readSerial()     //reads the "1" from the serial to start the readtemp function
  {
  int msg = 0;       //ASCII codes for the digits 0 through 9 is 48 through 57     
  msg = Serial.read();   
  delay(1);
  return msg;
}

void readtemp()
{                                      //function starts
int tempsense = analogRead(tsensePin); //reads voltage on Pin A0
float millivolts = (tempsense / 1024.0) * 5000;//convert pin A0 to millivolts
float celsius_value = (millivolts / 10.0) - 273.15; //sensor output is 10mV per degree Celsius
float fahrenheit_value = ((celsius_value)*(9.0/5.0)) + 32.0;//convert to fahrenheit

Serial.println();                         //print out the temps     
Serial.print("Celsius:");
Serial.println(celsius_value);
Serial.print("Fahrenheit:");
Serial.println(fahrenheit_value);
}

void light_on()
{
      digitalWrite(LED,HIGH);
    Serial.println("Light is ON");
}

void light_off()
{
      digitalWrite(LED,LOW);
    Serial.println("Light is Off");
}
  Serial.println(readSerial());
    //prompt1 = readSerial();  this line results in the Serial.println(readSerial()) giving me a -1                 
        
       
        if(readSerial() == '1')

Reading consumes the character, you can't read it twice like that.

Read the character. Then print it. Then choose what to do with it.

Well got everything working and managed to clean up some of the code. Thank you everyone for the input and help. Arrch the whole 1 =/= '1' thing kept ringing in my ear and I went back to check how my sketch that only controls the leds works and saw they use if (input == '1' ) then everything clicked and I fixed it. Again if anyone has any ideas how to do make a cleaner interface for choosing selections please let me know because this is just a prototype. End game would have a house with 3 rooms where you can select a room and then withing that room have options to control lights, doors, temperature. Ive been looking at G4P tools to build a simple interface but that would be the last step after I get everything build. Thanks again and here is the working code!

int temp_sense_input = 0; //Pin that the LM335 data is sent to
int input = 0;
char  prompt = 0;
int LED = 9; //Pin the LED is connected to
char x1 = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(LED,OUTPUT);
  Serial.println("What would you like to do?"); 
  Serial.println("1 = Read Temp"); 
  Serial.println("2 = Turn Light On"); 
  Serial.println("3 = Turn Light Off");
}


void loop() 
{ 
    if(Serial.available() > 0) 
   {      
  Serial.println("What would you like to do?"); 
  Serial.println("1 = Read Temp"); 
  Serial.println("2 = Turn Light On"); 
  Serial.println("3 = Turn Light Off");
  //Serial.println(Serial.read()); //use for seeing what the ASCII read from serial monitor is         
        
       input = input_func(); //ASCII char from the function and uses it for if statements
        if(input == '1')     //All 3 if statements compare the ASCII to its 'x' equivalent for boolean check
        {
          readtemp();
        }
        
        if(input == '2')
        {
          light_on();
        }
        if(input == '3')
        {
          light_off();
        }
    }
}



int input_func()               //reads ASCII char and returns it as the input for the if statements
  {
  input = Serial.read();       //ASCII codes for the digits 0 through 9 is 48 through 57     
  }


void readtemp()
{
int tsensePin = 0;
int tempsense = analogRead(tsensePin); //reads voltage on Pin A0
float millivolts = (tempsense / 1024.0) * 5000;//convert pin A0 to millivolts
float celsius_value = (millivolts / 10.0) - 273.15; //sensor output is 10mV per degree Celsius
float fahrenheit_value = ((celsius_value)*(9.0/5.0)) + 32.0;//convert to fahrenheit

Serial.println();                         //print out the temps     
//Serial.print("Celsius:");
Serial.println(celsius_value);
Serial.print("Fahrenheit:");
Serial.println(fahrenheit_value);
}

void light_on()
{
      digitalWrite(LED,HIGH);
    Serial.println("Light is ON");
}

void light_off()
{
      digitalWrite(LED,LOW);
    Serial.println("Light is Off");
}
  Serial.println("What would you like to do?"); 
  Serial.println("1 = Read Temp"); 
  Serial.println("2 = Turn Light On"); 
  Serial.println("3 = Turn Light Off");
}


void loop() 
{ 
    if(Serial.available() > 0) 
   {      
  Serial.println("What would you like to do?"); 
  Serial.println("1 = Read Temp"); 
  Serial.println("2 = Turn Light On"); 
  Serial.println("3 = Turn Light Off");

Your “menu” is crying out here to be moved into its own function. Saves doing it all twice.

Thank you I will look into this another day for now just found out about some char string and shortened the code a tad.

int temp_sense_input = 0; //Pin that the LM335 data is sent to
int LED = 9; //Pin the LED is connected to
int input = 0;
char myString[] = "What would you like to do?\n"
                  " 1 = Read Temp\n"
                  " 2 = Turn Light On\n"
                  " 3 = Turn Light Off\n\n";

void setup()
{
  Serial.begin(9600);
  pinMode(LED,OUTPUT);
  Serial.println(myString); 
  delay (2000);
}


void loop() 
{ 
    if(Serial.available() > 0) 
   {      
   
        
       input = input_func(); //ASCII char from the function and uses it for if statements
        if(input == '1')     //All 3 if statements compare the ASCII to its 'x' equivalent for boolean check
        {
          readtemp();
        }
        if(input == '2')
        {
          light_on();
        }
        if(input == '3')
        {
          light_off();
        }
Serial.println(myString); 
  delay (2000);
  //Serial.println(input); //use for seeing what the ASCII read from serial monitor is      
        
    }
}


int input_func()               //reads ASCII char and returns it as the input for the if statements
  {
  input = Serial.read();       //ASCII codes for the digits 0 through 9 is 48 through 57     
  }


void readtemp()
{
int tsensePin = 0;
int tempsense = analogRead(tsensePin); //reads voltage on Pin A0
float millivolts = (tempsense / 1024.0) * 5000;//convert pin A0 to millivolts
float celsius_value = (millivolts / 10.0) - 273.15; //sensor output is 10mV per degree Celsius
float fahrenheit_value = ((celsius_value)*(9.0/5.0)) + 32.0;//convert to fahrenheit
                       //print out the temps     
Serial.print("Fahrenheit:");
Serial.println(fahrenheit_value);
Serial.println();
Serial.println();
delay (2000);
}

void light_on()
{
      digitalWrite(LED,HIGH);
    Serial.println("Light is ON\n\n");
    delay (2000);
}

void light_off()
{
      digitalWrite(LED,LOW);
    Serial.println("Light is Off\n\n");
    delay (2000);
}

Attempted this for multiple rooms but its just one big mess that isn't working, and I probable wont get it up and running until next week, but here it is for suggestions and the criticism.

int temp_sense_input = 0; //Pin that the LM335 data is sent to
int LED = 9; //Pin the LED is connected to
int input = 0;
static int input1 = 0;


void setup()
{
  Serial.begin(9600);
  pinMode(LED,OUTPUT);
  Serial.println("What would you like to do?"); 
  Serial.println("1 = Room#1"); 
  Serial.println();
  delay (2000);
}


void loop() 
{ 
    if(Serial.available() > 0) 
   {      
   
        
        static int input = input_func(); //ASCII char from the function and uses it for if statements
        if(input == '2')     //All 3 if statements compare the ASCII to its 'x' equivalent for boolean check
        {
          room1();
        }

    }
}


int input_func()               //reads ASCII char and returns it as the input for the if statements
  {
  input = Serial.read();       //ASCII codes for the digits 0 through 9 is 48 through 57     
  }

/*int input_room1()               //reads ASCII char and returns it as the input for the if statements
  {
  input1 = Serial.read();       //ASCII codes for the digits 0 through 9 is 48 through 57     
  }
*/

void room1()
{
  

  
        static int input1 = Serial.read();
        if(input1 == '1')     //All 3 if statements compare the ASCII to its 'x' equivalent for boolean check
        {
          readtemp();
        }
        if(input1 == '2')
        {
          light_on();
        }
        if(input1 == '3')
        {
          light_off();
        }

  Serial.println("What would you like to do?"); 
  Serial.println("1 = Read Temp"); 
  Serial.println("2 = Turn Light On"); 
  Serial.println("3 = Turn Light Off");
  Serial.println();
  delay (2000);
}
  
  
  
void readtemp()
{
int tsensePin = 0;
int tempsense = analogRead(tsensePin); //reads voltage on Pin A0
float millivolts = (tempsense / 1024.0) * 5000;//convert pin A0 to millivolts
float celsius_value = (millivolts / 10.0) - 273.15; //sensor output is 10mV per degree Celsius
float fahrenheit_value = ((celsius_value)*(9.0/5.0)) + 32.0;//convert to fahrenheit
                           
Serial.print("Fahrenheit:");
Serial.println(fahrenheit_value);
Serial.println();
Serial.println();
delay (2000);
}

void light_on()
{
      digitalWrite(LED,HIGH);
    Serial.println("Light is ON");
    Serial.println();
    Serial.println();
    delay (2000);
}

void light_off()
{
      digitalWrite(LED,LOW);
    Serial.println("Light is Off");
    Serial.println();
    Serial.println();
    delay (2000);
}