#include <DHT.h>
// Define pin numbers for SAMD21
#define PUMP_PIN 8
#define PUMP1_PIN 9
#define PUMP2_PIN 10
#define PUMP3_PIN 11
#define PUMP4_PIN 12
#define PUMP5_PIN 13
#define LED_PIN 3
#define WATER_LEVEL_PIN 7
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
int soil = 0;
int soil1 = 0;
int soil2 = 0;
int soil3 = 0;
int soil4 = 0;
int soil5 = 0;
float moist = 30;
float moist1 = 30;
float moist2 = 30;
float moist3 = 30;
float moist4 = 30;
float moist5 = 30;
float hum;
float temp;
void setup() {
// Initialize serial communication
Serial.begin(9600);
dht.begin();
// Set pin modes for pumps and LED using registers
PORT->Group[PORTA].DIRSET.reg = (1 << PUMP_PIN) | (1 << PUMP1_PIN) | (1 << PUMP2_PIN) |
(1 << PUMP3_PIN) | (1 << PUMP4_PIN) | (1 << PUMP5_PIN) |
(1 << LED_PIN);
PORT->Group[PORTA].DIRCLR.reg = (1 << WATER_LEVEL_PIN);
}
void loop() {
// Turn on the LED
PORT->Group[PORTA].OUTSET.reg = (1 << LED_PIN);
delay(1000);
// Read water level
int waterLevel = PORT->Group[PORTA].IN.reg & (1 << WATER_LEVEL_PIN);
// Read and constrain analog values
int sensorValue = readADC(A0);
sensorValue = constrain(sensorValue, 225, 1023);
int sensorValue1 = readADC(A1);
sensorValue1 = constrain(sensorValue1, 225, 1023);
int sensorValue2 = readADC(A2);
sensorValue2 = constrain(sensorValue2, 225, 1023);
int sensorValue3 = readADC(A3);
sensorValue3 = constrain(sensorValue3, 225, 1023);
int sensorValue4 = readADC(A4);
sensorValue4 = constrain(sensorValue4, 225, 1023);
int sensorValue5 = readADC(A5);
sensorValue5 = constrain(sensorValue5, 225, 1023);
// Print sensor values and calculate soil moisture percentages
Serial.println(sensorValue);
soil = map(sensorValue, 225, 1023, 100, 0);
Serial.println("_________________________");
Serial.println("Strawberry soil moisture:");
Serial.print(soil);
Serial.println("%");
Serial.println(sensorValue1);
soil1 = map(sensorValue1, 225, 1023, 100, 0);
Serial.println("_________________________");
Serial.println("Peas 1 soil moisture:");
Serial.print(soil1);
Serial.println("%");
Serial.println(sensorValue2);
soil2 = map(sensorValue2, 225, 1023, 100, 0);
Serial.println("_________________________");
Serial.println("Peas 2 soil moisture:");
Serial.print(soil2);
Serial.println("%");
Serial.println(sensorValue3);
soil3 = map(sensorValue3, 225, 1023, 100, 0);
Serial.println("_________________________");
Serial.println("Peas 3 soil moisture:");
Serial.print(soil3);
Serial.println("%");
Serial.println(sensorValue4);
soil4 = map(sensorValue4, 225, 1023, 100, 0);
Serial.println("_________________________");
Serial.println("Soil 4");
Serial.print(soil4);
Serial.println("%");
Serial.println(sensorValue5);
soil5 = map(sensorValue5, 225, 1023, 100, 0);
Serial.println("_________________________");
Serial.println("Soil 5");
Serial.print(soil5);
Serial.println("%");
// Control pumps based on soil moisture and water level
controlPump(PUMP_PIN, soil, moist, waterLevel);
controlPump(PUMP1_PIN, soil1, moist1, waterLevel);
controlPump(PUMP2_PIN, soil2, moist2, waterLevel);
controlPump(PUMP3_PIN, soil3, moist3, waterLevel);
controlPump(PUMP4_PIN, soil4, moist4, waterLevel);
controlPump(PUMP5_PIN, soil5, moist5, waterLevel);
// Read DHT sensor data
hum = dht.readHumidity();
temp = dht.readTemperature();
Serial.println("_________________________");
Serial.print("Outside g");
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" C");
// Check water level and print messages
if (waterLevel == 0) {
Serial.println("__________________________");
Serial.println("No water for plants left");
Serial.println("Please add water");
}
// Check pump statuses and print messages
checkPumpStatuses();
// Handle incoming serial commands
handleSerialCommands();
}
void controlPump(int pumpPin, int soilMoisture, float desiredMoisture, int waterLevel) {
if ((soilMoisture < desiredMoisture) && (waterLevel != 0)) {
PORT->Group[PORTA].OUTSET.reg = (1 << pumpPin);
delay(30000);
PORT->Group[PORTA].OUTCLR.reg = (1 << pumpPin);
} else {
PORT->Group[PORTA].OUTCLR.reg = (1 << pumpPin);
}
}
void checkPumpStatuses() {
if (PORT->Group[PORTA].IN.reg & ((1 << PUMP_PIN) | (1 << PUMP1_PIN) | (1 << PUMP2_PIN) |
(1 << PUMP3_PIN) | (1 << PUMP4_PIN) | (1 << PUMP5_PIN))) {
Serial.println("One or more pumps are running");
} else {
Serial.println("None of the pumps are running");
Serial.println("Plants are watered and happy");
PORT->Group[PORTA].OUTCLR.reg = (1 << LED_PIN);
delay(60000);
}
}
void handleSerialCommands() {
if (Serial.available() > 0) {
String command = Serial.readStringUntil(' ');
int value = Serial.parseInt();
if (command == "Setmoist") {
moist = value;
} else if (command == "Setmoist1") {
moist1 = value;
} else if (command == "Setmoist2") {
moist2 = value;
} else if (command == "Setmoist3") {
moist3 = value;
} else if (command == "Setmoist4") {
moist4 = value;
} else if (command == "Setmoist5") {
moist5 = value;
} else if (command == "Setwaterlevel") {
waterLevel = value;
}
}
}
int readADC(uint8_t pin) {
// Configure the pin as an ADC input
PORT->Group[g_APinDescription[pin].ulPort].PINCFG[g_APinDescription[pin].ulPin].bit.PMUXEN = 1;
PORT->Group[g_APinDescription[pin].ulPort].PMUX[g_APinDescription[pin].ulPin >> 1].reg |= PORT_PMUX_PMUXE_B;
// Select the input pin for the ADC
ADC->INPUTCTRL.bit.MUXPOS = g_APinDescription[pin].ulADCChannelNumber;
while (ADC->STATUS.bit.SYNCBUSY);
// Start the ADC conversion
ADC->SWTRIG.bit.START = 1;
while (ADC->INTFLAG.bit.RESRDY == 0);
// Read the ADC result
int result = ADC->RESULT.reg;
return result;
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6