Need help! controlling rgb led with a srf05 sensor

Hello everyone!

I am doing a programming project for school and we are using arduino. The idea is to build a circuit in which a srf05 sensor detects movement at a certain distance and makes the 12v rgb led strip light up. The thing is, I don't know how to program it to do that and would appreciate help :slight_smile:

currently they both work at the same time but the sensor doesn't control the led

here's the code:

const int trigPin = 8;
const int echoPin = 7;
// defines variables
long duration;
int distance;

#define red 9
#define green 10
#define blue 11
int brightness;
int fadeAmount = 5;

void setup()
{
  //SENSOR EMITTER AND RECEIVER
  pinMode(8, OUTPUT); // Sets the trigPin as an Output
  pinMode(7, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication
}
void loop() {
  // Clears the trigPin
  {
    digitalWrite(8, LOW);
  }
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(8, HIGH);
  delayMicroseconds(10);
  digitalWrite(8, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(7, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);

  //LED'S
  analogWrite(green, brightness);
  analogWrite(blue, brightness);

  brightness += fadeAmount;
  if (brightness <= 0 || brightness >= 255 )
  {
    fadeAmount = -fadeAmount;
  }
  delay(20);
}

Do yourself (and us) a favour - give your pins names (you may be aware of which pins stuff is connected to, but we don't), and use them.

Use the IDE's auto-format tool.

what about now?

What do you mean?

are the formatting and pins better? i'm sorry but i really dont know anything about eletronics :frowning:

Oh.
You edited the earlier post.
Now I don't have a reference.

I guess that's better.

yes i changed it

...and now we both look stupid.
Enjoy the rest of your existence.

wow thanks for the help :neutral_face:

You're very welcome

Nice declaration. Why don't you use it then?

    digitalWrite(8, LOW);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.