Run the code and you can explain it yourself
I will, really but i cannot right now, i hope you guys would be around tomorrow sometime
run the simulation
You defined Float as 2, the keyword HIGH is defined as 1, so:
if (digitalRead (Float == HIGH))
Boils down to:
if(digitalRead(2 == 1)) // FALSE, 2 does not = 1 so 0 is returned and:
if(digitalRead(0)) is the result. Pin 0 is the serial TX pin so when you print something, the TX pin flickers making your LED flicker.
Yes! this is working, but I'm not sure how to make it the switch is UP for the LED to be on
so give it a try.
What is "up" expressed as "switch is opened" / "switch is closed"!?
Change this:
void loop() {
digitalWrite(GREEN, digitalRead(Float));
}
To:
void loop() {
digitalWrite(GREEN, not digitalRead(Float));
}
The "not"-operator is the attention-mark "!"
Do you see the red attentionmarks?
!true = false
!false = true
!HIGH= LOW
!LOW = HIGH
!0 = 1
!1= 0
the not-operator inverts the value
Thank you all so much, I truly appreciate it, I have fully finished my project and I would like to explain it all to you guys in a bit (i know you might not be interested but I think I still would want to).
my project is basically about using the AC's water for watering the plants (I basically take the water from the AC and use a relay + water pump to direct it to the plant).
It's a project for my school, now it's a bit more complicated than that, I take the water from the AC and move it a Bucket that I have a Floating Switch Liquid Sensor that I put attached to it, and once the switch is LOW (when the water level gets to the switch it's condition is LOW), the relay is supposed to allow the water pump to work and move the water from the FIRST bucket to a reserve bucket, a second bucket.
I did the same thing in the second bucket, i added another Floating Switch Liquid Sensor and another water pump, and from there the water pump is supposed to move the water to the plant that's planted in the soil.
I have also attached a Soil Moisture Level Sensor to the soil, so that when the Humidity or Wetness whatever you call it that's in the soil is high, it gives a command to the water pump in the second bucket to not work, thus, not killing the plant.
I'm using the codes you guys helped me with when I faced the problem with the water pump being stuck on HIGH all the time or when it was instantly changing from HIGH to LOW all the time, and it did truly help, thank you.
But now I'm facing another problem, while the codes are supposed to work properly, now when the Floating Water Sensor is on LOW (basically when it's floating) the water pump works - which is what's supposed to happen, but it doesn't stop once the Float Water Sensor changes to HIGH, instead it keeps going.
but if the Float Water Sensor is on HIGH (not working) once I upload the code it, the water pump is not supposed to work - yet it works like twice and then stops as it's supposed to be, why does it work twice? and how to solve all of this?
I can try and attach the whole code in here (it's not done like I'm supposed to add more stuff to finish the project, but I can attach it so you guys at least see what's the problem with the current code)
before I upload the code, let me clarify few stuff:
- LS stands for Level Switch (for the floating liquid water switch sensor)
- relayp stands for relay
- the lcd screen works 100% just fine with me and im able to show the Soil Moisture Level on it
- when the LS - Level Sensor is floating it's LOW, when it's not it's HIGH
- when the relay is activated it's LOW, when it's not it's HIGH (same as the LS)
- I'm sure the problem is with the coding
- I have made sure I have everything connected and defined correctly in the code
if you're wondering there is a problem with the wires, i checked multiple times, and there are 0 problems, and there are no problems with the variables im using or the names of the relay/water sensor (I mean i'm using the right LS - Level Sensor and the right relay in my statements), so I don't have any idea why it's not working.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define SoilMS A0
int relayp1 = 1;
int relayp2 = 2;
int LED1 = 3;
int LED2 = 4;
#define LS1 5
#define LS1_1 6
#define LS2 7
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
pinMode(relayp1, OUTPUT);
pinMode(relayp2, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LS1, INPUT_PULLUP);
pinMode(LS1_1, INPUT_PULLUP);
pinMode(LS2, INPUT_PULLUP);
lcd.init();
lcd.backlight();
}
void loop() {
if (digitalRead(LS2) == LOW)
{
digitalWrite(relayp2, LOW);
Serial.println("LS2 is LOW, Relay p2 activated.");
}
else
{
digitalWrite(relayp2, HIGH);
}
Serial.print("SoilMS Value: ");
Serial.print(SoilMSValue());
Serial.println("%");
lcd.setCursor(0,0);
lcd.print("SoilMS Value: ");
lcd.setCursor(0,1);
lcd.print(SoilMSValue());
lcd.setCursor(2,1);
lcd.print("%");
delay(3000);
}
int SoilMSValue(){
int MSValue = analogRead(SoilMS);
int MSValuePercentage = map(MSValue,0,1023,100,0);
return MSValuePercentage;
}
I hope you guys are able to solve it, you've been a big help, thank you so much
I would have purchased a float switch that has no/nc contacts, added a resistor and LED, power supply, and that would be it.
The " ! " means nothing to a beginner, it's just another one of those undecipherable computer hieroglyphics like "^", ">", and "$", the word "not" is instantly comprehended.
I believe the keywords "not", "and", "or" are supported in C++ versions 11 and later
is it okay if you guys reply to the problem I stated 3 comments ago :c:
First thing I see:
int relayp1 = 1; // variable = 0 (LOW) by default at startup
int relayp2 = 2;
void setup() {
Serial.begin(9600);
pinMode(relayp1, OUTPUT); // relay pins are LOW (ON) here
pinMode(relayp2, OUTPUT);
Turns the relays ON before loop() starts, change to:
void setup() {
Serial.begin(9600);
digitalWrite(relayPin1, HIGH); // sets relay pin HIGH (OFF)
digitalWrite(relayPin2, HIGH);
pinMode(relayp1, OUTPUT); // relay is OFF
pinMode(relayp2, OUTPUT);
you were right about that, for some reason though there is a problem with the Float Switch apparently, I'm not sure if it's the code or the wire conncection but it doesn't change from HIGH to LOW at all.
Do you see anything wrong with the coding for the Float Switch? (LS2)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define SoilMS A0
int relayp1 = 1;
int relayp2 = 2;
int LED1 = 3;
int LED2 = 4;
#define LS1 5
#define LS1_1 6
#define LS2 8
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
digitalWrite(relayp1,HIGH);
digitalWrite(relayp2,HIGH);
pinMode(relayp1, OUTPUT);
pinMode(relayp2, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LS1, INPUT_PULLUP);
pinMode(LS1_1, INPUT_PULLUP);
pinMode(LS2, INPUT_PULLUP);
lcd.init();
lcd.backlight();
}
void loop() {
if ((LS1) == LOW && (LS2) == LOW && (SoilMSValue) < 35)
{
digitalWrite(relayp1,LOW);
digitalWrite(LED1,HIGH);
}
if ((LS1) == LOW && (LS2) == LOW && (SoilMSValue) > 35)
{
digitalWrite(relayp1,HIGH);
digitalWrite(LED1,LOW);
}
if ((LS1) == LOW && (LS2) == HIGH && (SoilMSValue) < 35)
{
digitalWrite(relayp1,LOW);
digitalWrite(LED1,HIGH);
}
if ((LS1) == LOW && (LS2) == HIGH && (SoilMSValue) > 35)
{
digitalWrite(relayp1,LOW);
digitalWrite(LED1,HIGH);
}
if ((LS1) == HIGH)
{
digitalWrite(relayp1,HIGH);
digitalWrite(LED1,LOW);
}
if ((LS2) == LOW && (SoilMSValue) < 35)
{
digitalWrite(relayp2,LOW);
digitalWrite(LED2,HIGH);
}
if ((LS2) == LOW && (SoilMSValue) > 35)
{
digitalWrite(relayp2,HIGH);
digitalWrite(LED2,LOW);
}
if ((LS2) == HIGH)
{
digitalWrite(relayp2,HIGH);
digitalWrite(LED2,LOW);
}
if ((LS1_1) == LOW)
{
digitalWrite(relayp1,LOW);
digitalWrite(LED1,HIGH);
}
Serial.print("SoilMS Value: ");
Serial.print(SoilMSValue());
Serial.println("%");
lcd.setCursor(0,0);
lcd.print("SoilMS Value: ");
lcd.setCursor(0,1);
lcd.print(SoilMSValue());
lcd.setCursor(2,1);
lcd.print("%");
delay(1000);
}
int SoilMSValue(){
int MSValue = analogRead(SoilMS);
int MSValuePercentage = map(MSValue,0,1023,100,0);
return MSValuePercentage;
}
This is my final code
LS1 == 5, so if (LS1 == LOW) will always be false. Don't forget the digitalRead()
You have to :
digitalRead(LS1); // and LS2
First:
if (digitalRead(LS1) == LOW && digitalRead(LS2) == LOW &&
analogRead(SoilMSValue) < 35)
Here's a start, untested (I don't have your hardware).
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define SoilMS A0
int relayp1 = 1;
int relayp2 = 2;
int LED1 = 3;
int LED2 = 4;
byte LS1State, LS2State;
#define LS1 5
#define LS1_1 6
#define LS2 8
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
Serial.begin(9600);
digitalWrite(relayp1,HIGH);
digitalWrite(relayp2,HIGH);
pinMode(relayp1, OUTPUT);
pinMode(relayp2, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LS1, INPUT_PULLUP);
pinMode(LS1_1, INPUT_PULLUP);
pinMode(LS2, INPUT_PULLUP);
lcd.init();
lcd.backlight();
}
void loop()
{
LS1State = digitalRead(LS1); // save LS states for later use in loop
LS2State = digitalRead(LS2);
if (LS1State == LOW && LS2State == LOW && SoilMSValue() < 35)
{
digitalWrite(relayp1,LOW);
digitalWrite(LED1,HIGH);
}
// inserted deadband value of 3 to prevent ON / OFF jitter
else if (LS1State == LOW && LS2State == LOW && SoilMSValue() > 38)
{
digitalWrite(relayp1,HIGH);
digitalWrite(LED1,LOW);
}
// continue from here
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.