Merge pull request 'moisture-library' (#1) from moisture-library into main

Reviewed-on: http://192.168.2.248:3000/erickahmed/plant-manager/pulls/1
This commit is contained in:
2025-05-03 12:16:00 +02:00
5 changed files with 112 additions and 31 deletions
+41
View File
@@ -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
+7
View File
@@ -0,0 +1,7 @@
#ifndef CONFIG_H
#define CONFIG_H
#define MOISTURE_SENSOR_A0 A0
#define SENSOR_READINGS 3
#endif
+15 -31
View File
@@ -1,19 +1,14 @@
#include <stdint.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#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();
}
+36
View File
@@ -0,0 +1,36 @@
#include <Arduino.h>
#include <stdint.h>
#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;
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef MOISTURE_H
#define MOISTURE_H
#include <stdint.h>
#include <stdbool.h>
void initMoistureSensor(void);
int16_t readMoisture(void);
void triggerSensorRead(void);
extern volatile bool readSensors;
#endif