Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Display.h"
- #include <Bounce2.h>
- const int RLED = 4; // Red Led.
- const int BTN_Display = 8; // Button switches between modes.
- const int BTN_Alarm = 9; // Button activates alarm.
- Bounce Switch = Bounce(); // Debounces BTN_Display.
- Bounce Alarm = Bounce(); // Debounces BTN_Alarm.
- const int PIN_POTMETER = 14; // Potentiometer
- const int MAX_ANGLE = 280; // Max angle set to 30 degrees.
- const int PIN_NTC = 15;
- const int NTC_R25 = 10000;
- const int NTC_MATERIAL_CONSTANT = 3950;
- float temperature, resistance;
- int value;
- float celcius;
- int angle;
- bool ALARM = false; // Boolean variable for the alarm.
- bool TEMP_THRESHOLD = false; // Boolean variable for the temperature alarm.
- int modeD = 0; // Variable to switch between modes.
- String winform_Output; // String for the windows form output.
- int get_knob_angle()
- {
- int sensor_value = analogRead(PIN_POTMETER);
- int angle;
- angle = map(sensor_value, 0, 9500, 0, MAX_ANGLE);
- return angle;
- }
- float get_temperature()
- {
- value = analogRead(PIN_NTC);
- resistance = (float)value * NTC_R25 / (1024 - value);
- temperature = 1 / (log(resistance / NTC_R25) / NTC_MATERIAL_CONSTANT + 1 / 298.15) - 273.15;
- return temperature;
- }
- 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...
- {
- digitalWrite(RLED, HIGH);
- }
- void setup() {
- Serial.begin(9600);
- pinMode(RLED, OUTPUT);
- pinMode(BTN_Display, INPUT_PULLUP);
- Switch.attach(BTN_Display); // Button debouncing.
- Switch.interval(5); // Interval for the button debouncing.
- pinMode(BTN_Alarm, INPUT_PULLUP);
- Alarm.attach(BTN_Alarm);
- Alarm.interval(5);
- pinMode(PIN_POTMETER, INPUT);
- Serial.setTimeout(300); // How often it reads the windows form app output. (interval)
- }
- void loop() {
- Switch.update(); // Updates the BTN_Display
- Alarm.update(); // Updates the BTN_Alarm
- angle = get_knob_angle(); // Gets angle
- celcius = get_temperature(); // Gets temperature
- Serial.println(celcius); // Prints temperature to the windows form app
- winform_Output = Serial.readStringUntil('\n'); // Reads from windows form app untill a new line
- winform_Output.trim(); // Removes excess output from the windows form app
- if (Switch.fell()) // Switches between display modes
- {
- modeD++;
- if (modeD == 4)
- {
- modeD = 1;
- }
- }
- if (modeD == 1)
- {
- Serial.println("HHmm"); // Requests time from windows form app
- Display.show(winform_Output); // Displays time
- }
- if (modeD == 2)
- {
- Display.show(celcius); //displays temerature
- }
- if (modeD == 3)
- {
- Display.show(angle); // displays angle
- }
- if (Alarm.fell() && ALARM == false) //when BTN_Alarm is pressed and the alarm is off, the button turns the alarm on
- {
- ALARM = true; // Turns the alarm on
- Serial.println("Button Alarm is ON!"); //Prints to windows form app
- }
- if (winform_Output == "Button Reset.") //Checks if alarm has been reset from windows form app
- {
- if (ALARM == true) // If the button alarm has been activated, it will reset it
- {
- ALARM = false;
- Serial.println("Button Alarm is OFF!"); //Prints to windows form app
- }
- if (TEMP_THRESHOLD == true)// If the temperature alarm was on, it will reset it
- {
- TEMP_THRESHOLD = false;
- Serial.println("Temperature alarm is OFF!"); //Prints to windows form app
- }
- }
- if ((celcius < 16 || celcius > 30) && TEMP_THRESHOLD == false)// Checks for the temperature threshold and turns on the temperature alarm
- {
- TEMP_THRESHOLD = true;
- Serial.println("Temperature Threshold reached."); //prints to windows form app
- }
- if (ALARM == true || TEMP_THRESHOLD == true) // If either one of the alarms is on, the red LED lights up
- {
- red_led();
- }
- else
- {
- digitalWrite(RLED, LOW);
- }
- delay(50);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement