Close

Electrical circuit

A project log for Flower Gang

A mechanical flower

romanelemieuxromane.lemieux 05/02/2023 at 07:110 Comments
Electrical circuit

CODE :

//Define LED RGB
int rougePin = 9;
int vertPin = 10;
int bleuPin = 11;

//Define light sensor
int capteurLumiere = A0;

//variable initialisationto stock the value reading by the light sensor
int valeurLumiere = 0;

#include "Servo.h"

Servo servo; // creation of the object "servo"

void setup() {

  pinMode(rougePin, OUTPUT);
  pinMode(vertPin, OUTPUT);
  pinMode(bleuPin, OUTPUT);


  Serial.begin(9600);
      servo.attach(12); // attache le servo au pin spécifié
  }

void loop() {
  //to read the value of the light sensor
  valeurLumiere = analogRead(capteurLumiere);

  //to show the value lof the light in the "moniteur série"
  Serial.print("Valeur de lumière : ");
  Serial.println(valeurLumiere);

  //if the value of the light is lower than 500, then turn on the LED RGB
  if (valeurLumiere < 500) {
   
    digitalWrite(rougePin, HIGH);
    delay(500);
    digitalWrite(rougePin, LOW);
    digitalWrite(vertPin, HIGH);
    delay(500);
    digitalWrite(vertPin, LOW);
    digitalWrite(bleuPin, HIGH);
    delay(500);
    digitalWrite(bleuPin, LOW);
    
    servo.write(90); // demande au servo de se déplacer à cette position

  }
  //if the value of the light is greater than or equal to 500, then turn off the LED RGB
  else {
    digitalWrite(rougePin, LOW);
    digitalWrite(vertPin, LOW);
    digitalWrite(bleuPin, LOW);
    
    servo.write(0);

  }
   
}

Discussions