I am using an Arduino Mega with capacitive moisture sensor/ oled/keypad/peristaltic_water_pump/growth light. I am looking for the oled to display moisture level and also ask what moisture level to set. I'd like to be bale to use the keypad to input value for set moisture level. Then I want the pump to turn on if moisture level read by sensor is lower than the set moisture level. Independently of this I am looking to have growth light on for 14 hours and off for 10 hours. I am really new at this coding and I have tried some codes. they work separately but not together. Can someone help me?
Probably because you use delay() for timing (I'm guessing cause you don't post the secret code) and your code blocks. Use millis() for timing instead.
Non-blocking timing tutorials:
Blink without delay().
Blink without delay detailed explanation
Beginner's guide to millis().
Several things at a time.
Please read the how to get the most from the forum post. The post contains information on what we need to know in order to help you and how to post code properly.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in a code block.
@groundFungus I believe I did use delay() - I will look again once I have the computer (where "my secret code" is) and post it here so I can get better input. Thanks for the info I will look into them!
@groundFungus this is the code I used - I removed some delays. It seems to be working but sensor reading is not steady.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
#define SCREEN_WIDTH 128 // LCD display width, in pixels
#define SCREEN_HEIGHT 64 // LCD display height, in pixels
#define OLED_RESET -1 // Reset
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int AirValue = 400; //analog value when sensor is dry
const int WaterValue = 200; //analog value when sensor is wet
const int SensorPin = A0; //analog pin#
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
byte pump1 = 11;
int count = 0;
int growLight = 45;
const byte ROWS = 4; //number of rows on keypad
const byte COLS = 4; //number of columns on keypad
int percent = 45; //default humidity level value
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
//Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
display.clearDisplay();
pinMode(pump1, OUTPUT);
digitalWrite(pump1, HIGH);
pinMode(growLight, OUTPUT);
}
//start loop for pump when soil level below set value turn pump on or off
void loop()
{
soilMoistureValue = analogRead(SensorPin); //put Sensor insert into soil
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
if (soilmoisturepercent < 0)
soilmoisturepercent = 0;
if (soilmoisturepercent > 100)
soilmoisturepercent = 100;
display.setCursor(45, 0); //lcd display
display.setTextSize(2);
display.setTextColor(WHITE);
display.println("Soil");
display.setCursor(20, 15);
display.setTextSize(2);
display.setTextColor(WHITE);
display.println("Moisture");
display.setCursor(30, 40); //lcd display
display.setTextSize(3);
display.setTextColor(WHITE);
display.println(soilmoisturepercent);
display.setCursor(70, 40);
display.setTextSize(3);
display.println(" %");
display.display();
delay(250);
display.clearDisplay();
if (soilmoisturepercent < percent) { //if sensor reads lower then set value
digitalWrite(pump1, LOW); //pump on
delay (100); //delay
}
else {
digitalWrite(pump1, HIGH); //pump off
}
//keypad menu if select ‘1’ soil level is 20%, if select ‘2’ soil level is 40% if select ‘3’ soil level is 60%
char customKey = customKeypad.getKey();
if (customKey) {
Serial.println(customKey);
}
switch (customKey)
{
case '1':
percent = 20;
break;
case '2':
percent = 40;
break;
case '3':
percent = 60;
break;
case '*':
percent = 45;
break;
}
//light circuit turn light on for 14 seconds and off for 10 seconds
if (count < 14)
digitalWrite(growLight, LOW); // turn the LED ON
else
{
digitalWrite(growLight, HIGH); // turn the LED off
if (count > 24)
count = 0;
}
delay (1000); // wait for 1 seconds
count++;
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.