Dear all,
I am using four sensors, namely a pressure sensor, load cell, solenoid valve, and soil moisture sensor. What I need is some sensors that will run only when some other sensors give a preset value.
The next step will be like that:
Soil moisture sensor will take a reading per day
Pressure sensor will then record the value and compare the value with setpoints
If matches any steps, then the solenoid valve will ON otherwise remain OFF. Initially the solenoid valve will remain ON longer but after view days the opening time will start decreasing.
Load cell will record the reading per day.
I have some other small conditions which I explain at the end of the individual coding of the sensors.
If anyone can help me integrate all the code into to run with a single Arduino board, it will be a great help. I am attaching all the individual code (tested) and condition in a text file.
Regards
Preferably attach code to post in code tags. Code each sensor individually and encapsulate it so that it won’t interfere with anything outside its scope. Then they all play well together in your main loop and you should just need some ifs and comparators to get the outcome you desire
Your code will not work properly unless it is coordinated with the Hardware. Post a schematic, not a frizzy picture as you have it wired including links to technical information on the individual hardware items. Be sure to include all power and ground connections as well as power source(s). Links to azon and other other sales outlets are almost useless. A hand drawn schematic would be ok. Remember power and signals top to down, left to right.
isn't posting using code tags (</>) easier for everyone?
#include "HX711.h"
#define calibration_factor -106120.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define LOADCELL_DOUT_PIN 3
#define LOADCELL_SCK_PIN 2
HX711 scale;
void setup() {
Serial.begin(9600);
Serial.println("HX711 scale demo");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
Serial.println("Readings:");
}
void loop() {
Serial.print("Reading: ");
Serial.print(scale.get_units(), 3); //scale.get_units() returns a float
Serial.print(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor
Serial.println();
delay (86400000);
}
Soil Moisture Sensor
// Simple code for Measuring Voltage from
// Capacitive soil moisture sensor
//
int soil_pin1 = A0; // Analogue OUTPUT pin on sensor
void setup() {
Serial.begin(9600); // serial port setup
analogReference(EXTERNAL); // set the analog reference to 3.3V
}
void loop() {
Serial.print("Soil Moisture Sensor Voltage: ");
Serial.print((float(analogRead(soil_pin1))/1023.0)*3.3); // read sensor
Serial.println(" V");
delay(86400000); // slight delay between readings
}
Pressure Sensor
#include "Wire.h" //allows communication over i2c devices
const int pressureInput = A2; //select the analog input pin for the pressure transducer
const int pressureZero = 102.4; //analog reading of pressure transducer at 0psi
const int pressureMax = 921.6; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 250; //psi value of transducer being used
const int baudRate = 9600; //constant integer to set the baud rate for serial monitor
const int sensorreadDelay = 250; //constant integer to set the sensor read delay in milliseconds
float pressureValue = 0; //variable to store the value coming from the pressure transducer
void setup() //setup routine, runs once when system turned on or reset
{
Serial.begin(baudRate); //initializes serial communication at set baud rate bits per second
}
void loop() //loop routine runs over and over again forever
{
pressureValue = analogRead(pressureInput); //reads value from input pin and assigns to variable
pressureValue = ((pressureValue-pressureZero)*pressuretransducermaxPSI)/((pressureMax-pressureZero)); //conversion equation to convert analog reading to kpa(removing 6.9, we will get psi reading)
Serial.print(pressureValue, 1); //prints value from previous line to serial
Serial.println("psi"); //prints label to serial
// Serial.println("kpa"); //prints label to serial
delay(sensorreadDelay); //delay in milliseconds between read values
}
Solenoid Valve
// So, if we want the solenoid to allow water to flow, set the pin high. When we want the water to stop flowing, set the pin low.
// Normally, the valve is closed. When 12VDC is applied to the two terminals, the valve opens, and water can flow.
void setup()
{
pinMode(12,OUTPUT); // RELAY PIN
digitalWrite(12,HIGH); // Normally ON Only For Chanies Relay Module
}
void loop()
{
digitalWrite(12,LOW); // RELAY ON
delay(30000);
digitalWrite(12,HIGH); // RELAY OFF
delay(30000);
}
Just look at your solenoid valve code as an example. You have not named the pins so when you combine code you won’t be able to read it. Declare pin 12 as solenoidPin.
You have delays so you are using “blocking code”. You can’t combine codes that have blocking code because one codes delay prevents anything else happening. Look up blink without delay tutorial
Your code is not encapsulated in any way so any variables declared will be in the same scope as any other in the loop. That means that they are vulnerable to being messed with. At the very least you should create functions for discrete actions, include timing within and then call them in the loop. Look up class in c++ as this allows you to create a library and will encourage encapsulated code and OOP