unsigned long pumpOnTime = 15 * 60 * 1000; // Run in 1s
unsigned long pumpOffTime = 15 * 60 * 1000; // Stop in 1s
// PMW pin arduino uno
const byte motorpin = 11;
// testing servo control PH level & EC level
float ph_level = 6; // initial value
float ec_level = 500;
// let say one time pump os servo the incremental of solution in rate
float ph_rate = 0.1;
int ec_rate = 50;
// Servo
#include <Servo.h>
Servo myServo_ph; // create instance of Servo named myServoA
Servo myServo_ecA; // create instance of Servo named myServoB
Servo myServo_ecB; // create instance of Servo named myServoC
int servoSpeed = 120; // 0 = CCW full speed | 90 = zero speed | 180 = CW full speed
unsigned long rotationTime = 1 * 1000; // how long servos run per cycle
const byte servo_ph = 3;
const byte servo_ecA = 5;
const byte servo_ecB = 6;
// OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// 128x64
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
// put your setup code here, to run once:
pinMode(motorpin,OUTPUT);
pinMode(servo_ph,OUTPUT);
pinMode(servo_ecA,OUTPUT);
pinMode(servo_ecB,OUTPUT);
// servo
myServo_ph.attach(servo_ph); // correlate instance of Servo with pin
myServo_ecA.attach(servo_ecA); // correlate instance of Servo with pin
myServo_ecB.attach(servo_ecB); // correlate instance of Servo with pin
Serial.begin(9600);
// OLED display
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(20); // wait for initializing
oled.clearDisplay(); // clear display
}
void loop() {
// put your main code here, to run repeatedly:
// Pumping schedule
digitalWrite(11, HIGH);
delay(500);
digitalWrite(11,LOW);
delay(500);
// Servo
//------------------------------------------------------------
// we prioritize EC, then PH
//-----------------------------------------------------------
//When EC in low, we add stock solution ==> ph decrease
if (ec_level < 1250){
myServo_ecA.write(servoSpeed); // set servo to servoSpeed
myServo_ecB.write(servoSpeed); // set servo to servoSpeed
delay(rotationTime);
myServo_ecA.write(90); // set servo to "off"
myServo_ecB.write(90); // set servo to "off"
delay(5000); //delay for 5 sec while servos are "off"
// update actual value is from sensor
ph_level = ph_level - ph_rate;
ec_level = ec_level + ec_rate;
}
// when EC excesses limit ==> we add water ==> PH increases
else if (ec_level > 1300 ){
// preventing if EC excess limit
myServo_ph.write(servoSpeed); // set servo to servoSpeed
delay(rotationTime);
myServo_ph.write(90); // set servo to "off"
delay(5000); //delay for 5 sec while servos are "off"
// update actual value is from sensor
ph_level = ph_level + ph_rate;
ec_level = ec_level - ec_rate;
}
// Check PH
// when ph low pump more water, but when EC matches the want
if (ec_level >= 1200 && ec_level < 1300 ){
if ( ph_level < 5.5){
myServo_ph.write(servoSpeed); // set servo to servoSpeed
delay(rotationTime);
myServo_ph.write(90); // set servo to "off"
delay(5000); //delay for 5 sec while servos are "off"
// update actual value is from sensor
ph_level = ph_level + ph_rate;
ec_level = ec_level - ec_rate;
}
}
// Display
Serial.println(ec_level);
delay(10);
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setTextColor(WHITE); // text color
// EC
oled.setCursor(0, 0); // position to display
oled.println("EC :"); // text to display
oled.setCursor(40, 0); // position to display
oled.println(ec_level); // text to display
oled.display(); // show on OLED
// PH
oled.setCursor(0, 10); // position to display
oled.println("PH :"); // text to display
oled.setCursor(40, 10); // position to display
oled.println(ph_level); // text to display
oled.display();
}