Problem with Ultrasonic and LM35 sensor code.

Hi , i got problem with these loops because i'm not that good into this code. I wanna make it show the data instead keep blinking for Heat sensor. i'm using Arduino UNO.

Is there anyone would guide me which code should i change ?

btw here the code :

#include <LiquidCrystal.h>
#define trigPin 6    //trigPin
#define echoPin 7    //echoPin

LiquidCrystal lcd(12,11,5,4,3,2);
int tempPin = A1;   // the output pin of LM35
int fan = 10;       // the pin where fan is
int led = 9;        // led pin
int temp;
int tempMin = 35;   // the temperature to start the fan
int tempMax = 70;   // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
 
void setup() {
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  lcd.begin(16,2);  
  
  
  Serial.begin (9600);        //Baud rate
  pinMode (trigPin, OUTPUT);  //trig pin as output
  pinMode (echoPin, INPUT);   //echo pin as input
  pinMode (13, OUTPUT);       //Buzzer pin as output
}
 
void loop() {  
  
   int duration, distance;
  digitalWrite (trigPin, HIGH);
  delayMicroseconds (1000);
  digitalWrite (trigPin, LOW);
  duration = pulseIn (echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance > 30)
  {
    Serial.print(distance);
    Serial.println(" cm");
    digitalWrite (13, HIGH);
  }
  else
  {
    Serial.println (distance);
    Serial.println ( "cm");
    digitalWrite (13, LOW);
  }
 

void loop();
   temp = readTemp();     // get the temperature
   if(temp < tempMin) {   // if temp is lower than minimum temp
       fanSpeed = 0;      // fan is not spinning
       digitalWrite(fan, LOW);       
   } 
   if((temp >= tempMin) && (temp <= tempMax)) {  // if temperature is higher than minimum temp
       fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
       fanLCD = map(temp, tempMin, tempMax, 0, 100);  // speed of fan to display on LCD
       analogWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
   } 
   
   if(temp > tempMax) {        // if temp is higher than tempMax
     digitalWrite(led, HIGH);  // turn on led 
   } else {                    // else turn of led
     digitalWrite(led, LOW); 
   }
   
   lcd.print("TEMP: ");
   lcd.print(temp);      // display the temperature
   lcd.print("C ");
   lcd.setCursor(0,1);   // move cursor to next line
   lcd.print("FANS: ");
   lcd.print(fanLCD);    // display the fan speed
   lcd.print("%");
   delay(200);
   lcd.clear();   
}
 
int readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
}

Thank you so much :wink:

You have too loop functions, you can not do this. Please read
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

Hi Grumpy_Mike ,

Thank you for your reply . I will try to fix it :slight_smile:

Hi Grumpy_Mike ,

i've tried to merge but it seem my code is not running. but there is no errors on my coding.

#include <LiquidCrystal.h>
#define trigPin 6    //trigPin
#define echoPin 7    //echoPin

LiquidCrystal lcd(12,11,5,4,3,2);
int tempPin = A1;   // the output pin of LM35
int fan = 10;       // the pin where fan is
int led = 9;        // led pin
int temp;
int tempMin = 35;   // the temperature to start the fan
int tempMax = 70;   // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
int duration, distance;
 
