Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. int greenPin = 3;
  4. int redPin = 13;
  5. int yellowPin = 5;
  6.  
  7. int buttonPin = 1;
  8.  
  9. int servoPin = 10;
  10. int pinBuzzer   = 9;
  11.  
  12. int valServo;
  13.  
  14. int tempReached   = 20;
  15.  
  16. //Flags
  17. boolean startCounting = false;
  18. boolean runServo = false;
  19. boolean showTemp = false;
  20.  
  21. volatile byte state = LOW;
  22.  
  23. long timeArduinoStarted;
  24.  
  25. // Identificación de pines analógicos
  26. int sensorTempPin  = A0;
  27.  
  28. Servo myservo;
  29.  
  30. void setup()
  31. {
  32.  
  33.   pinMode(greenPin,OUTPUT);
  34.   pinMode(redPin,OUTPUT);
  35.   pinMode(yellowPin,OUTPUT);
  36.   pinMode(buttonPin,INPUT_PULLUP);
  37.  
  38.   delay(2000);
  39.  
  40.   //myservo.attach(10);
  41.  
  42.   digitalWrite(greenPin,LOW);
  43.   digitalWrite(redPin,LOW);
  44.   digitalWrite(yellowPin, LOW);
  45.  
  46.   Serial.begin(9600);
  47.  
  48.   attachInterrupt(digitalPinToInterrupt(buttonPin), _onInterruption, RISING);
  49. }
  50.  
  51.  
  52. void loop()
  53. {
  54.   digitalWrite(redPin, state);
  55.  
  56.   if (digitalRead(redPin)) {
  57.     Serial.println("Red pin on");
  58.    
  59.     delay(2000);
  60.  
  61.     _turnYellowPinOn();
  62.  
  63.     startCounting = !startCounting;
  64.    
  65.     timeArduinoStarted = millis();
  66.    
  67.     _startCounting();
  68.   }
  69.  
  70.   while (runServo == true) {
  71.     Serial.println("Running servo");
  72.    _runServo();
  73.   }
  74.  
  75.   _displayTemp();
  76. }
  77.  
  78.  
  79. //PRIVATE FUNCTIONS
  80.  
  81. void _turnYellowPinOn() {
  82.   Serial.println("Turning yellow pin on");
  83.   digitalWrite(yellowPin, HIGH);
  84. }
  85.  
  86. void _onInterruption() {
  87.   Serial.println("On interruption");
  88.   state = !state;
  89. }
  90.  
  91. void _startCounting() {
  92.   Serial.println("Counting..");
  93.   while (startCounting) {
  94.     //As soon as it's counting, run servo
  95.     runServo = !runServo;
  96.     if (millis() - timeArduinoStarted >= 10000) {
  97.       timeArduinoStarted = millis();
  98.      
  99.       Serial.println("Show temperature");
  100.  
  101.       showTemp = !showTemp;
  102.      
  103.       //Stop counting
  104.       startCounting = !startCounting;
  105.      
  106.       //Stop running servo
  107.       runServo = !runServo;
  108.      
  109.       //Turning leds off
  110.       state = !state;
  111.       digitalWrite(yellowPin, LOW);
  112.  
  113.      //Turning green pin on
  114.       digitalWrite(greenPin,HIGH);
  115.     }
  116.   }
  117. }
  118.  
  119. void _displayTemp() {
  120.  
  121.   int tempSensor=0;
  122.   float tempVoltage=0;
  123.   float tempDegrees;
  124.  
  125.   tempSensor  = analogRead(sensorTempPin);
  126.   tempVoltage = tempSensor * (3300/1024);
  127.   tempDegrees = (tempVoltage - 500) / 10;
  128.    
  129.     if (showTemp) {
  130.         Serial.println("tempDegrees");
  131.         Serial.println(tempDegrees);
  132.         showTemp = !showTemp;
  133.     }
  134.  
  135.     if(tempDegrees < tempReached) {
  136.          _runBuzzer();
  137.     }
  138. }
  139.  
  140. void _runServo() {
  141.   myservo.attach(10);
  142.   for (valServo = 0; valServo <= 180; valServo += 1) {
  143.     myservo.write(valServo);
  144.     delay(15);
  145.   }
  146.   for (valServo = 180; valServo >= 0; valServo -= 1) {
  147.     myservo.write(valServo);
  148.     delay(15);
  149.   }
  150.   myservo.detach();
  151.   runServo = !runServo;
  152. }
  153.  
  154. void _runBuzzer() {
  155.   tone(pinBuzzer, 1000);
  156.   delay(1000);      
  157.   noTone(pinBuzzer);  
  158.   delay(1000);
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement