Look for some advice or help.
I have a Monitoring system to monitor a couple of sensors in a Room (Movement (PIR) Magnet door Sensor and Pressure Pad)
Im having an issue with an If Else statement that i cannot get to work, maybe I'm missing something. Been racking my brain on this for about a week now, it worked 2 weeks ago, and me being a dumbass didn't save a version of the working code.
Here is the full code for my project.
#include <Wire.h>
#include <LiquidCrystal.h>
#include <dht.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
dht DHT;
String sensorNodeID = "Bedroom";
//Analog Inputs
#define DHT11_PIN 27
//Digital PWM Inputs
const int pressPad = 22; // Pressure Pad Switch (Arduino Mega Pin 22)
const String PRESSURE_SENSOR = String("EVENT,0,pressure=");
const int doorMagnet = 23; //Door Magnet
const String DOOR_SENSOR = String("EVENT,0,door=");
const int moveSensor = 24; // Movement Sensor
const String MOVEMENT_SENSOR = String("EVENT,0,movement=");
const int SysTestSwitch = 26; // Audio and Visual Alert Test Switch (Digital 26).
const int alertResetSwitch = 31;
const int alarm = 6; // Alarm = (Pin Digital 6).
//4x Relay Module.
const int relay1 = 40; //Relay 1 (Pin Digital 22-MEGA). - Blue Front Door Strobe
const int relay2 = 41; //Relay 2 on Digital Pin 23-MEGA. Red Room Identifier Strobe
const int relay3 = 42; //Relay 3 on Digital Pin 24-MEGA. Yellow Control Panel Strobe
const int relay4 = 43; //Relay 4 on digital Pin 25-MEGA. Alarm/Buzzer.
// byte pin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53};
// byte pinCount = sizeof(pin) / sizeof(pin[0]);
const char deg = 0xDF;
int data = 0;
int value, Count;
int val = 0;
String c;
int timerval = 0;
// Button Press Timer Count Limit
const long CYCLE = 10000; // Once Tested Change this to 5 Minutes 60000.
long time = millis();
void setup() {
pinMode (10, OUTPUT);
digitalWrite(10, HIGH); //LCD Backlight High
lcd.begin(16, 2); // Start the LCD Panel Display.
//lcdTest(); //Test the LCD Screen is Functioninig.
Serial.begin(9600); // Start the USB Serial Monitor.
pinMode(pressPad, INPUT_PULLUP); // Pressure Pad
pinMode(moveSensor, INPUT_PULLUP); // Movement Sensor
pinMode(doorMagnet, INPUT_PULLUP); // Door Magnet
pinMode(SysTestSwitch, INPUT_PULLUP); // System Alert Test Switch
//4x Relay Board.
pinMode (relay1, OUTPUT); // House External Strobe (Blue).
pinMode (relay2, OUTPUT); // Room Strobe (Red).
pinMode (relay3, OUTPUT); // Alarm / Buzzer.
pinMode (relay4, OUTPUT); // Alert Panel Strobe.
// for (byte i = 0; i < pinCount; i++)
{
//pinMode(pin[i], OUTPUT);
}
}
// End of Void Setup
// This is the Main Loop That Runs Constantly.
void loop() {
if (pressPad == 1) {
emergencyAlertFunction();
}
else if (SysTestSwitch == 1) {
testEmergencyAlertFunction();
}
else {
displaySensorData();
}
}
//This Ends the Loop Function.
void displaySensorData(){
// Send Data to the LCD Display
lcd.clear();
lcd.print("MAVIS: NORMAL");
lcd.setCursor(0, 1);
int chk = DHT.read11(DHT11_PIN);
lcd.print("T="); // LCD Display Room Tempreture
lcd.print(DHT.temperature);
lcd.print(" H=");
lcd.print(DHT.humidity);
//Send Sensor data to the Serial Monitor.
Serial.println("MAVIS: NORMAL");
// Room Tempreture Sensor
Serial.print("Room Temp: ");
Serial.print(DHT.temperature);
Serial.print(" | ");
// Room Humidity Sensor
Serial.print("Room Hum: ");
Serial.print(DHT.humidity);
Serial.print(" | ");
// Pressure Pad
Serial.print("Pressure Pad: ");
int pressPadState = digitalRead(pressPad);
Serial.print(pressPadState); //Display Pressure Pad Data to the Serial Monitor
Serial.print(" | ");
// Door Magnet
Serial.print("Door Magnet: ");
int doorState = digitalRead(doorMagnet);
Serial.print(doorState); // Display Door Magnet Data
Serial.print(" | ");
// Movement Sensor State
Serial.print("Movement: ");
int moveState = digitalRead(moveSensor);
Serial.print(moveState); // Display Movement Sensor Data
Serial.print(" | ");
// System Testing Switch Status
Serial.print("System Test Switch: ");
int SysTestSwitchState = digitalRead(SysTestSwitch);
Serial.print(SysTestSwitchState); // Display Alert Test Btn State
Serial.println();
digitalWrite(relay1, HIGH); //Yellow Strobe On.
digitalWrite(relay2, HIGH); //Blue Strobe On.
digitalWrite(relay3, HIGH); //Red Strobe On.
digitalWrite(relay4, HIGH); //Alarm/Buzzer On.
delay(2000);
}
void emergencyAlertFunction()
{
lcd.clear();
lcd.print("MAVIS: EMERGENCY");
Serial.println("MAVIS EMERGENCY ALERT: ACTIVE"); // Display Test to Serial Monitor
digitalWrite(relay1, LOW); //Yellow Panel Strobe On.
digitalWrite(relay2, LOW); //Blue Front Door Strobe On.
digitalWrite(relay3, LOW); //Red Room Identifier Strobe On.
digitalWrite(relay4, LOW); //Alarm/Buzzer On.
//Add Code here to Send SMS To Dedicated Mobile number.
delay(2000);
}
//Audio and Visual Device Testing Function.
void testEmergencyAlertFunction(){
//LCD Panel Display Information
lcd.print("MAVIS: TEST");
lcd.setCursor(0, 1);
lcd.print("TEST SWITCH: ON");
// Serial Monitor Information
Serial.println("MAVIS TEST SYSTEM ACTIVE");
Serial.println("ALERT SYSTEM TEST SWITCH: ACTIVE" );
Serial.print ("System Test Switch State: " );
int TestSwitchState = digitalRead(SysTestSwitch);
Serial.println(TestSwitchState);
digitalWrite(relay1, LOW); //Yellow Strobe On.
digitalWrite(relay2, LOW); //Blue Strobe On.
digitalWrite(relay3, LOW); //Red Strobe On.
digitalWrite(relay4, LOW); //Alarm/Buzzer On.
delay(2000);
} //End of Alert Test Function
// Function to Set All Pins Low.
void allPinsLow()
{
//for (byte i = 0; i < pinCount; i++)
{
//digitalWrite(pin[i], LOW); //Set All Pins LOW
}
}
//void getTemp(){
// value = analogRead(roomTemp);
//float resistance = (1023.0-value)/value;
//float kelvin = 1/(log(resistance)/3975 + 1/298.15);
//float celcius = kelvin - 273.15;
//String c = String(celcius);
//}
void getHumidity(){}
void getPressurePad(){}
void getDoorMagnet(){}
void getMoveSensor(){}
void getAlertTestSwitch(){}
void alertReset()
{
if (alertResetSwitch == 1){
lcd.print("Reset Button Pressed");
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
}
}
// Sends Emergency Text Function when the pressure pad is high for 5 minutes.
void sendSMSEmergency()
{
lcd.setCursor(0, 1);
lcd.print("Sending Emergency TXT...");
Serial.println("Sending Message --->");
delay(2000);
// Put Code here to Send SMS Text Message
}
// Sends Test SMS Text Function for Testing When Testing the System
void sendSMSTest()
{
Serial.println("MAVIS TEXT ALERT SYSTEM STARTING");
delay(2000);
// Put Code Here to Send SMS Text Message
}
//Send Data to MySQL via WIFI
void sendDataWifi()
{
}
//Store Data in SD Card
void sendDataSDCard()
{
}
void lcdTest(){
lcd.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
lcd.setCursor(0, 1);
lcd.print("1234567890.-+=");
delay(3000);
lcd.clear();
lcd.print("LCD TEST");
lcd.setCursor(0, 1);
lcd.print("COMPLETE");
lcd.clear();
delay(3000);
}
//REBOOTS/Resets THE SOFTWARE
void software_reboot()
{
asm volatile (" jmp 0");
}
In the Void Loop() section i have an If - Else If - Else statement.
I also have a Key type Switch on Pin 26 (Arduino Mega) what i want to happen is when the (SysTestSwitch) switch is turned on/High or 1, to run function: testEmergencyAlertFunction();
I can see in the Serial Monitor it being 0 when off/Low and 1 when high.
This is the Void Loop
void loop() {
if (pressPad == 1) {
emergencyAlertFunction();
}
else if (SysTestSwitch == 1) {
testEmergencyAlertFunction();
}
else {
displaySensorData();
}
}
I have tried >= 1, and > 0, and != 0. It's just not switching to that function using the switch.
if i change the code it runs perfectly, but not when i use the switch it does nothing except change value i the Serial Monitor.
Im using breadboard with a Pull Up resistor.
Im really stuck on this. Any help you can give would be great.
Many thanks
Mark