Go Down

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


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);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  else {
    Serial.println(" cm");
  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);



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

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.

shortcuts: alt+s submit/post or alt+p preview