timesetup not in void setup()

This commit is contained in:
mrbesen 2022-07-03 12:54:04 +02:00
parent f2120f4df1
commit 499b944173
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
1 changed files with 125 additions and 113 deletions

View File

@ -11,7 +11,6 @@
DS3231 clock; DS3231 clock;
#define pressed digitalRead(12) == LOW
#define lightsens 1 #define lightsens 1
#if lightsens #if lightsens
#define lightsensorpin A0 #define lightsensorpin A0
@ -39,134 +38,147 @@ bool brightmode = true;
bool houtremp; bool houtremp;
inline bool pressed() {
return digitalRead(12) == LOW;
}
void set(const unsigned char pin, bool on) { void set(const unsigned char pin, bool on) {
if(on) { if(on) {
if(brightmode) { if(brightmode) {
digitalWrite(pin, HIGH); digitalWrite(pin, HIGH);
} else { } else {
analogWrite(pin, ontime_lb); analogWrite(pin, ontime_lb);
} }
} else { } else {
analogWrite(pin, brightmode ? offtime : offtime_lb); analogWrite(pin, brightmode ? offtime : offtime_lb);
} }
} }
//prints the data to the lasers, write(0,0)-> all off, write()-> all on //prints the data to the lasers, write(0,0)-> all off, write()-> all on
void write(unsigned char m = 255, unsigned char h = 255) { void write(unsigned char m = 255, unsigned char h = 255) {
//write minutes //write minutes
for(unsigned char i = 0; i < 6; i++) { for(unsigned char i = 0; i < 6; i++) {
set(M[i], m & (1 << i)); set(M[i], m & (1 << i));
// analogWrite(M[i], (m & (1 << i)) ? 255 : offtime); }
} //write hour
//write hour for(unsigned char i = 0; i < 4; i++) {
for(unsigned char i = 0; i < 4; i++) { set(H[i], h & (1 << i));
set(H[i], h & (1 << i)); }
// analogWrite(H[i], (h & (1 << i)) ? 255 : offtime);
}
} }
inline void writeTime() { inline void writeTime() {
write(clock.getMinute(), clock.getHour(houtremp, houtremp)); write(clock.getMinute(), clock.getHour(houtremp, houtremp));
} }
void setup() { void setup() {
pinMode(13, OUTPUT); pinMode(13, OUTPUT); // Pin 13 LED
//setup clock modul //setup clock modul
Wire.begin(); Wire.begin();
clock.setClockMode(true); //set 12h format clock.setClockMode(true); //set 12h format
clock.turnOffAlarm(1);//disable Arlam clock.turnOffAlarm(1);//disable Arlam
clock.turnOffAlarm(2); clock.turnOffAlarm(2);
//Setup pins //Setup pins
for(unsigned char m = 0; m < 6; m++) { for(unsigned char m = 0; m < 6; m++) {
pinMode(M[m], OUTPUT); pinMode(M[m], OUTPUT);
} }
for(unsigned char h = 0; h < 4; h++) { for(unsigned char h = 0; h < 4; h++) {
pinMode(H[h], OUTPUT); pinMode(H[h], OUTPUT);
} }
pinMode(12, INPUT_PULLUP); pinMode(12, INPUT_PULLUP); // button
write();
//setup messure array //setup messure array
int brightness = analogRead(lightsensorpin); int brightness = analogRead(lightsensorpin);
//memset(messure, brightness, messure_size); //does only work for integers! for(int i = 0; i < messure_size; i++) {
for(int i = 0; i < messure_size; i++) { messure[i] = brightness;
messure[i] = brightness; }
} messure_add = brightness * messure_size;
messure_add = brightness * messure_size; }
while(!pressed) delay(5);//wait for press
//setup time void blink() {
char mode = 0;//mode = 0-> set hour, mode = 1 -> set min, mode = 2 -> exit setup for(int i = 0; i < 6; i++) {
bool pres = true;//was button pressed write(0, 0); // full off
unsigned long timeout = millis(); delay(300);
writeTime(); write(); // full on
while(mode < 2) { delay(300);
if(pressed) {//button is pressed }
if(!pres) {//button was not pressed before }
timeout = millis();//reset timeout
pres = true; void setTime() {
if(mode == 0) {//count hour while(pressed()) delay(5); // wait for not pressed anymore
byte hour = clock.getHour(houtremp, houtremp)+1;
clock.setHour(hour == 13 ? 1 : hour); write(); // full on
} else {//count min
byte minute = clock.getMinute()+1; while(!pressed()) delay(5); //wait for press
clock.setMinute(minute == 60 ? 0 : minute);
} //setup time
writeTime(); char mode = 0;//mode = 0-> set hour, mode = 1 -> set min, mode = 2 -> exit setup
} bool pres = true;//was button pressed
} else { unsigned long timeout = millis();
pres = false; writeTime();
} while(mode < 2) {
if(millis() - timeout > 10000) {//timed out if(pressed()) { //button is pressed
//blink 6 times if(!pres) { //button was not pressed before
for(int i = 0; i < 6; i++) { timeout = millis(); //reset timeout
write(0,0); pres = true;
delay(300); if(mode == 0) {//count hour
write(); byte hour = clock.getHour(houtremp, houtremp)+1;
delay(300); clock.setHour(hour == 13 ? 1 : hour);
} } else {//count min
writeTime(); byte minute = clock.getMinute()+1;
++mode;//next mode clock.setMinute(minute == 60 ? 0 : minute);
//reset timeout }
timeout = millis(); writeTime();
} }
delay(50); } else {
} pres = false;
}
if(millis() - timeout > 10000) { //timed out (button not pressed for 10s)
blink();
writeTime();
++mode;//next mode
//reset timeout
timeout = millis();
}
delay(50);
}
} }
void loop() { void loop() {
//update min //update min
byte hour = clock.getHour(houtremp, houtremp); byte hour = clock.getHour(houtremp, houtremp);
if(hour > 12) if(hour > 12)
hour -=12; hour -=12;
clock.setHour(hour); clock.setHour(hour);
write(clock.getMinute(), hour); write(clock.getMinute(), hour);
//let the led blink //let the led blink
digitalWrite(13, millis()/1000 % 2 == 0); digitalWrite(13, millis()/1000 % 2 == 0);
delay(500); delay(500);
//update lighting values
#if lightsens
unsigned short brightness = analogRead(lightsensorpin);//messure
messure_add -= messure[messure_pos]; //remove old value from avg if(pressed()) {
messure[messure_pos] = brightness; //add to array setTime();
messure_add += brightness; //add to avg }
if((++messure_pos) == messure_size)//increment pos, check range //update lighting values
messure_pos = 0; //reset #if lightsens
unsigned short brightness = analogRead(lightsensorpin);//messure
unsigned short bavg = messure_add / messure_size;
messure_add -= messure[messure_pos]; //remove old value from avg
if(bavg < lighton) { messure[messure_pos] = brightness; //add to array
brightmode = true; messure_add += brightness; //add to avg
} else if(bavg > lightoff) {
brightmode = false; if((++messure_pos) == messure_size)//increment pos, check range
} messure_pos = 0; //reset
#endif
unsigned short bavg = messure_add / messure_size;
if(bavg < lighton) {
brightmode = true;
} else if(bavg > lightoff) {
brightmode = false;
}
#endif
} }