Hey guys,
I am learning to make libraries and pointers and all of that, the code is still relatively simple. it is purely for learning purposes and almost no other function... other than keeping two of my house plants alive. so I cannot figure out the problem other than the code hangs in pump.cpp (listed last) in the method runThePump() during turning the pump on and off. it serial prints "turning pump on" and hangs, what seems like right away. anyway to keep things short. here is the code. any help is greatly appreciated as my two house plants won't live long by my hand alone lol. the comments in the main code are excellent, but I am still working on the library itself. please help as I have no clue as why this is an issue
short version: code hangs during the first run of the pump in the first instance of the class. the pump runs in cycles to keep it from getting to hot. I had this running originally without a library so I know it is not hardware.
Main Code:
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <pump.h>
#include <moisture.h>
#define OLED 9
#define OLED_RESET 10
Adafruit_SSD1306 display(OLED_RESET);
int pumpP1 = 2, // Pump 1
pumpP2 = 3, // Pump 2
soilPower = 4, //Power for soil moisture sensor Pin 4
soilPower2 = 5, //Power for soil moisture sensor 2 Pin 5
ledB = 6, //blue
ledG = 7, //green
soilSignal = 0, //Analog soil moisture sensor pin
soilSignal2 = 1; //Analog soil moisture sensor pin
int val = 0,//Declare values to hold soil moisture readings
val2 = 0, //Declare values to hold soil moisture readings
moistureLevel = 40; //what percent to water the plant
unsigned long pMills = 0;
long pumpOn = 300000, //millseconds of the interval to run the pump(sec * 1000);
pumpOff = 60000, //millseconds of the interval to pause the pump(sec * 1000);
pumpWateredTimer = 86400000, //milliseconds, how often to
pumpDryTimer = 32400000,
soilTimer = 0;
//tes
pump pump1(pumpP1); //Pump pin for pump 2
moisture m1(soilSignal, soilPower); //(signal, power)
//Liz
pump pump2(pumpP2); //Pump pin for pump 2
moisture m2(soilSignal2, soilPower2); //(signal, power)
void setup() {
pinMode(OLED, OUTPUT);
digitalWrite(OLED, HIGH);
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
//Tes
pump1.setPumpData(6, moistureLevel, ledB); //(cycle, mThresh, led);
pump1.setPumpTimers(pumpWateredTimer, pumpDryTimer, pumpOn, pumpOff);//(delayAfterWatered, delayAfterDryed, pumpOnTime, pumpOffTime)
//Liz
pump2.setPumpData(8, moistureLevel, ledG); //(cycle, mThresh, led)
pump2.setPumpTimers(pumpWateredTimer, pumpDryTimer, pumpOn, pumpOff); //(delayAfterWatered, delayAfterDryed, pumpOnTime, pumpOffTime)
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - pMills >= soilTimer) {
pMills = currentMillis;
val = m1.moistureTest(); // test moisture for pump1
val2 = m2.moistureTest(); // test moisture for pump2
dispMoistureOLED(val, val2); //display moisture for plant1 and plant2
soilTimer = 7200000; //don't check moisture again until this time
}
/* Serial.println(pump1.getMoistureValue());
Serial.println(pump2.getMoistureValue()); */ //not necassary with oled
pump1.setMoisture(val); // set moisture value for pump1
pump1.runPump(); //now see if the pump needs to run
pump2.setMoisture(val2); //set moisture for pump2
pump2.runPump();//now see if the pump needs to run
pump1.pumpFailSafe(); //lets not have any mistakes
pump2.pumpFailSafe();
}
void dispMoistureOLED(int &moisture, int &moisture2) //display that sh**
{ //Display Moisture percentages OLED
display.clearDisplay();
display.drawBitmap(0, 15, plantBMP, 64, 64, 1); //deleted the photo to meet character limit
display.display();
delay(500);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 1);
display.println("Moisture -");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(44, 25);
display.print("Tes ");
display.setCursor(85, 25);
display.println(moisture + String("%"));
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(44, 50);
display.print("Liz ");
display.setCursor(85, 50);
display.println(moisture2 + String("%"));
display.display();
}
library in comments
pump.cpp (3.52 KB)
pump.h (1.2 KB)