Smart Traffic 🚦 Light

 


#include <Servo.h>




Servo servoMotor; // Create a servo object


int servoPin1 = 9; // Define the pin for the servo motor




int ledPin1 = 2; // Define the pins for the LEDs


int ledPin2 = 3;


int ledPin3 = 4;




void setup() {


  pinMode(ledPin1, OUTPUT); // Set LED pins as outputs


  pinMode(ledPin2, OUTPUT);


  pinMode(ledPin3, OUTPUT);




  servoMotor.attach(servoPin1); // Attach the servo to its pin


}




void loop() {


 digitalWrite(ledPin1, HIGH);

 delay(1000);

  // Rotate servo from 90° to 0°


  for (int angle = 90; angle >= 0; angle -= 1) {


    servoMotor.write(angle);


    delay(30); // Adjust speed of servo rotation if needed


  }


  delay(5000);

  digitalWrite(ledPin1, LOW);

  digitalWrite(ledPin2, HIGH);

  delay(5000);

  digitalWrite(ledPin2, LOW);

  digitalWrite(ledPin3, HIGH);

  

  // Rotate servo from 0° to 90°


  for (int angle = 0; angle <= 90; angle += 1) {


    servoMotor.write(angle);


    delay(30); // Adjust speed


  }

delay(5000); 

digitalWrite(ledPin3, LOW);

}


Post a Comment

Previous Post Next Post

Contact Form