Skip to content

Instantly share code, notes, and snippets.

@JesusTinoco
Last active December 3, 2020 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JesusTinoco/d7c998fbac618793389a3a16926a3d6b to your computer and use it in GitHub Desktop.
Save JesusTinoco/d7c998fbac618793389a3a16926a3d6b to your computer and use it in GitHub Desktop.
Practica 4 - Arduino
#include <Servo.h>
#include <Timer5.h>
// Identificación de pines digitales
int pinServo = 9;
int pinTone = 8;
int pinBoton = 4;
int interruptButton = 1;
int pinRojo = 2;
int pinVerde = 3;
int pinAmarillo = 5;
// Identificación de pines analógicos
int pinTempSensor = A0;
// Umbrales temperatura
int umbralRojo = 17;
int tempSensor=0;
float tempVoltage=0;
float tempDegrees;
// Servo object, para controlar el Servo
Servo myservo;
int count = 0;
int servoPos = 0;
void setup()
{
pinMode(pinBoton,INPUT);
pinMode(interruptButton, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptButton), blink, CHANGE);
pinMode(pinRojo, OUTPUT);
pinMode(pinVerde, OUTPUT);
pinMode(pinAmarillo, OUTPUT);
pinMode(pinTone, OUTPUT);
myservo.attach(pinServo);
myservo.write(servoPos);
digitalWrite(pinRojo, HIGH);
digitalWrite(pinVerde, HIGH);
digitalWrite(pinAmarillo, HIGH);
delay(2000);
digitalWrite(pinRojo, LOW);
digitalWrite(pinAmarillo, LOW);
Serial.begin(9600); // Inicialización comunicación serie PC-Arduino
}
void timer() {
count++; // count number of toggles
servoPos += 20;
Serial.println(count);
if (count > 9) {
count = 0;
servoPos = 0;
measureTemperature();
printTemperature();
MyTimer5.end();
}
}
void loop()
{
digitalWrite(pinRojo, LOW);
if( digitalRead(pinBoton) ) {
digitalWrite(pinRojo,HIGH);
}
myservo.write(servoPos);
if(tempDegrees < umbralRojo)
{
noTone(pinTone);
}
else
{
tone(pinTone, 200);
}
delay(1000);
}
void measureTemperature() {
tempSensor = analogRead(pinTempSensor);
tempVoltage = tempSensor * (3300/1024); //in miliVolt
tempDegrees = (tempVoltage - 500) / 10;
}
void printTemperature() {
Serial.print(" Tiempo (sg) ");
Serial.print(millis()/1000);
Serial.print(" tempSensor ");
Serial.print(tempSensor);
Serial.print(" tempVoltage ");
Serial.print(tempVoltage);
Serial.print(" tempDegrees ");
Serial.println(tempDegrees);
}
void blink() {
digitalWrite(pinAmarillo, HIGH);
digitalWrite(pinVerde, LOW);
MyTimer5.begin(1); // 200=for toggle every 5msec
MyTimer5.attachInterrupt(timer);
MyTimer5.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment