Skip to content

Instantly share code, notes, and snippets.

@JesusTinoco
Created December 3, 2020 18:41
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/512ce52e36310966be27b76fb039c8ae to your computer and use it in GitHub Desktop.
Save JesusTinoco/512ce52e36310966be27b76fb039c8ae to your computer and use it in GitHub Desktop.
Practica 4 - Arduino (Led Interrupt)
#include <Servo.h>
#include <Timer5.h>
// Identificación de pines digitales
int pinServo = 9;
int pinTone = 8;
int pinBoton = 4;
int pinRojo = 1;
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;
volatile int count = 0;
int servoPos = 0;
void setup()
{
pinMode(pinBoton,INPUT);
pinMode(pinRojo,INPUT);
attachInterrupt(digitalPinToInterrupt(pinRojo), blink, RISING);
MyTimer5.attachInterrupt(timer);
pinMode(pinVerde, OUTPUT);
pinMode(pinAmarillo, OUTPUT);
pinMode(pinTone, OUTPUT);
myservo.attach(pinServo);
myservo.write(servoPos);
digitalWrite(pinVerde, HIGH);
Serial.begin(9600); // Inicialización comunicación serie PC-Arduino
}
void timer() {
count++; // count number of toggles
servoPos += 20;
if(count > 9) {
Serial.println("END!");
measureTemperature();
printTemperature();
}
Serial.println(count);
}
void loop()
{
if(count > 9) {
MyTimer5.end();
}
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() {
Serial.println("BLINK");
count = 0;
digitalWrite(pinAmarillo, HIGH);
digitalWrite(pinVerde, LOW);
MyTimer5.begin(1); // 200=for toggle every 5msec
MyTimer5.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment