working sample

This commit is contained in:
andreask 2020-09-06 00:06:11 +02:00
parent 616a3af2c0
commit f5ae34863b

View file

@ -66,7 +66,7 @@
SHT21 sensor; SHT21 sensor;
#endif #endif
#if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM || defined HAS_MANNLCOUNTER #if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM
volatile boolean alarm = false; volatile boolean alarm = false;
// Get the Interrupt Mask for the selected Alarm PIN // Get the Interrupt Mask for the selected Alarm PIN
@ -93,9 +93,47 @@
#endif #endif
#ifdef HAS_MANNLCOUNTER #ifdef HAS_MANNLCOUNTER
volatile uint16_t count_min = 0; #include <util/delay.h>
volatile uint32_t count_all = 0; uint16_t count_min = 0;
volatile unsigned long alteZeit=0, entprellZeit=20; //ms uint32_t count_all = 0;
unsigned long lastTime = millis();
#define debounce( port, pin ) \
({ \
static uint8_t flag = 0; /* new variable on every macro usage */ \
uint8_t i = 0; \
\
if( flag ){ /* check for key release: */ \
for(;;){ /* loop ... */ \
if( !(port & 1<<pin) ){ /* ... until key pressed or ... */ \
i = 0; /* 0 = bounce */ \
break; \
} \
_delay_us( 98 ); /* * 256 = 25ms */ \
if( --i == 0 ){ /* ... until key >25ms released */ \
flag = 0; /* clear press flag */ \
i = 0; /* 0 = key release debounced */ \
break; \
} \
} \
}else{ /* else check for key press: */ \
for(;;){ /* loop ... */ \
if( (port & 1<<pin) ){ /* ... until key released or ... */ \
i = 0; /* 0 = bounce */ \
break; \
} \
_delay_us( 98 ); /* * 256 = 25ms */ \
if( --i == 0 ){ /* ... until key >25ms pressed */ \
flag = 1; /* set press flag */ \
i = 1; /* 1 = key press debounced */ \
break; \
} \
} \
} \
i; /* return value of Macro */ \
})
#endif #endif
// Global Variable to Track Deep Sleep // Global Variable to Track Deep Sleep
@ -140,6 +178,7 @@ int16_t brightness()
} }
#endif #endif
#ifndef HAS_MANNLCOUNTER
// Setup Wakeup Interrupt Timer // Setup Wakeup Interrupt Timer
void init_wdt() void init_wdt()
{ {
@ -179,29 +218,20 @@ ISR(WATCHDOG_vect) {
// enable WDT interrupt // enable WDT interrupt
WDTCSR = (1<<WDIE)|(1<<WDP3)|(1<<WDP0); WDTCSR = (1<<WDIE)|(1<<WDP3)|(1<<WDP0);
} }
#endif
#if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM || defined HAS_MANNLCOUNTER #if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM
// innterrupt handler pin change // innterrupt handler pin change
ISR(PCINT0_vect) ISR(PCINT0_vect)
{ {
#ifdef HAS_MANNLCOUNTER //counter mode
if((millis() - alteZeit) > entprellZeit) {
// innerhalb der entprellZeit nichts machen
count_min++;
count_all++;
alteZeit = millis(); // last button push
}
#else //Alarm mode
sleep_disable(); sleep_disable();
#ifdef HAS_LED #ifdef HAS_LED
blink(1); blink(1);
#endif #endif
alarm = true; alarm = true;
#endif
} }
#endif #endif
// Get Battery Voltage // Get Battery Voltage
int32_t readVcc() { int32_t readVcc() {
bitClear(PRR, PRADC); bitClear(PRR, PRADC);
@ -237,9 +267,11 @@ uint8_t calcEepromAddr(uint16_t framecounter) {
void setup() void setup()
{ {
// Initialize Sleep Timer #ifndef HAS_MANNLCOUNTER
init_wdt(); // Initialize Sleep Timer
PRR = bit(PRTIM1); init_wdt();
PRR = bit(PRTIM1);
#endif
#ifdef RF_LORA #ifdef RF_LORA
// Setup LoraWAN // Setup LoraWAN
@ -281,7 +313,7 @@ void setup()
radio.sleep(); radio.sleep();
#endif #endif
#if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM || defined HAS_MANNLCOUNTER #if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM
MCUCR = (MCUCR & ~(bit(ISC01)|bit(ISC00))) | bit(ISC01); // fallende Flanke MCUCR = (MCUCR & ~(bit(ISC01)|bit(ISC00))) | bit(ISC01); // fallende Flanke
GIMSK = (1<<PCIE0); GIMSK = (1<<PCIE0);
PCMSK0 = (1<<ALARM_INT); PCMSK0 = (1<<ALARM_INT);
@ -388,11 +420,6 @@ void loop()
alarm = false; alarm = false;
#endif #endif
#ifdef HAS_MANNLCOUNTER
data.count_all=count_all;
data.count_min=count_min;
#endif
#if defined DS18B20_PIN #if defined DS18B20_PIN
#ifdef DS18B20_POWER #ifdef DS18B20_POWER
@ -411,6 +438,27 @@ void loop()
#endif #endif
#endif #endif
#ifdef HAS_MANNLCOUNTER
pinMode(ALARM_PIN, INPUT_PULLUP);
// FIXME Timer interrupt
while(millis() - lastTime < 60000){
if( debounce( PINA, PA0 ) ){
count_min++;
count_all++;
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
}
lastTime = millis();
data.count_all=count_all;
data.count_min=count_min;
count_min=0;
// nach 60 sek
#endif
// Add Battery Voltage, 20mv steps, encoded into 1 Byte // Add Battery Voltage, 20mv steps, encoded into 1 Byte
uint32_t batv = readVcc(); uint32_t batv = readVcc();
data.bat = (uint8_t)(batv/20); data.bat = (uint8_t)(batv/20);
@ -436,9 +484,6 @@ void loop()
#else #else
// Send LoRa Packet, Increment Frame Counter // Send LoRa Packet, Increment Frame Counter
lora.Send_Data((unsigned char *)&data, sizeof(data), Frame_Counter_Tx, SF7BW125, 0x01); lora.Send_Data((unsigned char *)&data, sizeof(data), Frame_Counter_Tx, SF7BW125, 0x01);
#ifdef HAS_MANNLCOUNTER
count_min = 0;
#endif
#endif #endif
// Save the next FrameCounter to EEPROM // Save the next FrameCounter to EEPROM
Frame_Counter_Tx++; Frame_Counter_Tx++;
@ -455,6 +500,8 @@ void loop()
digitalWrite(LED_PIN, 0); digitalWrite(LED_PIN, 0);
#endif #endif
#ifndef HAS_MANNLCOUNTER
// Sleep until next Measurement // Sleep until next Measurement
sleep(SLEEP_TIME); sleep(SLEEP_TIME);
#endif
} }