Diorama 3.0 – Codice in C
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
Servo myservo;
LiquidCrystal_I2C lcd(0x27,20,4);
//Anemometro
int d=0;
int p=0;
int c=0;
long t=0;
float vel;
//Servo
int val;
//Motore
int speed1;
//Luci
int thr=60;
float Rand;
void setup() {
lcd.begin();
lcd.backlight();
Serial.begin(9600);
pinMode(INPUT,2);
t=millis();
myservo.attach(12);
//Pin Motore
pinMode(21,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(22,OUTPUT);
pinMode(23,OUTPUT);
pinMode(24,OUTPUT);
//Pin Luci
pinMode(30,OUTPUT);
pinMode(31,OUTPUT);
pinMode(32,OUTPUT);
randomSeed(analogRead(5));
Serial.begin(9600);
}
void loop() {
vento();
TurnMotorA();
servo();
strada();
casa();
pale();
}
void vento(){
d= digitalRead(2);
if (d==0 && p==1){
c++;
Serial.println(d);
}
p=d;
if (millis()-t>=1000){
Serial.println(c);
Vel();
t=millis();
c=0;
}
}
//Funzione Anomometro
void Vel(){
vel=c*2;
lcd.setCursor(0, 1);
lcd.print(“Velocita’: “);
lcd.setCursor(10,1);
lcd.print(vel);
Serial.println(vel);
lcd.setCursor(14, 1);
lcd.print(” km/h”);
}
//Funzione Servo
void servo(){
val = analogRead(0);
val = map(val, 0, 1023, 179, 0);
myservo.write(val);
delay(15);
lcd.setCursor(0,0);
lcd.print(“Orientamento:”);
if (val>-1 && val<46){ lcd.print(“Sud “);} else if (val>45 && val<91){ lcd.print(“Est “);} else if (val>90 && val<136){
lcd.print(“Nord”);}
else {
lcd.print (“West”);}
}
//Funzione Motore1
void TurnMotorA(){
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
if (c>7){
speed1=0;
digitalWrite(21,speed1);
digitalWrite(24,speed1);
digitalWrite(23,speed1);
digitalWrite(22,speed1);
else {
speed1=c;
digitalWrite(21,speed1);
digitalWrite(24,speed1);
digitalWrite(23,speed1);
digitalWrite(22,speed1);
}
}
//Funzione Luci Strade
void strada (){
int v= analogRead(5);
Serial.println(v);
if (v>thr){
digitalWrite(30, LOW);}
else {
digitalWrite(30, HIGH);}
delay(2);
}
//Funzione Luci Case
void casa(){
Rand=random(1000,5000);
int v=analogRead(5);
if (v<thr){
digitalWrite(31, HIGH);
delay(Rand);}
else {
digitalWrite(31, LOW);
delay(Rand);}
delay(2);
}
//Funzione Luci Pale
void pale (){
int v=analogRead(5);
if (v<thr){
digitalWrite(32,HIGH);
delay(500);}
}