Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //================================== Libraries Needed ==================================
- #include <SPI.h>
- #include "RF24.h"
- //================================== NRF Pins ==================================
- #define CE_PIN 3
- #define CSN_PIN 5
- // ================================== Relay Control Pin ==================================
- #define fanRelay 4
- // ================================== NRF24L01 Vairables ==================================
- RF24 radio(CE_PIN, CSN_PIN);
- uint8_t address[][6] = { "1Node", "2Node" }; //1Node
- bool radioNumber = 0; // This is Radio 0
- // ================================== NRF24L01 Recovery ==================================
- unsigned long lastReceiveTime = 0; // Used to calculate change in time for timers
- unsigned long recoveryStartTime = 0;
- bool inRecovery = false;
- bool awaitingNewData = false; // Used to force the fan to actually recieve new data when recovering from transmission error before resuming operation
- // ================================== Incoming Data Structure from Reciever ==================================
- struct SensorData {
- float ambientTemp;
- float objectTemp;
- uint16_t CO2; // Not being used in this device, but if another device was using it from same transmission, would be important
- uint16_t TVOC;
- bool flameDetected;
- };
- SensorData receivedData;
- // ================================== Designating fan PWM pin ==================================
- const int fan_control_pin = 9; // analog value from 0 to 255 to control rpm of fan, higher number ='s higher rpm
- // ================================== Flame Detector ==================================
- bool flameCooldown = false;
- unsigned long flameCooldownStart = 0;
- // ================================== Enter Setup ==================================
- void setup() {
- pinMode(fan_control_pin, OUTPUT);
- pinMode(fanRelay, OUTPUT);
- Serial.begin(115200);
- if (!radio.begin()) {
- Serial.println("Radio hardware not responding!");
- while (1); // If Radio isn't working, stalls out program
- }
- radio.setAutoAck(true); //Setup for Transciever
- radio.enableDynamicPayloads();
- radio.setRetries(5, 15);
- radio.setPALevel(RF24_PA_LOW);
- radio.setDataRate(RF24_1MBPS);
- Serial.println(F("This is Radio 0 - Receiver"));
- radio.openWritingPipe(address[radioNumber]);
- radio.openReadingPipe(1, address[!radioNumber]);
- radio.startListening();
- lastReceiveTime = millis();
- }
- // ================================== Enter Main Loop ==================================
- void loop() {
- if (flameCooldown) { // If a flame has been detected, "pause" system for 10 seconds
- unsigned long elapsed = millis() - flameCooldownStart;
- if (elapsed >= 10000) {
- flameCooldown = false;
- Serial.println("Flame cooldown ended. Resuming fan operation.");
- } else {
- unsigned long secondsLeft = (10000 - elapsed) / 1000;
- Serial.print("Flame detected! Cooling down... ");
- Serial.print(secondsLeft); Serial.println("s left.");
- }
- }
- // Check for new data
- if (radio.available()) {
- radio.read(&receivedData, sizeof(SensorData));
- Serial.println("Received data:");
- Serial.print("Ambient: "); Serial.print(receivedData.ambientTemp); Serial.println(" C ");
- Serial.print("Object: "); Serial.print(receivedData.objectTemp); Serial.println(" C ");
- Serial.print("TVOC: "); Serial.print(receivedData.TVOC); Serial.println(" ppb ");
- Serial.print("Flame: "); Serial.println(receivedData.flameDetected ? "Yes" : "No");
- lastReceiveTime = millis(); // Sets the time that a message was last recieved, used to calculate if more then 20 seconds has passed later on
- if (receivedData.flameDetected && !flameCooldown) { //If a flame is being seen by sensor, and the device is currently not reacting to it turn off fan and start timer
- Serial.println("Flame detected — turning off fan for 10 seconds!");
- digitalWrite(fanRelay, LOW);
- analogWrite(fan_control_pin, 0);
- flameCooldown = true;
- flameCooldownStart = millis(); // Assigns a time to flameCooldownStart
- }
- // Exit recovery mode if needed by seeing if new data has popualted in memory
- if (awaitingNewData) {
- Serial.println("New data received — exiting recovery mode.");
- awaitingNewData = false;
- inRecovery = false;
- }
- // Only control the fan if not in recovery or flame cooldown
- if (!inRecovery && !flameCooldown) {
- handleFan(receivedData.TVOC, receivedData.ambientTemp, receivedData.objectTemp); // Fan Logic elsewhere to keep this area clean
- }
- }
- if (!inRecovery && millis() - lastReceiveTime > 20000) { // If new data hasn't been recieved in the last 20 seconds,
- Serial.println("No data received for 20s — entering recovery mode. Turning off fan."); // trys to reboot the device in order to get it working again
- digitalWrite(fanRelay, LOW);
- analogWrite(fan_control_pin, 0);
- Serial.println("Resetting NRF24L01 module...");
- radio.powerDown();
- delay(1000);
- radio.powerUp();
- radio.startListening();
- inRecovery = true;
- recoveryStartTime = millis();
- }
- // Delay recovery mode resume
- if (inRecovery && !awaitingNewData && millis() - recoveryStartTime > 5000) {
- Serial.println("Recovery mode active. Waiting for new data before restarting fan.");
- awaitingNewData = true;
- }
- delay(500);
- }
- // ================================== handleFan Function ==================================
- void handleFan(float TVOC, float ambientTemp, float objectTemp) {
- float tempDelta = objectTemp - ambientTemp; // Comparing the temperature of whats being sensed to the temperature of the area around the sensor
- float fanPWM = TVOC * .2; // TVOC Value is multiplied by scaling coefficient to optimize having a variety of values for PWM output to fan
- if (fanPWM < 60) { //If TVOC value after being scaled by the scaling coefficient is still less then 60, fan stays/turns off
- digitalWrite(fanRelay, LOW);
- analogWrite(fan_control_pin, 0);
- Serial.println("RELAY OFF (low TVOC)");
- Serial.println();
- return;
- }
- if (tempDelta > 5.0) { //If the difference in ambient temperature from the temperature being sensed is more then 5 degrees Celsius, scale up PWM output
- fanPWM *= 1.25; // Increase 25%
- Serial.println("Object temp much higher than ambient — boosting fan.");
- }
- if (fanPWM > 254){ //If output exceeds what it can possibly be, clamps the highest value at 255, which is max Output for the fan
- Serial.println("Due to simulating enviroment for capstone presentation, value exceeds normal expected range: Clamping at 255");
- Serial.print("Current value: ");
- Serial.print(fanPWM);
- fanPWM = 255;
- }
- digitalWrite(fanRelay, HIGH); //Makes sure the fan is on
- analogWrite(fan_control_pin, fanPWM); //Fan is told to run at value of fanPWM after it had been scaled
- Serial.print("Fan running at PWM: "); Serial.println(fanPWM);
- Serial.println();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement