loading

Arduino | Run 2 Function at the same time.

Hi , i wanna ask about 2 function which is Ultrasonic and Heat that run at the same time  , For me , i just got Heat running as i want but the Ultrasonic doesn't trigger the buzzer if object (Laptop) 30cm away from the ultrasonic. I've been trying since last week but the result is still the same ;'(

Btw here is the code , i've seperate it to 2 function which is Ultrasonic and Heat.  i hope u guys can guide me which part should i change or Add.

Thanks in advance ;)



//***********************************

#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;
int buzzer = LOW;

void setup() {
  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
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  lcd.begin(16,2);
  }

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();  
}

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


void loop() { 

  ultrasonic();
  heat();
}


//***********************************




sort by: active | newest | oldest
1-10 of 13Next »
brahim begin 10 months ago

hello i want run 4 servos in the same time but i cannot do it !!!

help me please

And we know your exact problem and code how?

You only need to post you question in one area.

The arduino cannot run functions at the same time. It can only run through each line of code one at a time. It just does it vary rapidly.

What kind of buzzer are you using? Seams like the problem may be your providing power to a piezo or speaker expecting a sound but getting nothing since the piezo/speaker doesn't have the ability to buzz without additional hardware. That or you may need to adjust the ultrasonic portion a bit. It may be best to break the project down to just the ultrasonic sensor and get that side of it working first.

Melromeo (author)  mpilchfamily3 years ago

hi , thank you for your fast reply.

i'm just using 5v Buzzer (standard) .Maybe it because of the loop function . because if i run separately for Ultrasonic and Heat . It work like a charm but if i combine those two sketches in 1 arduino . it doesn't work like i have planned.


Thanks

Melz

Then the problem is you need a delay after setting the buzzer high. Otherwise the arduino just flies through the buzzer section of the code and then goes through the rest of the code. Since the sonic sensors output changes so rapidly it may detect greater than 30cm an the first couple of passes but detect something closer on another pass. All of this taking place within 1 second.

Melromeo (author)  mpilchfamily3 years ago

do i need to use delay or millis ? i'm quite blurry about this function but i will try and let you know. thanks Mpilchfamily ;)

Millis track the number of milliseconds since the program started to run. A delay makes the arduino wait before moving on to the next line of code. So a delay after setting the buzzer pin high will allow that buzzer to sound for that amount of time. then the program can continue on. The way you have it set now the buzzer will have gaps in it making noise as it run through the code.

Try this.

#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;
int buzzer = LOW;

void setup() {
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
pinMode(fan, OUTPUT);
pinMode(led, OUTPUT);
pinMode(tempPin, INPUT);
lcd.begin(16,2);
}

void loop()
{
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);

heat();
}
else
{
Serial.println (distance);
Serial.println ( "cm");
digitalWrite (13, LOW);

heat();
}
}

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();
}

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


This makes the ultrasonic section the primary section then checks the heat section for updates while the buzzer is either sounding or not. Anything you want to be constantly going should be the void loop.

Melromeo (author)  mpilchfamily3 years ago

Hi , Mpilchfamily

i have uploaded the code , it still don't work. perhaps it same like before.

i notice that , if the fan is start running . the ultrasonic doesn't run.

by the way thanks for the code and i appreciate that.

Melromeo (author)  Melromeo3 years ago

By the here is the hardware . i'm making a smart cooler with anti - theft system . i'm using ultrasonic which is if someone taking away the laptop from the cooler pad and it will trigger the buzzer.

20140407_190324.jpg
nomuse Melromeo3 years ago

It is hard to see, but it looks like those are USB-powered fans but you are running them from the Arduino's power supply. It can't handle that much.

Also hard to tell if that is a piezo speaker, or a piezo speaker-based buzzer. The Rat Shack has both, but the former requires you send a tone to it, not just a voltage.

In any case, since the alarm function assumes the laptop has been removed, then go ahead and break from the rest of the circuit to sound the alarm; put in a hefty delay so the buzzer goes for several seconds. There's no need to monitor the ultrasonic or the temperature or run the fans, even, if the laptop is walking away.

1-10 of 13Next »