diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..25ba931 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,41 @@ +PROJECT = plant-manager + +# Flags +MCU = atmega328p +F_CPU = 16000000UL + +CC = avr-gcc +OBJCOPY = avr-objcopy +AVRDUDE = avrdude +CFLAGS = -Wall -Os -DF_CPU=$(F_CPU) -mmcu=$(MCU) + +PROGRAMMER = arduino +PORT = /dev/ttyACM0 + +SRC = main.c +OBJ = $(SRC:.c=.o) + +# Default target +all: $(PROJECT).hex + +# Compiler +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +# Linker +$(PROJECT).elf: $(OBJ) + $(CC) $(CFLAGS) -o $@ $^ + +# Create hex file +$(PROJECT).hex: $(PROJECT).elf + $(OBJCOPY) -O ihex -R .eeprom $< $@ + +# Uploader +upload: $(PROJECT).hex + $(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -P $(PORT) -U flash:w:$<:i + +# Cleaner +clean: + rm -f *.o *.elf *.hex + +.PHONY: all upload clean diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..d8583ac --- /dev/null +++ b/src/config.h @@ -0,0 +1,7 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define MOISTURE_SENSOR_A0 A0 +#define SENSOR_READINGS 3 + +#endif diff --git a/src/main.c b/src/main.c index f5e9590..b15b133 100644 --- a/src/main.c +++ b/src/main.c @@ -1,19 +1,14 @@ #include #include #include - -#define MOISTURE_SENSOR_A0 A0 - -volatile bool readSensors = false; -int16_t moistureReadingSum = 0; -int8_t moistureReadingCount = 0; -const uint8_t SENSOR_READINGS = 3; +#include "config.h" +#include "moisture.h" ISR(WDT_vect) { readSensors = true; } -void watchdogs() { +void watchdogs(void) { cli(); MCUSR &= ~(1 << WDRF); WDTCSR |= (1 << WDCE) | (1 << WDE); @@ -21,38 +16,27 @@ void watchdogs() { sei(); } -int16_t readMoisture() { - if (readSensors) { - moistureReadingSum += analogRead(MOISTURE_SENSOR_A0); - moistureReadingCount++; - - if (moistureReadingCount >= SENSOR_READINGS) { - Serial.println(moistureReadingSum / SENSOR_READINGS); - moistureReadingSum = 0; - moistureReadingCount = 0; - } else readSensors = true; - } - readSensors = false; -} - -void enterSleep() { - // Arduino disables USB when sleeping - // if using Serial, use flag : SLEEP_MODE_IDLE - // otherwise for deep sleep : SLEEP_MODE_PWR_DOWN +void enterSleep(void) { set_sleep_mode(SLEEP_MODE_IDLE); - sleep_enable(); sleep_cpu(); sleep_disable(); } -void setup() { - pinMode(MOISTURE_SENSOR_A0, INPUT); +void setup(void) { Serial.begin(9600); + initMoistureSensor(); watchdogs(); } -void loop() { - int16_t moisture = readMoisture(); +void loop(void) { + if (readSensors) { + int16_t result = readMoisture(); + if (result != -1) { + Serial.println(result); + readSensors = false; + } + } + enterSleep(); } diff --git a/src/moisture.cpp b/src/moisture.cpp new file mode 100644 index 0000000..8963e61 --- /dev/null +++ b/src/moisture.cpp @@ -0,0 +1,36 @@ +#include +#include +#include "moisture.h" +#include "config.h" + +static int16_t moistureReadingSum = 0; +static int8_t moistureReadingCount = 0; + +volatile bool readSensors = false; + +void initMoistureSensor(void) { + pinMode(MOISTURE_SENSOR_A0, INPUT); +} + +void triggerSensorRead(void) { + readSensors = true; +} + +int16_t readMoisture(void) { + if (!readSensors) + return -1; + + moistureReadingSum += analogRead(MOISTURE_SENSOR_A0); + moistureReadingCount++; + + if (moistureReadingCount >= SENSOR_READINGS) { + int16_t avg = moistureReadingSum / SENSOR_READINGS; + //Serial.println(avg); + moistureReadingSum = 0; + moistureReadingCount = 0; + readSensors = false; + return avg; + } else readSensors = true; + + return -1; +} diff --git a/src/moisture.h b/src/moisture.h new file mode 100644 index 0000000..1f17008 --- /dev/null +++ b/src/moisture.h @@ -0,0 +1,13 @@ +#ifndef MOISTURE_H +#define MOISTURE_H + +#include +#include + +void initMoistureSensor(void); +int16_t readMoisture(void); +void triggerSensorRead(void); + +extern volatile bool readSensors; + +#endif