-1

Name of study: AUTOMATIC ELECTRIC FAN

We used an ultrasonic sensor for the switching on/off of our Fan and an LM35 SENSOR to control the speed regulation. During our experiment, we use a LED instead of a Fan before we proceed to fan. Here is our code:

Description: Codes for LM35 Sensor using LED and Arduino Uno as Microcontroller)

#define led 9
float temp;

void setup ()
{
   pinMode(led, OUTPUT);
   Serial.begin(9600);
} 

 void loop ()
{
    temp = analogRead (A0);
    temp = temp*0.48828125;
     if (temp < 31 )
  {
    digitalWrite(led, HIGH);
  } 
  else {

    digitalWrite(led, LOW);
  }
    Serial.print("TEMPERATURE: ");
    Serial.print(temp);
    Serial.print("*C");
     Serial.println();
     delay(1000);
 }   

Description: Code for ULTRASONIC SENSOR using two LED’s AND Arduinbo Uno as Microcontroller

#define trigPin 13
#define echoPin 12
#define led 6
#define led 5
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds (2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds (10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration*0.034/2;
  if (distance < 100 )
  {
    digitalWrite(led, HIGH);
  } 
  else {

    digitalWrite(led, LOW);
  }
  Serial.print("Distance:");
  Serial.println(distance);
  Serial.print("cm");
  delay(1000);
}
  • You need to edit your question, select the code, and tap the code format button. (It looks like open and close braces: {}. That will format the code so that it's readable. Right now it is decidedly not readable. – Duncan C Mar 8 at 15:45
  • Thank You. You know how to combined the codes for the two sensors? – Bless G. Ampuan Mar 8 at 15:49
  • 2
    As I always say: Step 1: understand your existing code and how it works. Step 2: write a new program which incorporates what you learned in step 1. – Majenko Mar 8 at 15:56
  • @MichelKeijzers, I resisted the urge to edit the OPs code. I was trying to use their first post as a "teaching moment" for how questions should be formatted. – Duncan C Mar 8 at 18:25
  • @DuncanC Sorry, I justed improved the layout without reading all comments. – Michel Keijzers Mar 8 at 20:57
2

You don't have two codes; the 2nd section has no setup() or loop().

If it did, then you could do this:

  • The stuff before setup() gets put together.
  • The stuff in setup() from one gets moved into the other. The empty setup() gets deleted.
  • The stuff in loop() from one gets moved into the other. The empty loop() gets deleted.

Take care of any duplicate names, pin assignments, etc.

Review the combined loop() code and adjust as needed to do what you want.

  • Thanks for the cleanup Michel, easier to read now. – CrossRoads Mar 8 at 18:18
  • You're welcome! – Michel Keijzers Mar 8 at 20:56

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.