Combining code doesn't work

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! :slight_smile:

It's possibly got something to do with you using pin 13 for two things. You're expecting it to be the echo, meanwhile you're screwing around with it forcing pin 13 off or on as a result of the ldr reading.

int Led_Pin = 13; //analog pin 0Comments that belie the code are a waste of space.

kenwood120s: It's possibly got something to do with you using pin 13 for two things. You're expecting it to be the echo, meanwhile you're screwing around with it forcing pin 13 off or on as a result of the ldr reading.

Oh indeed, didnt notice that. How do i fix that?

Xypod13: Oh indeed, didnt notice that. How do i fix that?

Put the ultrasound echo wire in a different Arduino hole and change its reference in the code?

kenwood120s:
Put the ultrasound echo wire in a different Arduino hole and change its reference in the code?

i changed it to 12 but it didnt work, then to 8 and it worked :? Anyway, Thanks!

Xypod13:
i changed it to 12 but it didnt work, then to 8 and it worked :?

Hmm, that’s interesting. I wonder why?

Xypod13:
Anyway, Thanks!

You’re welcome.

tip: next time post your code in the code tags (the </> icon) else the code police will nail you :wink:

Then code will look like this