I am doing a project using sensors (LDR & Ultrasonic) and LEDs using the Arduino Software. I have managed to get the light bulb to work, however I would like to know on how to combine two different Arduino programs into one. Attached below are the two different programs
Program 1:
int ldr=A0;//Set A0(Analog Input) for LDR.
int value=0;
void setup() {
Serial.begin(9600);
pinMode(3,OUTPUT);
}
void loop() {
value=analogRead(ldr);//Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if(value<300)
{
digitalWrite(3,HIGH);//Makes the LED glow in Dark.
}
else
{
digitalWrite(3,LOW);//Turns the LED OFF in Light.
}
}
Program 2:
#define trigPin 13
#define echoPin 12
#define led 11
void setup()
{ Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
}
void loop()
{ long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 10)
{ digitalWrite(led,HIGH);
}
else {
digitalWrite(led,LOW);
}
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Thank you!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…