Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- int greenPin = 3;
- int redPin = 13;
- int yellowPin = 5;
- int buttonPin = 1;
- int servoPin = 10;
- int pinBuzzer = 9;
- int valServo;
- int tempReached = 20;
- //Flags
- boolean startCounting = false;
- boolean runServo = false;
- boolean showTemp = false;
- volatile byte state = LOW;
- long timeArduinoStarted;
- // Identificación de pines analógicos
- int sensorTempPin = A0;
- Servo myservo;
- void setup()
- {
- pinMode(greenPin,OUTPUT);
- pinMode(redPin,OUTPUT);
- pinMode(yellowPin,OUTPUT);
- pinMode(buttonPin,INPUT_PULLUP);
- delay(2000);
- //myservo.attach(10);
- digitalWrite(greenPin,LOW);
- digitalWrite(redPin,LOW);
- digitalWrite(yellowPin, LOW);
- Serial.begin(9600);
- attachInterrupt(digitalPinToInterrupt(buttonPin), _onInterruption, RISING);
- }
- void loop()
- {
- digitalWrite(redPin, state);
- if (digitalRead(redPin)) {
- Serial.println("Red pin on");
- delay(2000);
- _turnYellowPinOn();
- startCounting = !startCounting;
- timeArduinoStarted = millis();
- _startCounting();
- }
- while (runServo == true) {
- Serial.println("Running servo");
- _runServo();
- }
- _displayTemp();
- }
- //PRIVATE FUNCTIONS
- void _turnYellowPinOn() {
- Serial.println("Turning yellow pin on");
- digitalWrite(yellowPin, HIGH);
- }
- void _onInterruption() {
- Serial.println("On interruption");
- state = !state;
- }
- void _startCounting() {
- Serial.println("Counting..");
- while (startCounting) {
- //As soon as it's counting, run servo
- runServo = !runServo;
- if (millis() - timeArduinoStarted >= 10000) {
- timeArduinoStarted = millis();
- Serial.println("Show temperature");
- showTemp = !showTemp;
- //Stop counting
- startCounting = !startCounting;
- //Stop running servo
- runServo = !runServo;
- //Turning leds off
- state = !state;
- digitalWrite(yellowPin, LOW);
- //Turning green pin on
- digitalWrite(greenPin,HIGH);
- }
- }
- }
- void _displayTemp() {
- int tempSensor=0;
- float tempVoltage=0;
- float tempDegrees;
- tempSensor = analogRead(sensorTempPin);
- tempVoltage = tempSensor * (3300/1024);
- tempDegrees = (tempVoltage - 500) / 10;
- if (showTemp) {
- Serial.println("tempDegrees");
- Serial.println(tempDegrees);
- showTemp = !showTemp;
- }
- if(tempDegrees < tempReached) {
- _runBuzzer();
- }
- }
- void _runServo() {
- myservo.attach(10);
- for (valServo = 0; valServo <= 180; valServo += 1) {
- myservo.write(valServo);
- delay(15);
- }
- for (valServo = 180; valServo >= 0; valServo -= 1) {
- myservo.write(valServo);
- delay(15);
- }
- myservo.detach();
- runServo = !runServo;
- }
- void _runBuzzer() {
- tone(pinBuzzer, 1000);
- delay(1000);
- noTone(pinBuzzer);
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement