Ok, I decided for my needs all I really need to do is be able to turn power on and off so I have written code to utilize the digital pins as a power source, and continue with the analog read. This should extend the life of my sensors since I only need to get a reading about 4 times a day.
Would anyone mind looking over this code and make sure it is actually limiting electrolysis as I intend? I am still learning here. Thanks for all the help so far!
The way I have it set up at the moment is to have the power(vcc) come from the digital pin and then tie the ground to the common ground so as to not tie up more digital pins. Will this work as intended or do I need to also have a dedicated digital pin written low for each sensor?
#include <SPI.h>
#include <MySensor.h>
#define ANALOG_INPUT_SOIL_SENSOR1 A0
#define CHILD_ID1 0 // Id of the sensor child
#define ANALOG_INPUT_SOIL_SENSOR2 A1
#define CHILD_ID2 1 // Id of the sensor child
long previousMillis = 0;
long interval = (10000); //delay betweein readings - 1000 is 1 second
MySensor gw;
MyMessage msg1(CHILD_ID1, V_HUM),msg2(CHILD_ID2, V_HUM);
int soilPower1 = 1; //digital pin to get power from
int soilPower2 = 2; //digital pin to get power from
void setup()
{
gw.begin();
gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0"); // Send the sketch version information to the gateway and Controller
pinMode(ANALOG_INPUT_SOIL_SENSOR1, INPUT);// sets the soil sensor analog pin as input
pinMode(ANALOG_INPUT_SOIL_SENSOR2, INPUT);
gw.present(CHILD_ID1, S_HUM);// Register all sensors to gw (they will be created as child devices)
gw.present(CHILD_ID2, S_HUM);
pinMode(soilPower1,OUTPUT);
pinMode(soilPower2,OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
digitalWrite(soilPower1,LOW);
digitalWrite(soilPower2,LOW);
if (currentMillis - previousMillis + 500 > interval) {
digitalWrite(soilPower1,HIGH);
digitalWrite(soilPower2,HIGH);
}
if (currentMillis - previousMillis > interval) {
int soilValue1 = analogRead(ANALOG_INPUT_SOIL_SENSOR1);// Read analog soil value
int soilValue2 = analogRead(ANALOG_INPUT_SOIL_SENSOR2);
previousMillis = currentMillis;
Serial.println(soilValue1);
Serial.println(soilValue2);
gw.send(msg1.set(constrain(map(soilValue1, 1023, 250, 0, 100), 0, 100)));
gw.send(msg2.set(constrain(map(soilValue2, 1023, 250, 0, 100), 0, 100)));
digitalWrite(soilPower1,LOW);
digitalWrite(soilPower2,LOW);
}
}