Hello!
I have a project for school, of which i have to use a laser and a ultrasone. I have the 2 codes, but i need to combine them. Here they are:
Laser:
int LDR_Pin = A0; //analog pin 0
int Led_Pin = 13; //analog pin 0
void setup(){
- Serial.begin(9600);*
- pinMode(LDR_Pin, OUTPUT);*
- pinMode(Led_Pin, OUTPUT);*
}
void loop(){ - int LDRReading = analogRead(LDR_Pin);*
- Serial.println(LDRReading);*
- if (LDRReading>100) {digitalWrite(Led_Pin, LOW);};*
- if (LDRReading<100) {digitalWrite(Led_Pin, HIGH);};*
- delay(250); //just here to slow down the output for easier reading*
}
And the Ultrasone:
#define trigPin 10
#define echoPin 13
int led_rood = 11;
void setup() {
-
Serial.begin (9600);*
-
pinMode(trigPin, OUTPUT);*
-
pinMode(echoPin, INPUT);*
}
void loop() { -
float duration, distance;*
-
digitalWrite(trigPin, LOW);*
-
delayMicroseconds(2);*
-
digitalWrite(trigPin, HIGH);*
-
delayMicroseconds(10);*
-
digitalWrite(trigPin, LOW);*
-
duration = pulseIn(echoPin, HIGH);*
_ distance = (duration / 2) * 0.0344;_ -
if (distance >= 70 || distance <= 2){*
-
Serial.print("Distance = ");*
-
Serial.println("led uit");*
-
digitalWrite(led_rood, LOW);*
-
}*
-
else {*
-
Serial.print("Distance = ");*
-
Serial.print(distance);*
-
Serial.println(" cm");*
-
digitalWrite(led_rood, HIGH);*
-
delay(500);*
-
}*
-
delay(500);*
}
And here's the combined code:
#define trigPin 10
#define echoPin 13
int led_rood = 11;
int LDR_Pin = A0; //analog pin 0
int Led_Pin = 13; //analog pin 0
void setup() {
-
Serial.begin (9600);*
-
pinMode(trigPin, OUTPUT);*
-
pinMode(echoPin, INPUT);*
-
Serial.begin (9600);*
-
pinMode(LDR_Pin, OUTPUT);*
-
pinMode(Led_Pin, OUTPUT);*
}
void loop() { -
int LDRReading = analogRead(LDR_Pin);*
-
Serial.println(LDRReading);*
-
if (LDRReading>100) {digitalWrite(Led_Pin, LOW);};*
-
if (LDRReading<100) {digitalWrite(Led_Pin, HIGH);};*
-
delay(250); //just here to slow down the output for easier reading*
-
float duration, distance;*
-
digitalWrite(trigPin, LOW);*
-
delayMicroseconds(2);*
-
digitalWrite(trigPin, HIGH);*
-
delayMicroseconds(10);*
-
digitalWrite(trigPin, LOW);*
-
duration = pulseIn(echoPin, HIGH);*
_ distance = (duration / 2) * 0.0344;_ -
if (distance >= 70 || distance <= 2){*
-
Serial.print("Distance = ");*
-
Serial.println("led uit");*
-
digitalWrite(led_rood, LOW);*
-
}*
-
else {*
-
Serial.print("Distance = ");*
-
Serial.print(distance);*
-
Serial.println(" cm");*
-
digitalWrite(led_rood, HIGH);*
-
delay(500);*
-
}*
-
delay(500);*
}
On the serial monitor it reads the light sensor of the laser program right, but with my ultrasone it keeps saying LED = off, even tho i put my hand in front of the sensor.
Can someone tell me what's wrong? thanks!