As you will see in my code info block, I am trying to get an alarm to trigger and flash some LED's. After some (read many) tries, I have gotten this far, but two things allude me to date. How to reset the alarm timer when the sensor switch triggers, and how to merge in a flash toggle without creating a whole new variable set and timer. I think I basically need to use a "tick", but don't quite get the testing concept of that.
If anyone has any improvement ideas in these veins, or basically just want to tell me a "better" way. Please check out what I have so far and let me know what you think.
This is currently running OK on an Uno clone, and is a small section of a modification to a automated chicken coop sketch written by Roger Reed based on Time Alarms. I have many more changes to make, though some are done and successful, but space is beginning to get tight, and I haven't even started motor control yet! It looks like 2 things will happen...the full project will ultimately evolve to a 256Mega, and I will learn about programming these things to make it happen as I wish it to happen!
The immediate issues are:
-code is 1572 bytes no debug, would like it smaller
-reset of the alarm delay must be moved to the time the switch is released, as opposed to when it is pressed
-the delays must be eliminated, and I can do this in the same way I did the other 2, but maybe there is a better way? (THE MAIN QUESTION
/*8/24/13 -fab
* Object is to write code that heart beats the P-13 LED and watches the
* egg box lid switch. If the switch is triggered (P-7 HIGH), the (P-3)
* egg box light goes on. If the egg box is left open, (testing 15 sec),
* the egg box lights flash until egg box lid is closed.
*
* Current issues:
* -seems bloated for simple timing.
*
* -after egg box lid is closed, timing may be anywhere in the timing
* cycle when the lid is opened again. Timing reset must be moved to lid
* opening time. You may not get the full time on reopening before
* the flashing begins.
*
* -delays need to be eliminated, still allowing the flashing to be
* apparent. The full code requires no delays.
* -Currently too big for simple timing. Would lie to use Time Alarms
* for one time triggers. No success thus far.(used in main project)
*/
long previousMillis = 0;
long previousMillis2 = 0;
const int flashInterval = 1000; //sets heart beat speed
const int flashEggBoxTimeOut = 15000; //sets time to begin flashing
byte eggLightAlarm=0; //var to declare time to flash
//has expired.(Lid open too long)
//PINS
const byte EGG_BOX_LIGHT_PIN = 3,
HEART_BEAT_PIN = 13,
EGG_BOX_SWITCH_PIN = 7;
void setup(){
pinMode(EGG_BOX_LIGHT_PIN, OUTPUT);
pinMode(HEART_BEAT_PIN, OUTPUT);
// Serial.begin(57600); //just for debug
}
void loop(){
// Serial.println(eggLightAlarm);
if(iseggBoxOpen()){
eggLightOn();
unsigned long currentMillis2 = millis();
if (currentMillis2 - previousMillis2 > flashEggBoxTimeOut){
previousMillis2 = currentMillis2;
eggLightAlarm = 1;
}
}
else{
eggLightOff(); //turns off egg box light and resets
} //timer...move reset to lid opening.
unsigned long currentMillis = millis(); //
if (currentMillis - previousMillis > flashInterval){ //
previousMillis = currentMillis; //sets heart beat
heartBeat(); //toggles LED p-13
if(eggLightAlarm == 1){ //detects lid open too long
flashEggBoxLight(); //toggles egg box light
}
}
}
//************
//end of loop
//************
//**************
//****************
//functions begin
//****************
boolean isheartOn(){
return digitalRead(HEART_BEAT_PIN) == HIGH;
}
void heartBeat(){
if(isheartOn()){
digitalWrite(HEART_BEAT_PIN, LOW); //p-13 led off
}
else{
digitalWrite(HEART_BEAT_PIN, HIGH); //p-13 led on
}
}
boolean iseggBoxOpen(){
// Serial.println("EBO?");
return digitalRead(EGG_BOX_SWITCH_PIN) == HIGH; //lid open = true
}
void flashEggBoxLight(){
if(digitalRead(EGG_BOX_LIGHT_PIN == HIGH)){ //is p-7 high(eblight on)?
// Serial.println("fEBL1"); // off if they're on
digitalWrite(EGG_BOX_LIGHT_PIN, LOW); //p-7 low, eblight off
delay(200); //MUST eliminate efficiently
}
else{
// Serial.println("fEBL2");
eggLightOn(); //turn 'em on if they're off
}
}
void eggLightOn(){
digitalWrite(EGG_BOX_LIGHT_PIN, HIGH); //turn 'em on
// Serial.println("eLOn");
delay(50); //just to slow things down a bit
}
void eggLightOff(){
digitalWrite(EGG_BOX_LIGHT_PIN, LOW); //turn 'em off
// Serial.println("eLOff");
eggLightAlarm = 0;
delay(50); //just to slow things down a bit
}