#include <Servo.h>
Servo braco;
int const abaixar = 140;
int const levantar = 5;
int emissor = 9;
int receptor = 10;
long duration, cm;
void setup() {
// put your setup code here, to run once:
pinMode (12, OUTPUT);
braco.attach (7);
pinMode (emissor, OUTPUT);
pinMode (receptor, INPUT);
braco.write (levantar);
Serial.begin (9600);
}
void loop() {
sonar();
delay (100);
Serial.println (cm);
if (cm==12){
// baixar
for (int x=levantar; x<abaixar; x++){
braco.write (x);
delay (6);
Serial.println (braco.read());
}
//Liga o ventilador
digitalWrite (12, HIGH);
delay (2500);
// levantar
for (int x=abaixar; x>levantar; x--){
braco.write (x);
delay (6);
Serial.println (braco.read());
}
//desliga o ventilador
digitalWrite (12, LOW);
delay (2500);
/*
//abaixar
for (int x=levantar; x<abaixar; x++){
braco.write (x);
delay (6);
}
delay (7000);
*/ }
}
int sonar(){
digitalWrite(emissor, LOW);
delayMicroseconds(2);
digitalWrite(emissor, HIGH);
delayMicroseconds(5);
digitalWrite(emissor, LOW);
duration = pulseIn(receptor, HIGH);
cm = microsecondsToCentimeters(duration);
return cm;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
Nenhum comentário:
Postar um comentário