void setup() {
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  lcd.begin(16,2);  
  
  digitalWrite (trigPin, HIGH);
  delayMicroseconds (1000);
  digitalWrite (trigPin, LOW);
  duration = pulseIn (echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  temp = readTemp();     // get the temperature
  Serial.begin (9600);        //Baud rate
  pinMode (trigPin, OUTPUT);  //trig pin as output
  pinMode (echoPin, INPUT);   //echo pin as input
  pinMode (13, OUTPUT);       //Buzzer pin as output
}

void ultrasonic()
{

  if (distance > 30)
  {
    Serial.print(distance);
    Serial.println(" cm");
    digitalWrite (13, HIGH);
  }
  else
  {
    Serial.println (distance);
    Serial.println ( "cm");
    digitalWrite (13, LOW);
  }
}

void heat()
{ 
 
   if(temp < tempMin) {   // if temp is lower than minimum temp
       fanSpeed = 0;      // fan is not spinning
       digitalWrite(fan, LOW);       
   } 
   if((temp >= tempMin) && (temp <= tempMax)) {  // if temperature is higher than minimum temp
       fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
       fanLCD = map(temp, tempMin, tempMax, 0, 100);  // speed of fan to display on LCD
       analogWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
   } 
   
   if(temp > tempMax) {        // if temp is higher than tempMax
     digitalWrite(led, HIGH);  // turn on led 
   } else {                    // else turn of led
     digitalWrite(led, LOW); 
   }
   
   lcd.print("TEMP: ");
   lcd.print(temp);      // display the temperature
   lcd.print("C ");
   lcd.setCursor(0,1);   // move cursor to next line
   lcd.print("FANS: ");
   lcd.print(fanLCD);    // display the fan speed
   lcd.print("%");
   delay(200);
   lcd.clear();   
}
 
int readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
} 


void loop() {  
  
ultrasonic();
heat();
}

Thanks
Melz

What does happen when you run it ?
Nothing at all on the serial monitor ?
I see that you are only measuring the distance once in the setup() function. Is that what you meant to do ?
The same goes for measuring the temperature. You only do it once.

Incidentally this function

void ultrasonic()
{

  if (distance > 30)
  {
    Serial.print(distance);
    Serial.println(" cm");
    digitalWrite (13, HIGH);
  }
  else
  {
    Serial.println (distance);
    Serial.println ( "cm");
    digitalWrite (13, LOW);
  }
}

could be condensed to

void ultrasonic()
{
  byte state = LOW;
  if (distance > 30)
  {
    state = HIGH;
  }
  Serial.print(distance);
  Serial.println(" cm");
  digitalWrite (13, state);
}

hi UKHeliBob ,

i just wanna run 2 sensor at the same time when USB plug in.

i've make function for Ultrasonic and Heat .. here is my code

#include <LiquidCrystal.h>
#define trigPin 6    //trigPin
#define echoPin 7    //echoPin

LiquidCrystal lcd(12,11,5,4,3,2);
int tempPin = A1;   // the output pin of LM35
int fan = 10;       // the pin where fan is
int led = 9;        // led pin
int temp;
int tempMin = 35;   // the temperature to start the fan
int tempMax = 70;   // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
int duration, distance;
 
void setup() {
heat ();

  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  lcd.begin(16,2);  
  
   
 
ultrasonic();

  Serial.begin (9600);        //Baud rate
  pinMode (trigPin, OUTPUT);  //trig pin as output
  pinMode (echoPin, INPUT);   //echo pin as input
  pinMode (13, OUTPUT);       //Buzzer pin as output
}

void ultrasonic()
{
digitalWrite (trigPin, HIGH);
  delayMicroseconds (1000);
  digitalWrite (trigPin, LOW);
  duration = pulseIn (echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance > 30)
  {
    Serial.print(distance);
    Serial.println(" cm");
    digitalWrite (13, HIGH);
  }
  else
  {
    Serial.println (distance);
    Serial.println ( "cm");
    digitalWrite (13, LOW);
  }
}

void heat()
{ 
 temp = readTemp();     // get the temperature
   if(temp < tempMin) {   // if temp is lower than minimum temp
       fanSpeed = 0;      // fan is not spinning
       digitalWrite(fan, LOW);       
   } 
   if((temp >= tempMin) && (temp <= tempMax)) {  // if temperature is higher than minimum temp
       fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
       fanLCD = map(temp, tempMin, tempMax, 0, 100);  // speed of fan to display on LCD
       analogWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
   } 
   
   if(temp > tempMax) {        // if temp is higher than tempMax
     digitalWrite(led, HIGH);  // turn on led 
   } else {                    // else turn of led
     digitalWrite(led, LOW); 
   }
   
   lcd.print("TEMP: ");
   lcd.print(temp);      // display the temperature
   lcd.print("C ");
   lcd.setCursor(0,1);   // move cursor to next line
   lcd.print("FANS: ");
   lcd.print(fanLCD);    // display the fan speed
   lcd.print("%");
   delay(200);
   lcd.clear();   
}
 
int readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
} 


void loop() {  
  
ultrasonic();
heat();
}

but sometimes the lcd blinking and not running . what should i do ?

thanks
Melz

Make the first thing in setup the serial begin and remove the calls to the two main functions in the setup.

Hi Grumpy_Mike

did you mean

heat ();  and ultrasonic();

and it will be

void setup() {

  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  lcd.begin(16,2);  
  
  Serial.begin (9600);        //Baud rate
  pinMode (trigPin, OUTPUT);  //trig pin as output
  pinMode (echoPin, INPUT);   //echo pin as input
  pinMode (13, OUTPUT);       //Buzzer pin as output
}

Thanks
Melz

Make the first thing in setup the serial begin

Did you?

void setup() {

  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  lcd.begin(16,2);  
  
  Serial.begin (9600);        //Baud rate
  pinMode (trigPin, OUTPUT);  //trig pin as output
  pinMode (echoPin, INPUT);   //echo pin as input
  pinMode (13, OUTPUT);       //Buzzer pin as output
}

No. Why not?

Hi PaulS ,

i did that already , now the ultrasonic run as i want . but the Heat is stuck and not read any analog input.
What should i do ? Sorry i'm still newbie .

Thanks
Melz

Sorry i'm still newbie .

Even a newbie should be able to see that we are not in the room with you. Therefore, we can't see what your code looks like now.

Use Tools + Auto Format before posting ALL of your current code.

Your code
makes me dizzy
trying

to read
it.

Hi PaulS ,

Sorry , i have Auto format it and here is my current code.

#include <LiquidCrystal.h>
#define trigPin 6    //trigPin
#define echoPin 7    //echoPin

LiquidCrystal lcd(12,11,5,4,3,2);
int tempPin = A1;   // the output pin of LM35
int fan = 10;       // the pin where fan is
int led = 9;        // led pin
int temp;
int tempMin = ;   // the temperature to start the fan
int tempMax = 70;   // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
int duration, distance;

void setup() {
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  lcd.begin(16,2);  

  Serial.begin (9600);        //Baud rate
  pinMode (trigPin, OUTPUT);  //trig pin as output
  pinMode (echoPin, INPUT);   //echo pin as input
  pinMode (13, OUTPUT);       //Buzzer pin as output
} 

void ultrasonic()
{
  digitalWrite (trigPin, HIGH);
  delayMicroseconds (1000);
  digitalWrite (trigPin, LOW);
  duration = pulseIn (echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance > 30)
  {
    Serial.print(distance);
    Serial.println(" cm");
    digitalWrite (13, HIGH);
  }
  else
  {
    Serial.println (distance);
    Serial.println ( "cm");
    digitalWrite (13, LOW);
  }
}

void heat()
{ 
  temp = readTemp();     // get the temperature
  if(temp < tempMin) {   // if temp is lower than minimum temp
    fanSpeed = 0;      // fan is not spinning
    digitalWrite(fan, LOW);       
  } 
  if((temp >= tempMin) && (temp <= tempMax)) {  // if temperature is higher than minimum temp
    fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
    fanLCD = map(temp, tempMin, tempMax, 0, 100);  // speed of fan to display on LCD
    analogWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
  } 

  if(temp > tempMax) {        // if temp is higher than tempMax
    digitalWrite(led, HIGH);  // turn on led 
  } 
  else {                    // else turn of led
    digitalWrite(led, LOW); 
  }

  lcd.print("TEMP: ");
  lcd.print(temp);      // display the temperature
  lcd.print("C ");
  lcd.setCursor(0,1);   // move cursor to next line
  lcd.print("FANS: ");
  lcd.print(fanLCD);    // display the fan speed
  lcd.print("%");
  delay(200);
  lcd.clear();   
}

int readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
} 


void loop() {  

  ultrasonic();
  heat();
}

here is my current code.

So, you lied when you said you moved Serial.begin() to the start of setup(). I need to understand why before I read any more code..

int readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
}

So you have a value that you multiply by 0.48828125 and then return it as an int!!!

Look up int and float and understand the difference.