I've seen this Erweiterung für elektronisches Namensschild – AZ-Delivery
Which gave me the idea to make a notifier for my mailbox.
Here is what I want to do:
-
It is supposed to run on a digispark (attiny85). I'll admit beforehand it is a clone, cause the originals were sold out.
-
The box will have two switches.
-
One switch will detect and count how many times the lid was opened.
-
The other when the door was opened.
-
it also will detect a battery low event
-
On every event it then will send the data over a 433mhz to a other digispark
I also want to prevent false positives.
So an activated switch on the flap only should be counted if longer than few seconds. And only if such event didn't happened too fast in succession. Also if the flap detected open for more too long it should be added one to the counter but as well assumed something went wrong. (e.g. flap stuck open, switch fallen off, or similar)
A door open event (the other switch) needs to last at least a few seconds as well.
My program (send side) seems right (but not finished by any means. needs cleanup. has some dead things in it) ... any suggestions on improvements?
Also I have no clue how to use the 433 module ... there might be (in worst case) a ton of other 433 signals. I don't want my receiver picking up the wrong signal. But I also want to go my signal through. Maybe a longer code, and send a few repeats.
Code so far:
/*
Tests two switches.
Accounts for false positives
*/
#include <avr/sleep.h>
#include <virtualwire.h>
//or a other library for transfer?
#define CountButton 2
// Spark 0
#define ResetButton 3
// Spark 1
#define BattTestPin A0
// Spark 3
#define SendPin 5
// Spark 4
/*
interval1 = time count button has to be pressed till counted
interval2 = time the count button is inactive once it was pressed
interval3 = time reset button has to be pressed till counted
interval4 = time the reset button is inactive once it was pressed
interval5 = time count button has to be pressed till is regareded wrong
*/
#define interval1 1000
//#define interval2 15000
#define interval2 3000
//#define interval3 60000
#define interval3 3000
//#define interval4 60000
#define interval4 3000
#define interval5 6000
#define testInterval 3000
//above shortened for debug. fix for final upload
unsigned long previousMillis1 = millis() - interval1; // setup in a way that the first check will be true
unsigned long previousMillis2 = millis() - interval2;
unsigned long previousMillis3 = millis() - interval3;
unsigned long previousMillis4 = millis() - interval4;
unsigned long lastTest = millis() - testInterval;
//only setup what is nessensary.
bool errorFlag = false;
int buttonState1 = LOW;
int lastButtonState1 = LOW;
int buttonState2 = LOW;
int lastButtonState2 = LOW;
int buttonPushCounter = 0;
int buttonPushCounterFalseTrigger = 0; // keep track on how many times it was falsely triggered
float batteryStatus = 0; // set to int
void setup()
{
pinMode(CountButton, INPUT);
pinMode(ResetButton, INPUT);
pinMode(SendPin, OUTPUT);
//pinMode(BattTestPin, INPUT);
Serial.begin(9600); //debug
// send reset signal to receiver at this point
}
void Powerdown() // Toggle Powerdown, if critical Voltage is reached.
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
cli();
sleep_enable();
sei();
sleep_cpu();
//restart
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - lastTest >= (testInterval)) {
batteryStatus = map(analogRead(BattTestPin),655,859,32,42);
batteryStatus = batteryStatus /10; //debug
lastTest = currentMillis;
//Serial.println(batteryStatus); //debug
if ( batteryStatus <= 3.4 ){
// cry for help -- with battery low
Serial.println("Help Battery low"); //debug
Powerdown();
}
}
if ((digitalRead(CountButton) == HIGH) && (lastButtonState1 == LOW)) {
if (currentMillis - previousMillis2 >= (interval2)) {
previousMillis1 = currentMillis;
lastButtonState1 = HIGH;
Serial.println("Button down"); // debug
} else {
previousMillis2 = millis();
buttonPushCounterFalseTrigger ++;
Serial.print("False \t"); //debug
Serial.println(buttonPushCounterFalseTrigger); // debug
delay((interval2-1000));
}
}
if ((digitalRead(CountButton) == LOW) && (lastButtonState1 == HIGH)) {
if (currentMillis - previousMillis1 >= (interval1)) {
if (!errorFlag) {
buttonPushCounter ++;
Serial.println(buttonPushCounter); // debug replace with send code
} else {
errorFlag = false;
}
} else {
buttonPushCounterFalseTrigger ++;
Serial.print("False \t"); //debug
Serial.println(buttonPushCounterFalseTrigger); // debug
delay((interval2-1000));
}
previousMillis2 = currentMillis;
lastButtonState1 = LOW;
}
if ((digitalRead(CountButton) == HIGH) && (lastButtonState1 == HIGH) && (currentMillis - previousMillis1 >= (interval5)) && !(errorFlag)) {
Serial.println("Something wrong"); // debug
buttonPushCounter ++;
Serial.println(buttonPushCounter); // debug replace with send code
errorFlag=true;
}
// reset part of of counter
if ((digitalRead(ResetButton) == HIGH) && (lastButtonState2 == LOW)) {//
// if (currentMillis - previousMillis2 >= (interval2)) {
//previousMillis1 = millis();
lastButtonState2 = HIGH;
Serial.println("Reset down"); // debug
// }
}
if ((digitalRead(ResetButton) == LOW) && (lastButtonState2 == HIGH)) {
if (currentMillis - previousMillis3 >= (interval3)) {
buttonPushCounter = 0;
Serial.println(buttonPushCounter); // debug replace with send code
} else {
//noop;
}
previousMillis3 = millis();
lastButtonState2 = LOW;
}
if (buttonPushCounterFalseTrigger >= 100) {
//cry for help
}
}
Edit: Only after posting the picture I've seen that my comments didn't got included.