Go Down

Topic: need help with HC-sr04 sensor (Read 483 times) previous topic - next topic

ernjaeg5416

okay so im trying to write an if statement to turn on an led light to turn on when something passes in front of the sensor and stay on until (or turn off)when something passes through the sensor again.

Ex i walk into the room Led turns on and stays on until i exit the room.

new to arduino all together would appreciate any input here is the code i have with no needed libraries
.




#define trigPin 12
#define echoPin 13
int pinLed = 11;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinLed = 11;
  pinMode(pinLed, OUTPUT);
}

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(500);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
   
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
 
  if (distance <= 199 )
  {
    digitalWrite(pinLed, HIGH);
   
  }
 
  if (distance <= 199 & & pinLed == HIGH) // this statement is my problem says i cant compare between pointer and integer///
  { digitalWrite(pinLed, LOW);
  }
 



}

michinyon

Well you have an error down the end,   which you already indentified.

while ( ....    pinLed == HIGH ) makes no sense.   

Easiest solution is to have some logical variable in your program so you can remember
whether you have the LED turned on,  or not.

Go Up