loading

Arduino Run at the same time?

Hi , 

i got problem with my code which is can't run at the same time.

for the Heat is great but the ultrasonic didn't trigger the buzzer which is more than 30cm away.

btw here is my code , which part should i change ? Thanks

#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

Probably the code is locking up because it isn't seeing an echo. Check the ultrasonic works first, and then find a code example that uses a pin-change interrupt to handle the wait, or put a timeout argument on pulseIn, say 100msec.

Melromeo (author)  steveastrouk3 years ago

there is an Echo which is #define echoPin 7 //echoPin ,

i've run separately which is Heat and Ultrasonic

for Ultrasonic code

#define trigPin 6 //trigPin

#define echoPin 7 //echoPin

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

}

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

}

delay(500);

}

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

if work like a charm but if i merge two sketches , it became horrible.

Try

duration = pulseIn (echoPin, HIGH,10000);

The code should time out after10msec.

Also

distance = (duration/2) / 29.1;

should be

distance = (duration/ 60);

DEFINE "horrible" ??

Melromeo (author)  steveastrouk3 years ago

Hi steveastrouk ,

i mean the ultrasonic is running if i upload the code separately but it does not running if i combine 2 sketches at the same time which is Heat and Ultrasonic at the same time.

What do you mean by it does not " run" ? Does the ultrasonic click ? Does the display not work ? Does the sounder not happen ?

Melromeo (author)  steveastrouk3 years ago

i mean , the sound is not happen.

So does the serial out not change when you print "distance"

Melromeo (author)  steveastrouk3 years ago

Hi , steveastrouk

void ultrasonic()

{

digitalWrite (trigPin, HIGH);

delayMicroseconds (1000);

digitalWrite (trigPin, LOW);

duration = pulseIn (echoPin, HIGH,10000);

distance = (duration/2) / 60;

if (distance > 30)

{

digitalWrite (13, HIGH);

}

else

{

digitalWrite (13, LOW);

}

}

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

i have removed serial print since i don't need it . by the way here the code that i've follow ur guide. but it still won't work if the fan is running.

Appreciate your help! :)

Does the ultrasonic code work on its own ?

I don't understand what your problem is. What are you trying to get running at the same time. Keep in mind nothing about an Arduino can be run "at the same time". Basically the arduino is running through each line of the sketch one at a time. It just does it vary fast.

What kind of buzzer are you using? Is it the kind that will bake noise just by supplying 5V to it. Or are you using a basic 2 lead piezo or speaker. If it's the later then your not going to get a sound just by setting the output to high.