Skip to content

Commit

Permalink
Add food dispensing system.
Browse files Browse the repository at this point in the history
Signed-off-by: Szymon Duchniewicz <[email protected]>
  • Loading branch information
Willmish committed Mar 28, 2022
1 parent 2c68994 commit 1ad7c5a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include <ArduinoJson.h>
#include <PubSubClient.h>

// sensors and actuators
#include <Arduino_MKRENV.h>
#include <Servo.h>


// 0: no log messages, 1: safe log messages, 2: halting log messages
Expand All @@ -18,12 +20,19 @@
#define PUMP_ON_TIME 5000L
// MAX Time in MS the heater is on after receiving MQTT msg (if not turned off by user before)
#define HEATER_TIMEOUT 30*1000L
// Time for food dispensing in MS
#define FOOD_DISPENSING_TIMEOUT 5000L
// Interval of the main HW Clock in MS
#define HW_TIMER_INTERVAL_MS 50L
#define WATER_LEVEL_SENSOR_READINGS 10
#define READING_INTERVAL 2

#define WATER_MAX_LEVEL 680
#define WATER_MIN_LEVEL 400
#define WATER_SENSOR_MAX_VAL 800
// Servo positions
#define SERVO_CLOSED_POS 0
#define SERVO_OPEN_POS 180

const int JSON_SIZE = 1000;

Expand All @@ -38,6 +47,8 @@ int getWaterLevelReading();
bool isWaterContainerFull();
bool isWaterContainerEmpty();

Servo food_dispenser_servo;

// Values modifed by Interrupts
volatile int heaterTimerId = -1; // Stores the id of the current heaterTimer interrupt
volatile bool heater_status = false;
Expand Down
15 changes: 11 additions & 4 deletions src/src.ino
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ int getWaterLevelReading()
delay(READING_INTERVAL);
}
return avg/WATER_LEVEL_SENSOR_READINGS;

}


Expand Down Expand Up @@ -93,7 +94,7 @@ void foodDispenserTimerHandler()
#if (DEBUG > 1)
Serial.println("Stopping food dispenser!");
#endif

food_dispenser_servo.write(SERVO_CLOSED_POS);
}

void callback(char* topic, byte* payload, unsigned int length) {
Expand Down Expand Up @@ -192,7 +193,11 @@ void callback(char* topic, byte* payload, unsigned int length) {
{
if (strcmp((const char*) callbackDoc["dispense_food"], "dispense_food") == 0)
{
Serial.println("Dispensing foood!!!!");
#if (DEBUG > 1)
Serial.println("Dispensing food!!!!");
#endif
food_dispenser_servo.write(SERVO_OPEN_POS);
ISR_Timer.setTimeout(FOOD_DISPENSING_TIMEOUT, foodDispenserTimerHandler);
}
}
}
Expand Down Expand Up @@ -225,7 +230,8 @@ void setup() {
pinMode(WATER_PUMP_PIN,OUTPUT);
pinMode(HEATER_PIN,OUTPUT);
pinMode(BUZZER_PIN,OUTPUT);
pinMode(MOTOR_PIN,OUTPUT);
food_dispenser_servo.attach(MOTOR_PIN);
food_dispenser_servo.write(SERVO_CLOSED_POS); // reset servo position

initialiseSerial();
checkENVShieldInitialisation();
Expand All @@ -247,7 +253,8 @@ void loop() {
float humi = ENV.readHumidity();
float illu = ENV.readIlluminance();
int sound = analogRead(MIC_PIN);
float water = getWaterLevelReading();
// get avg and map to 0-100 %
float water = map(getWaterLevelReading(), 0, WATER_SENSOR_MAX_VAL, 0, 100); //actual max should be close to 1023

// Check if any reads failed
if (isnan(temp) || isnan(humi) || isnan(illu)) {
Expand Down

0 comments on commit 1ad7c5a

Please sign in to comment.