
EJERCICIOS ARDUINO TANDA 3
Ejercicio 1. Puerta automática


2. Sensor de distancia ultrasónico



3. Sensor ultrasónico con pantalla LCD


4. Greenhouse simple

#define ServoPin 3
int sensePin = A0; //This is the Arduino Pin that will read the sensor output
int sensorInput; //The variable we will use to store the sensor input
double temp; //The variable we will use to store temperature in degrees.
void servoChange(int); // function for setting servo position
void setup()
{
pinMode(ServoPin,OUTPUT);
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
Serial.begin(9600); // start the serial port at 9600 baud (default)
}
//INTERRUPT FUNCTION - repeats every 1ms
SIGNAL(TIMER0_COMPA_vect)
{
sensorInput = analogRead(A0); //read the analog sensor and store it
temp = (double) sensorInput / 1024; //find the percentage of input reading
temp = temp * 5; //multiply by 5V to get voltage
temp = temp - 0.5; //Subtract the offset
temp = temp * 100; //Convert to degrees
}
void loop()
{
int pw;
if(temp > 25)//fully open
{
pw = 2500;
Serial.println("OPEN");
}
else if(temp <= 20) //fully closed
{
pw = 0;
Serial.println("CLOSED");
}
else if(temp > 20 && temp <= 25)
{
pw = 1500;
Serial.println("HALF");
}
servoChange(pw);
}
void servoChange(int x)
{
digitalWrite(ServoPin, HIGH);
delayMicroseconds(x);
digitalWrite(ServoPin,LOW);
delay(15);
return ;
}
5. Alimentador de gatos (o de linces)

#include <Servo.h>
Servo myservo; // create servo object to control a servo
int buttonState = 0; //variable for reading the pushbutton status
const int buttonPin = 2; // the number of the pushbutton pin
int pos;
int num; //number of times the dog presses the bone
void setup() {
myservo.attach(9); // attaches the servo on pin 9
}
void loop() {
buttonState = digitalRead(buttonPin);//read the state of the pushbotton value;
if (buttonState ==HIGH){
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
delay(5000); //wait for bone to be released
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}


