Code Arduino
Le code Arduino permet de donner les informations au système sur comment fonctionner.
Code du Système complet
#include <Servo.h>
//assignation des pins capteur
const unsigned int TRIG_PIN=13;
const unsigned int ECHO_PIN=12;
const unsigned int BAUD_RATE=9600;
//variable
int VAR =180;
int INC=1;
int MAPVAR;
//composant
int FOG = 10;
int ENG = 8;
int LED = 6;
Servo myservo;
void setup() {
myservo.attach(9);
Serial.begin(9600);
myservo.attach(A0);
pinMode(FOG, OUTPUT);
pinMode(ENG, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(BAUD_RATE);
}
void loop() {
// servo et lumière
myservo.write(VAR);
analogWrite(LED, MAPVAR);
MAPVAR = map(VAR, 0, 180, 0, 255);
//capteur
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(100);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(100);
digitalWrite(TRIG_PIN, LOW);
const unsigned long duration= pulseIn(ECHO_PIN, HIGH);
int distance= duration/29/2;
if(duration==0){
Serial.println("Warning: no pulse from sensor");
}
else{
Serial.print("Distance de l'objet : ");
Serial.print(distance);
Serial.print("cm\n");
//Serial.println(distance);
//Serial.println(" cm");
Serial.print(" VAR=");
Serial.println(VAR);
delay(10);
}
//ON-OFF composant
if (distance >=30)
{
digitalWrite(FOG, 0);
digitalWrite(ENG, 0);
if(VAR>=180)
{
INC=-2;
}
if(VAR<=0){ INC= +2;
} VAR=VAR+INC;
}
if(distance <=30)
{
digitalWrite(FOG, 1);
digitalWrite(ENG, 1);
if(VAR>=180)
{
INC=-20;
}
if(VAR<=0)
{
INC= +15;
}
VAR=VAR+INC;
}
}