Hi.
I am new to this forum, so bare with me.
For some time now I have been doing a project for my chickencoop.
Evrything works as intended.
But....
I would like to incorporate a timeout function for my "Door Down" and "Door Up" function in case of reed switch failiure or some other fault with the door.
I understand i have to incorporate a millis function in my while-loop, but im having trouble with understanding how to construct the code.
void oppover()//---------------------------------Make door move up-----------------------------
{
while(digitalRead(topSwitchPin)==HIGH)
{
digitalWrite(motorOpp, HIGH);
digitalWrite(motorNed, LOW);
analogWrite(fart, 1023);
Blynk.run();
if (digitalRead(topSwitchPin)==HIGH && digitalRead(bottomSwitchPin)==HIGH)
{
led.off();
}
if (digitalRead(topSwitchPin)==LOW)
{
stopp();
}
}
}
All of the code
#define BLYNK_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define fart 2
#define motorNed 16
#define motorOpp 1
#define bottomSwitchPin 14
#define topSwitchPin 12
#define ONE_WIRE_BUS 0
#define BLYNK_Green "#23C48E"
#define BLYNK_Red "#D3435C"
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
WidgetLED led(V10);
SimpleTimer timer;
void ledwidget() {
if (digitalRead(bottomSwitchPin)==LOW) {
led.on();
led.setColor(BLYNK_Red);
}
if (digitalRead(topSwitchPin)==LOW) {
led.on();
led.setColor(BLYNK_Green);
}
if (digitalRead(topSwitchPin)==HIGH && digitalRead(bottomSwitchPin)==HIGH) {
led.off();
}
}
char auth[] = blablabla
char ssid[] = blablabla
char pass[] = blablabla
void setup(void) {
Serial.begin(9600); // initialize serial port hardware
Blynk.begin(auth, ssid, pass);
pinMode(motorOpp, OUTPUT);
pinMode(motorNed, OUTPUT);
digitalWrite(motorOpp, LOW);
digitalWrite(motorNed, LOW);
pinMode(fart, OUTPUT);
analogWrite(fart, 0);
pinMode(bottomSwitchPin, INPUT_PULLUP); // set bottom switch pin as input
pinMode(topSwitchPin, INPUT_PULLUP); // set top switch pin as input
sensors.begin();
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
Serial.print("Device 0 Address: ");
printAddress(insideThermometer);
sensors.setResolution(insideThermometer, 9);
timer.setInterval(1000L, ledwidget);
}
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.println(tempC);
Blynk.virtualWrite(V4, tempC+1);
}
BLYNK_WRITE(V1)
{
int i=param.asInt();
if (i==1)
{
oppover();
}
else
{
stopp();
}
}
BLYNK_WRITE(V2)
{
int i=param.asInt();
if (i==1)
{
nedover();
}
else
{
stopp();
}
}
void oppover()
{
while(digitalRead(topSwitchPin)==HIGH)
{
digitalWrite(motorOpp, HIGH);
digitalWrite(motorNed, LOW);
analogWrite(fart, 1023);
Blynk.run();
if (digitalRead(topSwitchPin)==HIGH && digitalRead(bottomSwitchPin)==HIGH)
{
led.off();
}
if (digitalRead(topSwitchPin)==LOW)
{
stopp();
}
}
}
void nedover()
{
while(digitalRead(bottomSwitchPin)==HIGH)
{
digitalWrite(motorNed, HIGH);
digitalWrite(motorOpp, LOW);
analogWrite(fart, 1023);
Blynk.run();
if (digitalRead(topSwitchPin)==HIGH && digitalRead(bottomSwitchPin)==HIGH)
{
led.off();
}
if (digitalRead(bottomSwitchPin)==LOW)
{
stopp();
}
}
}
void stopp()
{
digitalWrite(motorNed, LOW);
digitalWrite(motorOpp, LOW);
analogWrite(fart, 0);
}
void loop() {
Blynk.run();
timer.run();
sensors.requestTemperatures();
printTemperature(insideThermometer);
}
It may be bad coding, but works as intended.