I’m having a bit of trouble understanding while loops which I want to use in a function to reset an RTC.
I have copied a bit of my code and the pertinent functions are checkReset() and resetTime()
I’m afraid it is in a ‘developmental’ state and there is lots of ‘commenting out’.
I have three button switches. The first simply calls CheckReset() which is meant to screen out inadvertent button presses. From there I either return to loop() or enter resetTime().
That works fine and the first part of the reset function also does what I want,
in that it resets the ‘year’ variable but adding code to reset the month variable produces no effect. (so I have commented out this part - as I have commented out the code sending the reset variables to the RTC - which works fine).
I imagined that once the ‘year’ while loop ended the following ‘month’ loop would start and so on until the end of the resetTime() function so what am I missing apart from brains?
Initially I intended using a global variable ‘wait’ (unsigned long) to control the loops but I can see that might confuse the system so I tried using different local variables with no result.
One other thing, I first used ‘break’ to get out of the while loop when the variable ‘year’ was correct but it did not close the loop.
#include <Wire.h>
#include<LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>
#include <BH1750.h>
#include <RealTimeClockDS1307.h>
BH1750 lightMeter;
#define DS1307_ADDRESS 0x68
#define BACKLIGHT_PIN 3
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
//The position of the following is critical!
byte decToBcd(byte val) {
return ((val / 10 * 16) + (val % 10));
}
byte bcdToDec(byte val) {
return ((val / 16 * 10) + (val % 16));
}
OneWire ds(10); // on pin 10
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7); // 0x3F is the LCD address
byte amend;
byte atd ;// difference between inside air temp and OAT. ATMOSPHERE
byte breeze; //ATMOSPHERE
byte count1 = 0; //atmosphere
byte date = 0; //checktime, settime, readtime
byte day = 0;
byte edit = 0;
byte heaterState = 0;
byte heatStart = 0;
byte hour = 0;
byte hrst = 0;
byte lastInput = 0;
byte lightLevel;
byte minute = 0;
byte month = 3;
byte No = 7;
//byte no;
byte orbit;
byte resetRTC = 0;
byte second = 0;
byte startup = 1; //heating
byte intSwitch = 6;
byte year = 16;
byte Yes = 8;
//byte yes;
byte zero = 0x00; //Workround for issue #527
unsigned long wait;
float adjustment; //heating
float air; //atmosphere
float baseTemp; //heating
float basic[] = {0, 12.5, 12.5, 12.5, 14.00, 15.95, 18, 18, 18, 18, 16, 14, 12.5};
float controlTemp;
float dayEnvelope;
float dvt = 0.01875;
float hTime;
float hysteresis = 0.25;
float temperature;
float minNow = (hour * 60) + minute + (second / 60);
float variation[] = {0, 0, 0, 0.05, 0.065, 0.065, 0, 0, 0, 0.065, 0.065, 0.05, 0};
//SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
void setup() {
Wire.begin();
lcd.begin(20, 4);
Serial.begin(9600);
dht.begin();
lightMeter.begin();
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
pinMode(2, OUTPUT); //Heat
pinMode(6, INPUT); //reset
pinMode(7, INPUT); //the no switch
pinMode(8, INPUT); //the yes switch
pinMode(5, OUTPUT); //Rain
pinMode(9, OUTPUT); //Humidity
} //End Setup
//==================================================================
void loop() {
displayTime();
heating();
humidity();
//displayTime();
light();
atmosphere();
if (digitalRead(intSwitch) == HIGH)checkReset();
}
//==================================================================
void checkReset() {
/*while (digitalRead(Yes) == HIGH || digitalRead(No) == HIGH) {
//Do nothing to make sure Yes/No button isn't pressed inadvertantly
}
*/
lcd.clear();
lcd.setCursor(0, 2);
lcd.print(" Change Date/Time?");
lcd.setCursor(0, 3);
lcd.print("No Yes");
wait = (millis() + 10000); // Response time = 10 seconds
while (wait > millis()) { //this is the quit timer
if (digitalRead(No) == HIGH) {
wait = millis(); //to get out of loop
}
if (digitalRead(Yes) == HIGH) {
delay(500);
resetTime();
lcd.clear();
}
}
// if no input return after (ten) seconds
lcd.clear();
}
// End of 'checkReset'
//----------------------------------------------------------
void resetTime() {
wait = (millis() + 10000);
lcd.clear();
while (wait > millis()) {
lcd.print("The year is 20");
lcd.print(year);
lcd.setCursor(6, 2);
lcd.print("Correct?");
lcd.setCursor(0, 3);
lcd.print("No Yes");
if (digitalRead(No) == 1) {
++year;
if (year > 21)year = 16;
delay(500);
wait = (millis() + 10000);
lcd.clear();
}
if (digitalRead(Yes) == 1) {
lcd.clear();
wait = millis();
}
} // End year loop
//------------------------------------
/*
wait = (millis() + 10000);
lcd.clear();
while (wait> millis()) {
lcd.print("The month is ");
if (month < 10)lcd.print(" ");
lcd.print(month);
lcd.setCursor(6, 2);
lcd.print("Correct?");
lcd.setCursor(0, 3);
lcd.print("No Yes");
if (digitalRead(No) == 1) {
++month;
if (month > 12)month = 1;
delay(500);
wait = (millis() + 10000);
lcd.clear();
}
if (digitalRead(Yes) == 1) {
lcd.clear();
wait = millis();
}
} // End month loop
*/
//------------------------------------------------------------------
/* Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.write (decToBcd(second));
Wire.write (decToBcd(minute));
Wire.write (decToBcd(hour));
Wire.write (decToBcd(day));
Wire.write (decToBcd(date));
Wire.write (decToBcd(month));
Wire.write (decToBcd(year));
Wire.write(zero);
Wire.endTransmission();
lcd.clear();
*/
} //end of resetTime