Help - Making Ultrasonic Sensor, Servo Motors, DC Motor, LED Strip to work together

Hello everyone, i am new to this forum. I came here today to ask about a question regarding on how to make four electronics to work together.

I am currently trying to make a program where an Ultrasonic Sensor will receive input, and two Servo Motors will move, then the LED Strip will begin to fade from slow to fast and finally the motor will spin.

I have tried to make a little program where the ultrasonic sensor will output two servo motors to move, however i am having trouble adding the LED Strip to fade.

Here is my code.

Servo servo1;
Servo servo2;
int trigPin = 12;
int echoPin = 11;
int led = 3;
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
long distance;
long duration;

 
 void setup() 
{
servo1.attach(7);
servo2.attach(6);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);// put your setup code here, to run once:
 pinMode(led, OUTPUT);

}
 
void loop() { //start of loop

  ultra();
  servo1.write(0);
  servo2.write(90);
  if(distance <= 10){ //Ultrasonic Sensor receive input  
  servo1.write(90);
  servo2.write(0); //Two servo motors will move 
  delay(10000);//Servo motors hold position for 10 seconds and returns to 0 degree
  analogWrite(led, brightness); // fade LED strip code
        brightness = brightness + fadeAmount;
        (brightness <= 0 || brightness >= 255); 
        fadeAmount = -fadeAmount;
  
  }
}
 
void ultra(){
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2 );
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration*0.034/2;
  
  }

any help is appreciated.

You will need a conditional "if" before "(brightness..." replacing the semicolon ";" because the semicolon indicates "end of command" then a "then" before fadeAmount... to reverse the fade direction...

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