@ J-M-L Thank you, The control system is for an outside greenhouse and is replacing the one I constructed and programmed many years ago using PIC 16F876 controller.
The entrance door has two single button push switches (one inside and one outside) both connected in parallel. Push to enter and the door opens and remains open until either the internal or external button is pressed a second time upon which it then closes. If the outside temperature is higher than the set point the vent will open and so does the door should there be a further increase, subject to the control parameters set within the program.
I have amended my code and tested it using the model testing platform and all works fine. However, I would imagine programmers looking over it could offer improvements.
I've posted my working program here:-
/*********************
Greenhouse relay motor control with door toggling OPEN/CLOSE switch
Vent fan 5v logic high drive signal output
Humidity circulation fan ( via relay 4 when configured)
15/11/2019
*********************/
#define DHTPIN 4 // pin 4 sensor is connected to
#include <DHT.h>
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// *************************************** motor (1) door control motor = in1,in2,enA ***************************
int led = 13;
int status = false;
int in1 = 12;
int in2 = 11;
int enApin = 10;
int outPin = 7; // external door OPEN indicator led
long time = 0; // the last time the output pin was toggled
long debounce = 200;
int vent = 9; // window vent fan
int fan = 8; // humidity fan
int buttonLpin = 6; // left side door switch
int buttonRpin = 5; // right hand door switch
int val = 0;
int ledPin = 13; // door locked open led
int buttonState = 0;
int lastButtonState = 0;
const int buttonPin = 2; // the pin that the pushbutton is attached to
int buttonPushCounter = 0; // counter for the number of button presses
const uint32_t debounceTime = 10; // 5 mSec, enough for most switches
int maxTemp2 = 28; // max set at 28 for door
int minTemp2 = 24; // min set at 24 " "
int maxTemp = 18; // max set at 18 for vent
int minTemp = 15; // min set at 15 for vent
int maxHum = 60;
int minHum = 55;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
pinMode(outPin, OUTPUT);
pinMode(ledPin, OUTPUT); // door locked open
pinMode(buttonPin, INPUT_PULLUP); // set the internal pull up, door button.
pinMode (vent, OUTPUT); // ventfan
pinMode (enApin, OUTPUT); // motor conrol module enable
pinMode (in1, OUTPUT); // motor control
pinMode (in2, OUTPUT); // motor control
pinMode (fan, OUTPUT); // humidity fan
pinMode (buttonLpin, INPUT_PULLUP); // left door switch
pinMode (buttonRpin, INPUT_PULLUP); // right door switch
Serial.begin(9600);
dht.begin();
}
void loop() {
// *********** Door press toggle button...push to open / close ***********
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
} else {
// if the current state is LOW then the button went from on to off:
}
// Delay a little bit to avoid bouncing
delay(50);
}
lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0) {
digitalWrite(ledPin, HIGH); // Uno on board led
digitalWrite(outPin, HIGH); // door lock open led (ON)
digitalWrite(in1, LOW); // motor direction
digitalWrite(in2, LOW); // motor direction
delay(200); // let motor direction relays settle before actuating 12v relay
val = digitalRead(buttonLpin);
val = digitalRead(buttonLpin); // read the input pin
digitalWrite(enApin, val); // set 12v relay
return; // return and loop
} else {
digitalWrite(ledPin, LOW); // return to program,door lock open led off
digitalWrite(outPin, LOW); // door lock open led (OFF)
}
float h = dht.readHumidity(); // checking humidity
// Read temperature as Celsius
float t = dht.readTemperature();
// ============================== VENT CHECK ============
if (t > maxTemp) {
digitalWrite(vent, HIGH);
} else if (t < minTemp) { // if(h < min temp digitalWrite(vent, LOW);
// =============================== DOOR CHECK (temp2) OPENING FOR HIGH TEMPERATURE
}
if (t > maxTemp2) {
val = digitalRead(buttonLpin); // read the door limit left micro switch
digitalWrite(enApin, val); // enable 12v switching relay (in3)
delay( debounceTime );
digitalWrite(in1, LOW); // door motor >>>> OPEN pin 12
digitalWrite(in2, LOW); // " " " " pin 11
// ======================================= door closing / for temp2 = LOW TEMPERATURE
} else if (t < minTemp2) {
val = digitalRead(buttonRpin); // read the door limit right micro switch
digitalWrite(enApin, val); // sets the 12v switching relay (in3)
delay( debounceTime );
digitalWrite(in1, HIGH); // door motor >>>> CLOSE pin 12
digitalWrite(in2, HIGH); // " " " " pin 11
} //=================== checking humidity sensors to control fan if high fan on
if (h > maxHum) {
digitalWrite(fan, HIGH); // 5v output control line HIGH
} else if (h < minHum) {
digitalWrite(fan, LOW); // 5v output control line LOW
}
}