////////////////////////////////////////////////////////////////////////////////
// Name: JanVoltageFlipStripped.ino //
// Platform: Arduino UNO Rev3 //
// Created by: Jan van Bemmel, April 2018, GPL copyrights //
// http://robotigs.com//robotigs/includes/parts_header.php?idpart=283 //
// An Arduino is used to measure moisture and show it on terminal . //
////////////////////////////////////////////////////////////////////////////////
// SET PRECOMPILER OPTIONS *****************************************************
//Define the needed header files for the precompiler, no charge if not used ----
//Define precompiler variables -------------------------------------------------
#define voltageFlipPin1 7 //Connected to grid probe1? OUTPUT
#define voltageFlipPin2 6 //Connected to grid probe2? OUTPUT
#define sensorPin 1 //Connected to wich device?????????????????????????????????????????????????
//int tempPin = 0; //Pin to measure the temperature, better use #define
//Declare variables ------------------------------------------------------------
const int relaySet = 8; //HIGH=Water on, LOW=water off
word val1a, val2a, val3a; //3 measurments on situation Pin1=HIGH, Pin2=LOW
word val1b, val2b, val3b; //3 measurments on situation Pin1=LOW, Pin2=HIGH
word val1, val2, avg; //Used in moisture calculations
int counter = 0; //Count the number of times we watered the flowers
int flipTimer = 1000; //Time needed to stabilise after voltage flip probes
//END OF PRECOMPILER OPTIONS ---------------------------------------------------
void setup() { //Setup runs once ***********************************************
Serial.begin(9600); //Nothing more needed for the Serial Monitor to function
pinMode(voltageFlipPin1, OUTPUT);
pinMode(voltageFlipPin2, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(relaySet, OUTPUT);
digitalWrite(relaySet, LOW); //Switch the water OFF
}//--(end setup )---------------------------------------------------------------
void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
setSensorPolarity(true); //Pin1=HIGH, Pin2=LOW
delay(flipTimer); //Wait for the capacitiv values set to zero = stabilised
int val1a = analogRead(sensorPin); //Connected to ????????????????????????????????
delay(flipTimer); //Wait for the capacitiv values set to zero = REDUNDANT
setSensorPolarity(false); //Pin1=LOW, Pin2=HIGH
delay(flipTimer); //Wait for the capacitiv values set to zero = stabilised
int val1b = 1023 - analogRead(sensorPin); //Make a reading and invert it
delay(flipTimer); //Wait for the capacitiv values set to zero = REDUNDANT
setSensorPolarity(true); //Pin1=HIGH, Pin2=LOW
delay(flipTimer); //Wait for the capacitiv values set to zero = stabilised
int val2a = analogRead(sensorPin); //Connected to ????????????????????????????
delay(flipTimer); //Wait for the capacitiv values set to zero = REDUNDANT
setSensorPolarity(false); //Pin1=LOW, Pin2=HIGH
delay(flipTimer); //Wait for the capacitiv values set to zero = stabilised
int val2b = 1023 - analogRead(sensorPin); //Make a reading and invert it
delay(flipTimer); //Wait for the capacitiv values set to zero = REDUNDANT
setSensorPolarity(true); //Pin1=HIGH, Pin2=LOW
delay(flipTimer); //Wait for the capacitiv values set to zero = stabilised
int val3a = analogRead(sensorPin); //Connected to ????????????????????????????
delay(flipTimer); //Wait for the capacitiv values set to zero = REDUNDANT
setSensorPolarity(false); //Pin1=LOW, Pin2=HIGH
delay(flipTimer); //Wait for the capacitiv values set to zero = stabilised
int val3b = 1023 - analogRead(sensorPin); //Make a reading and invert it
delay(flipTimer); //Wait for the capacitiv values set to zero = REDUNDANT
reportLevels(); //Calculate and output results
delay(60000); //Wait one minute before we start all over again
} //End of void loop() //KEEP ON RUNNING THIS LOOP FOREVER
void reportLevels() { //Calculate and output results ***************************
val1 = (val1a + val2a + val3a) / 3;
val2 = (val1b + val2b + val3b) / 3;
avg = (val1 + val2) / 2; //avg = avg + avg
delay (500); //Wait half a second REDUNDANT
String msg = "avg: ";
msg += avg;
Serial.print (val1);
Serial.println (val2);
Serial.println(avg);
if (avg > 500) { //If the soil is moist enough then no watering needed
digitalWrite(relaySet, LOW); //Switch the water OFF, REDUNDANT
}else{
delay (500); //Wait half a second REDUNDANT
digitalWrite(relaySet, HIGH); //Switch the water ON
delay (15000); //Flowers will be watered for 15 sec
digitalWrite(relaySet, LOW); //Switch the water OFF
counter = counter + 1; //Count the number of times we watered the flowers
}//End of if the soil is moist enough then no watering needed ----------------
} //Exit calculate and output results ------------------------------------------
void setSensorPolarity(boolean flip) { //Toggles the voltage of the probes *****
if (flip) {
digitalWrite(voltageFlipPin1, HIGH);
digitalWrite(voltageFlipPin2, LOW);
}else{
digitalWrite(voltageFlipPin1, LOW);
digitalWrite(voltageFlipPin2, HIGH);
}
} //Exit setSensorPolarity -----------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS TABLE ARDUINO UNO //
// Board -Atmel- PIN - IDE - Function - Connection ALT //
// //
// CONNECTIONS RAILS TOP LEFT: DIGITAL PWM<~> ******************************* //
// SCL - 28 - PC5 -19/A5- ADC5/SCL/PCINT13 - TWI //
// SDA - 27 - PC4 -18/A4- ADC4/SDA/PCINT12 - TWI //
// AREF - 21 - REF - - AREF - //
// GND - 22 - GND - - GND - //
// 13 - 19 - PB5 - 13 - SCK/PCINT5 - SPI //
// 12 - 18 - PB4 - 12 - MISO/PCINT4 - SPI //
// ~11 - 17 - PB3 - 11 - MOSI/OC2A/PCINT3 - PWM //
// ~10 - 16 - PB2 - 10 - SS/OC1B/PCINT2 - PWM //
// ~9 - 15 - PB1 - 9 - OC1A/PCINT1 - PWM //
// 8 - 14 - PB0 - 8 - PCINT0/CLK0/ICP1 - DIO //
// //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 7 - 13 - PD7 - 7 - PCINT23/AIN1 - VoltageFlipPin1 OUT DIO //
// ~6 - 12 - PD6 - 6 - PCINT22/OCA0/AIN0 - VoltageFlipPin2 OUT PWM //
// ~5 - 11 - PD5 - 5 - PCINT21/OC0B/T1 - PWM //
// ~4 - 6 - PD4 - 4 - PCINT20/XCK/T0 - PWM //
// ~3 - 5 - PD3 - 3 - PCINT19/OC2B/INT1 - INT //
// ~2 - 4 - PD2 - 2 - PCINT18/INT0 - INT //
// TX->1 - 3 - PD1 - 1 - PCINT17/TXD - Serial monitor TXD //
// RX<-0 - 2 - PD0 - 0 - PCINT16/RCD - Serial Monitor RCD //
// //
// CONNECTIONS RAILS BOTTOM LEFT: POWER ************************************* //
// 5V - 7 - VCC - - VCC - VCC //
// RES - 1 - RES - - PCINT14/RESET - RES //
// 3.3V - - - - - //
// 5V - - - - - //
// GND - - - - - //
// GND - - - - - //
// Vin - - - - - //
// //
// CONNECTIONS RAILS BOTTOM RIGHT: ANALOG IN ******************************** //
// A0 - 23 - PC0 -A0/14- ADC0/PCINT8 - SensorPin ANALOG INPUT ADC //
// A1 - 24 - PC1 -A1/15- ADC1/PCINT9 - ADC //
// A2 - 25 - PC2 -A2/16- ADC2/PCINT10 - ADC //
// A3 - 26 - PC3 -A3/17- ADC3/PCINT12 - ADC //
// A4 - 27 - PC4 -A4/18- ADC4/SDA/PCINT12 - TWI //
// A5 - 28 - PC5 -A5/19- ADC5/SCL/PCINT13 - TWI //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP: //
// Start End Number Description //
// 0000 0000 1 Never use this memory location to be AVR compatible //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898