123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- #define SERIALBAUD 57600
- #define SERIALCONF SERIAL_8N1
- #define PROJECT_NAME "DualS0ImpCounter"
- #define PROJECT_VERSION "1.0"
- #include <avr/wdt.h>
- #include <EEPROM.h>
- #define PIN_OUT_HARDRESET 6
- #define PIN_OUT_LED 13
- #define PIN_IN_PULSE1 2
- #define PIN_IN_PULSE2 3
- #define COUNTERS_COUNT 2
- #define PIN_IN_ADC_PWRGOOD A3
- unsigned int powerGoodMinValue = 500;
- int debounceDelay = 40;
- int debounceRecoveryDelay = 70;
- bool debug = true;
- int debuglevel = 1;
- bool output_json = true;
- unsigned long currentReading[COUNTERS_COUNT];
- uint16_t currentReadingImpulses[COUNTERS_COUNT];
- uint16_t currentReadingImpulses_saved[COUNTERS_COUNT];
- byte pulseInPins[] = {PIN_IN_PULSE1, PIN_IN_PULSE2};
- byte pulseInLastState[2];
- unsigned long pulseInLastDebounceTime[] = {0, 0};
- bool pulseAlreadyDetected[COUNTERS_COUNT];
- unsigned long pulseInLastMillis[COUNTERS_COUNT];
- uint16_t meter_impPerUnit[] = {1000, 100};
- uint16_t meter_noImpulseTimeout_seconds[] = {0, 300};
- uint16_t meter_savePulsesOnInterval_mins[] = {15, 0};
- unsigned long pulsesLastSaved_seconds[COUNTERS_COUNT];
- unsigned long lastDataSent_seconds[COUNTERS_COUNT];
- unsigned long lastSaveTime_pulses = 0;
- unsigned long millis_everysecond = 0;
- unsigned long seconds=0;
- #define MAX_CMD_LEN 120
- #define MAX_CMDDATA_LEN 90
- char cmdBuffer[MAX_CMD_LEN + 1];
- int cmdBufferCount = 0;
- bool cmdComplete = false;
- char cmdDataBuffer[MAX_CMDDATA_LEN + 1];
- uint16_t eeprom_addr_debug = 2;
- uint16_t eeprom_addr_output_json = 3;
- uint16_t eeprom_addr_debounce1 = 4;
- uint16_t eeprom_addr_debounce2 = 6;
- uint16_t eeprom_addr_powerGoodMinValue = 8;
- uint16_t eeprom_addr_impPerUnit[] = {10, 12};
- uint16_t eeprom_addr_noImpTout[] = {14, 16};
- uint16_t eeprom_addr_saveInt[] = {18, 20};
- uint16_t eeprom_addr_currentReading[2] = {32, 44};
- uint16_t eeprom_addr_currentReading_backup1[2] = {36, 48};
- uint16_t eeprom_addr_currentReading_backup2[2] = {40, 52};
- static byte eeprom_datasetsize_currentReadingImpulses = 4;
- uint16_t eeprom_addr_area_start_currentReadingImpulses[2] = {128, 640};
- uint16_t eeprom_addr_area_length_currentReadingImpulses[2] = {512, 128};
- uint16_t eeprom_writecounter_currentReadingImpulses[COUNTERS_COUNT];
- uint16_t eeprom_nextoffset_currentReadingImpulses[COUNTERS_COUNT];
- void setup() {
-
- wdt_disable();
-
- for (int i = 0; i < COUNTERS_COUNT; i++) {
- eeprom_writecounter_currentReadingImpulses[i] = 0;
- eeprom_nextoffset_currentReadingImpulses[i] = 0;
- currentReading[i] = 0;
- pulseInLastMillis[i] = 0;
- pulseAlreadyDetected[i] = false;
- }
-
-
-
-
- pinMode(PIN_OUT_HARDRESET, INPUT);
- pinMode(PIN_IN_PULSE1, INPUT_PULLUP);
- pinMode(PIN_IN_PULSE2, INPUT_PULLUP);
-
-
-
-
- delay(2L * 1000L);
-
-
-
-
-
-
-
-
- wdt_enable(WDTO_8S);
- Serial.begin(SERIALBAUD, SERIALCONF);
- Serial.print(PROJECT_NAME);
- Serial.print(" v");
- Serial.print(PROJECT_VERSION);
- Serial.print(F(" starting..."));
-
- loadConfig();
-
- restoreAllLastData();
-
- printAllCurrentReadings();
-
- }
- void loop() {
- if ((millis() - millis_everysecond) >= 1000) {
- millis_everysecond = millis();
- everySecond();
- }
- checkInputStates();
- checkPowerGood();
- wdt_reset();
- }
- void everySecond() {
- onNoImpulseTimeout();
- saveDataOnInterval();
- seconds++;
- }
|