Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. #include "Display.h"
  2. #include <Bounce2.h>
  3.  
  4. const int RLED = 4; // Red Led.
  5. const int BTN_Display = 8; // Button switches between modes.
  6. const int BTN_Alarm = 9; // Button activates alarm.
  7. Bounce Switch = Bounce(); // Debounces BTN_Display.
  8. Bounce Alarm = Bounce(); // Debounces BTN_Alarm.
  9.  
  10. const int PIN_POTMETER = 14; // Potentiometer
  11. const int MAX_ANGLE = 280; // Max angle set to 30 degrees.
  12.  
  13. const int PIN_NTC = 15;
  14. const int NTC_R25 = 10000;
  15. const int NTC_MATERIAL_CONSTANT = 3950;
  16. float temperature, resistance;
  17. int value;
  18.  
  19. float celcius;
  20. int angle;
  21.  
  22. bool ALARM = false; // Boolean variable for the alarm.
  23. bool TEMP_THRESHOLD = false; // Boolean variable for the temperature alarm.
  24. int modeD = 0; // Variable to switch between modes.
  25.  
  26. String winform_Output; // String for the windows form output.
  27.  
  28. int get_knob_angle()
  29. {
  30. int sensor_value = analogRead(PIN_POTMETER);
  31. int angle;
  32. angle = map(sensor_value, 0, 9500, 0, MAX_ANGLE);
  33. return angle;
  34. }
  35.  
  36. float get_temperature()
  37. {
  38. value = analogRead(PIN_NTC);
  39. resistance = (float)value * NTC_R25 / (1024 - value);
  40. temperature = 1 / (log(resistance / NTC_R25) / NTC_MATERIAL_CONSTANT + 1 / 298.15) - 273.15;
  41. return temperature;
  42. }
  43.  
  44. void red_led() // Method that turns on the red LED; created a method because for some reason it lagged when the line was only digitalWrite...
  45. {
  46. digitalWrite(RLED, HIGH);
  47. }
  48.  
  49.  
  50.  
  51. void setup() {
  52. Serial.begin(9600);
  53. pinMode(RLED, OUTPUT);
  54. pinMode(BTN_Display, INPUT_PULLUP);
  55. Switch.attach(BTN_Display); // Button debouncing.
  56. Switch.interval(5); // Interval for the button debouncing.
  57. pinMode(BTN_Alarm, INPUT_PULLUP);
  58. Alarm.attach(BTN_Alarm);
  59. Alarm.interval(5);
  60. pinMode(PIN_POTMETER, INPUT);
  61. Serial.setTimeout(300); // How often it reads the windows form app output. (interval)
  62.  
  63. }
  64.  
  65. void loop() {
  66.  
  67. Switch.update(); // Updates the BTN_Display
  68. Alarm.update(); // Updates the BTN_Alarm
  69.  
  70. angle = get_knob_angle(); // Gets angle
  71. celcius = get_temperature(); // Gets temperature
  72. Serial.println(celcius); // Prints temperature to the windows form app
  73.  
  74. winform_Output = Serial.readStringUntil('\n'); // Reads from windows form app untill a new line
  75. winform_Output.trim(); // Removes excess output from the windows form app
  76.  
  77. if (Switch.fell()) // Switches between display modes
  78. {
  79. modeD++;
  80. if (modeD == 4)
  81. {
  82. modeD = 1;
  83. }
  84. }
  85.  
  86. if (modeD == 1)
  87. {
  88. Serial.println("HHmm"); // Requests time from windows form app
  89. Display.show(winform_Output); // Displays time
  90. }
  91.  
  92. if (modeD == 2)
  93. {
  94. Display.show(celcius); //displays temerature
  95. }
  96.  
  97. if (modeD == 3)
  98. {
  99. Display.show(angle); // displays angle
  100. }
  101.  
  102. if (Alarm.fell() && ALARM == false) //when BTN_Alarm is pressed and the alarm is off, the button turns the alarm on
  103. {
  104. ALARM = true; // Turns the alarm on
  105. Serial.println("Button Alarm is ON!"); //Prints to windows form app
  106. }
  107.  
  108. if (winform_Output == "Button Reset.") //Checks if alarm has been reset from windows form app
  109. {
  110. if (ALARM == true) // If the button alarm has been activated, it will reset it
  111. {
  112. ALARM = false;
  113. Serial.println("Button Alarm is OFF!"); //Prints to windows form app
  114. }
  115. if (TEMP_THRESHOLD == true)// If the temperature alarm was on, it will reset it
  116. {
  117. TEMP_THRESHOLD = false;
  118. Serial.println("Temperature alarm is OFF!"); //Prints to windows form app
  119. }
  120. }
  121.  
  122. if ((celcius < 16 || celcius > 30) && TEMP_THRESHOLD == false)// Checks for the temperature threshold and turns on the temperature alarm
  123. {
  124. TEMP_THRESHOLD = true;
  125. Serial.println("Temperature Threshold reached."); //prints to windows form app
  126. }
  127.  
  128. if (ALARM == true || TEMP_THRESHOLD == true) // If either one of the alarms is on, the red LED lights up
  129. {
  130. red_led();
  131. }
  132. else
  133. {
  134. digitalWrite(RLED, LOW);
  135. }
  136. delay(50);
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement