Pages: [1] 2   Go Down
Author Topic: moisture sensors  (Read 1014 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im having problems with my sketch i just started a month ago so im a newbie....if anyone can help me fix this PLEASE

Code:
const int TxPin = 6;
const int buttonPin1 = 2;  
const int buttonPin2 = 3;  
const int buttonPin3 = 4;  
const int buttonPin4 = 5;  
const int pumpPin = 9;                
const int valvePin1=13;        
const int valvePin2 = 12;
const int valvePin3 = 11;
const int valvePin4 = 10;
int buttonState1 = LOW;
int buttonState2 = LOW;
int buttonState3 = LOW;
int buttonState4 = LOW;
const int PROBE = A0-A4; // Analog pin 0-3
int MOISTURE_LEVEL = 250; // the value after the LED goes ON

#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

void setup() {
  
   pinMode(PROBE,INPUT);
   pinMode(pumpPin,OUTPUT);
   pinMode(valvePin1,OUTPUT);
   pinMode(valvePin2,OUTPUT);
   pinMode(valvePin3,OUTPUT);
   pinMode(valvePin4,OUTPUT);
   pinMode(TxPin, OUTPUT);
   digitalWrite(TxPin, HIGH);
   digitalWrite(pumpPin, HIGH);
   digitalWrite  (valvePin1,HIGH);
   digitalWrite(valvePin2,HIGH);
   digitalWrite(valvePin3,HIGH);
   digitalWrite(valvePin4,HIGH);
   mySerial.begin(9600);
   mySerial.write(12);                 // Clear  
   delay(5);  
   mySerial.write(17);
   mySerial.write(22);  //
                             // Required d
}
void loop(){


   mySerial.write(128);
  mySerial.print("S1-");
  
   mySerial.write(148);  
  mySerial.print( "S2-");  

 
  mySerial.write(168);
  mySerial.print("S3-");
 

   mySerial.write(188);  
  mySerial.print( "S4-");  
  
  mySerial.write(132);  
  mySerial.print (analogRead(A0));
   mySerial.write(152);  
  mySerial.print (analogRead(A1));
   mySerial.write(172);
  mySerial.print (analogRead(A2));
    mySerial.write(192);
  mySerial.print (analogRead(A3));
    
    
  
  
  if (analogRead(A3) > MOISTURE_LEVEL)
{
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin4, LOW);
}  
  if (analogRead(A3) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin4, HIGH);
}
  if (analogRead(A1) > MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin2, LOW);
}
  if (analogRead(A1) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin2, HIGH);
}    
  if (analogRead(A2) > MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin3, LOW);
}
  if (analogRead(A2) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin3, HIGH);
}  
  if (analogRead(A0) > MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin1, LOW);
}
  if (analogRead(A0) < MOISTURE_LEVEL)
{
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin1, HIGH);
}
}


it works sort of...i wanna add buttons for each valve but one step at a time i guess smiley-confuse


Moderator edit: [code] [/code] tags added.
« Last Edit: February 10, 2013, 10:18:37 pm by Coding Badly » Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure what you are thinking here but am pretty sure it's not right smiley
Code:
const int PROBE = A0-A4; // Analog pin 0-3
...

void setup() {
   pinMode(PROBE,INPUT);
   ...
}

Maybe you want
Code:
const int PROBE1=A0, PROBE2=A1, PROBE3=A2, PROBE4=A3;

pinMode( PROBE1,  INPUT);
pinMode( PROBE2,  INPUT);
... etc

But, I would recommend getting ONE of them working before writing all that non-working code for FOUR...

And then you can learn about arrays to expand for the other three smiley

Cheers,
John
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

please more help.....im new
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
please more help.....im new
When learning to walk, one does not enter a triathlon as the first step.

Dump 75% of that code. Deal with ONE sensor.

Give us a clue as to what you are talking to with the mySerial instance. How are we supposed to know if you have it connected correctly without even knowing what it is? How are we supposed to know if you are sending it the right data, without even knowing what it is?

Did you at least connect power and ground to the it, whatever it is?

You don't have any Serial.print() or Serial.println() statements in the code, so how do you have any clue that it is reading the moisture sensors (whatever they are) correctly?

That code does something. You haven't told us what. You want that code to do something. You haven't told us what. Obviously, there are differences between what it does and what you want, but you haven't told us what they are.

Only YOU can help yourself right now. There is a LOT more information you need to provide if you want us to help you.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset




im trying to read 4 moisture sensors on a parallax lcd, and turn a pump and valve on when they hit a certain level i got all the led the represent the pumps and valves to light up and turn off when i want except the last one A0 it flashes instead of being solid i have everything hook up right i think i have no problem doing one sensor more then that










Code:
const int TxPin = 6;       \\lcd pin
const int buttonPin1 = 2;  \\pins that are for
const int buttonPin2 = 3;  \\buttons to open valves
const       int buttonPin3 = 4;                       \\  and pumps
const int buttonPin4 = 5;   
const int pumpPin = 9;        \\pin that turns on the pump   
const int valvePin1=13;        \\pins that run valves
const int valvePin2 = 12;       
const int valvePin3 = 11;
const int valvePin4 = 10;
int buttonState1 = LOW;           \\setting the button state to low
int buttonState2 = LOW;
int buttonState3 = LOW;
int buttonState4 = LOW;
const int PROBE1= A0                \\analog pin A0-A4 moisture sensors
const int PROBE2= A1                 \\called PROBE1- PROBE4
const int PROBE3= A2
const int PROBE4= A0
int MOISTURE_LEVEL = 250;                              // the value after the valves and pump goes ON

#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

void setup() {

      pinMode(PROBE1,INPUT);                    \\ sets PROBE1-PROBE4 TO input
      pinMode(PROBE2,INPUT);                   
   pinMode(PROBE3,INPUT)                                     
   pinMode(PROBE4,INPUT);             
   pinMode(pumpPin,OUTPUT);    \\ sets pumpPin to output
   pinMode(valvePin1,OUTPUT);    \\ sets valvePins to output
   pinMode(valvePin2,OUTPUT);
   pinMode(valvePin3,OUTPUT);
   pinMode(valvePin4,OUTPUT);
   pinMode(TxPin, OUTPUT);          \\lcd pin to output
   digitalWrite(TxPin, HIGH);         
   digitalWrite(pumpPin, LOW);     
   digitalWrite  (valvePin1,LOW);
   digitalWrite(valvePin2,LOW);
   digitalWrite(valvePin3,LOW);
   digitalWrite(valvePin4,LOW);
   mySerial.begin(9600);
   mySerial.write(22);                \\ turns display on
   mySerial.write(17);                \\ turn on lcd back light   
   mySerial.write(12);                 \\ Clear  LCD
   delay(5);                          \\ required delay after clear Lcd
 
   
                             
}
void loop(){


   mySerial.write(128);         \\ sets sets lcd cursor to line 0 space 0
  mySerial.print("S1-");
 
   mySerial.write(148);           \\ sets lcd cursor to line 1 space 0
  mySerial.print( "S2-"); 

 
  mySerial.write(168);              \\sets lcd cursor to line 2 space 0
  mySerial.print("S3-");
 

   mySerial.write(188);              \\sets lcd cursor to line3 space 0
  mySerial.print( "S4-"); 
 
  mySerial.write(132);               \\sets lcd cursor to line 0 space 4
  mySerial.print (analogRead(A0));
   mySerial.write(152);               \\sets lcd cursor to line 1 space 4
  mySerial.print (analogRead(A1));
   mySerial.write(172);                \\sets lcd cursor to line 2 space 4
  mySerial.print (analogRead(A2));
    mySerial.write(192);               \\sets lcd cursor to line 3 space 4
  mySerial.print (analogRead(A3));
   
   
   
 
  if (analogRead(A3) > MOISTURE_LEVEL)
{
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin4, LOW);

  if (analogRead(A3) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin4, HIGH);
}
  if (analogRead(A1) > MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin2, LOW);
}
  if (analogRead(A1) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin2, HIGH);
}     
  if (analogRead(A2) > MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin3, LOW);
}
  if (analogRead(A2) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin3, HIGH);

  if (analogRead(A0) > MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin1, LOW);
}
  if (analogRead(A0) < MOISTURE_LEVEL)
{
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin1, HIGH);
}
}
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int PROBE1= A0                \\analog pin A0-A4 moisture sensors
const int PROBE2= A1                 \\called PROBE1- PROBE4
const int PROBE3= A2
const int PROBE4= A0

Why do you have two probe pins defined as A0? And why are there no semicolons?

And please you code tags when you post code.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
im trying to read 4 moisture sensors on a parallax lcd
Why? Is the LCD getting wet? You really want to stop that from happening.

Quote
i got all the led the represent the pumps and valves to light up and turn off when i want except the last one A0 it flashes
According to the comments in the code, A0 has a moisture sensor attached to it. How can a moisture sensor flash?

Code:
      pinMode(PROBE1,INPUT);                    \\ sets PROBE1-PROBE4 TO input
      pinMode(PROBE2,INPUT);                   
   pinMode(PROBE3,INPUT)                                     
   pinMode(PROBE4,INPUT);
This is not doing anything for the analog pins. Analog pins are input only.

Code:
  if (analogRead(A3) > MOISTURE_LEVEL)
{
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin4, LOW);
}
  if (analogRead(A3) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin4, HIGH);
}
Why are you not using else? If the reading is not greater than MOISTURE_LEVEL (so the first block is skipped), the only choices are for it to be equal to or less than MOISTURE_LEVEL.

As the code is written, you are ignoring the cases where the readings are equal to MOISTURE_LEVEL. Why?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Hope this is better, any more suggestions? Sorry i for the forgotten semi colons when i was typing it out.  at the moment i have leds to represent the pump and valve relays.

Code:
const int TxPin = 6;       //lcd pin
const int buttonPin1 = 2;  //pins that are for
const int buttonPin2 = 3;  //buttons to open valves
const       int buttonPin3  =  4;                       // and pumps
const int buttonPin4 = 5;   
const int pumpPin = 9;        //pin that turns on the pump   
const int valvePin1 = 13;        //pins that run valves
const int valvePin2 = 12;       
const int valvePin3 = 11;
const int valvePin4 = 10;
int buttonState1 = LOW;           //setting the button state to low
int buttonState2 = LOW;
int buttonState3 = LOW;
int buttonState4 = LOW;
const int PROBE1=A0, PROBE2=A1, PROBE3=A2, PROBE4=A3;
int MOISTURE_LEVEL = 250;                              //the value after the valves and pump goes ON

#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

void setup() {

   
   pinMode(pumpPin,OUTPUT);    // sets pumpPin to output
   pinMode(valvePin1,OUTPUT);    // sets valvePins to output
   pinMode(valvePin2,OUTPUT);
   pinMode(valvePin3,OUTPUT);
   pinMode(valvePin4,OUTPUT);
   pinMode(TxPin, OUTPUT);          //lcd pin to output
   digitalWrite(TxPin, HIGH);         
   digitalWrite(pumpPin, LOW);     
   digitalWrite  (valvePin1,LOW);
   digitalWrite(valvePin2,LOW);
   digitalWrite(valvePin3,LOW);
   digitalWrite(valvePin4,LOW);
   mySerial.begin(9600);
   mySerial.write(22);                // turns display on
   mySerial.write(17);                // turn on lcd back light   
   mySerial.write(12);                 // Clear  LCD
   delay(5);                          //required delay after clear Lcd
 
   
                             
}
void loop(){


   mySerial.write(128);        // sets sets lcd cursor to line 0 space 0
  mySerial.print("S1-");
 
   mySerial.write(148);           // sets lcd cursor to line 1 space 0
  mySerial.print( "S2-"); 

 
  mySerial.write(168);              //sets lcd cursor to line 2 space 0
  mySerial.print("S3-");
 

   mySerial.write(188);              //sets lcd cursor to line3 space 0
  mySerial.print( "S4-"); 
 
  mySerial.write(132);               //sets lcd cursor to line 0 space 4
  mySerial.print (analogRead(A0));
   mySerial.write(152);               //sets lcd cursor to line 1 space 4
  mySerial.print (analogRead(A1));
   mySerial.write(172);                //sets lcd cursor to line 2 space 4
  mySerial.print (analogRead(A2));
    mySerial.write(192);               //sets lcd cursor to line 3 space 4
  mySerial.print (analogRead(A3));
   
   
   
 
  if (analogRead(A3) < MOISTURE_LEVEL)
{
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin1, HIGH);

  else
  {
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin4, LOW);
}
  if (analogRead(A1) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin2, HIGH);
}
  else
  {
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin2, LOW);
}     
  if (analogRead(A2) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin3, HIGH);
}
  else
  {
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin3, LOW);

  if (analogRead(A0) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin,HIGH );
    digitalWrite(valvePin1, HIGH);
}
  else
{
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin1, LOW);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   pinMode(TxPin, OUTPUT);          //lcd pin to output
You should not be diddling with the SoftwareSerial instance's pin(s).

I'd like to see you add Serial.begin() to setup() (with some baud rate), and read the pins differently.

Code:
int val0 = analogRead(PROBE0);
Serial.print("Probe 0: ");
Serial.println(val0);
if(val0 < MOISTURE_LEVEL)
{
  // Stuff goes here
}
else
{
  // Stuff goes here
}

Then, open the Serial Monitor, and tell us what output you see. You may have a hardware problem.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6944
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So your problem was that the LED was flickering. After your most recent code iteration, is the LED still flickering? What is the latest status of problems you see?

Besides, if you use real devices, pumps and fans, in place of the LEDs, you will see the devices turn on and off, then on and off etc. You need at some hysteresis so you compare if value is above threshold1 to decide whether to turn on and compare if value is below threshold2, not 1 again to decide whether to turn off.
Logged


Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I count 1 pump and 4 valves.

(1) Do you want the pump to turn on when any of the four moisture sensors are dry?
(2) Do you want the pump to turn off when any of the four moisture sensors are wet?

It looks to me like your code is doing both (1) and (2).

Cheers,
John
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

its still flickering.....yes i run one pump and four valves , the pump and valve are suppose to turn on when the the sensor is dry..i didnt put equal in cause i thought i wouldnt matter
Code:
const int TxPin = 6;       //lcd pin
const int buttonPin1 = 2;  //pins that are for
const int buttonPin2 = 3;  //buttons to open valves
const int buttonPin3 = 4;                       // and pumps
const int buttonPin4 = 5;   
const int pumpPin = 9;        //pin that turns on the pump   
const int valvePin1 = 13;        //pins that run valves
const int valvePin2 = 12;       
const int valvePin3 = 11;
const int valvePin4 = 10;
int buttonState1 = LOW;           //setting the button state to low
int buttonState2 = LOW;
int buttonState3 = LOW;
int buttonState4 = LOW;
const int PROBE1=A0, PROBE2=A1, PROBE3=A2, PROBE4=A3;
int MOISTURE_LEVEL = 250;                              //the value after the valves and pump goes ON

#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

void setup() {

   
   pinMode(pumpPin,OUTPUT);    // sets pumpPin to output
   pinMode(valvePin1,OUTPUT);    // sets valvePins to output
   pinMode(valvePin2,OUTPUT);
   pinMode(valvePin3,OUTPUT);
   pinMode(valvePin4,OUTPUT);
   pinMode(TxPin, OUTPUT);          //lcd pin to output
   digitalWrite(TxPin, HIGH);         
   digitalWrite(pumpPin, LOW);     
   digitalWrite  (valvePin1,LOW);
   digitalWrite(valvePin2,LOW);
   digitalWrite(valvePin3,LOW);
   digitalWrite(valvePin4,LOW);
   mySerial.begin(9600);
   mySerial.write(22);                // turns display on
   mySerial.write(17);                // turn on lcd back light   
   mySerial.write(12);                 // Clear  LCD
   delay(5);                          //required delay after clear Lcd
 
   
                             
}
void loop(){


   mySerial.write(128);        // sets sets lcd cursor to line 0 space 0
  mySerial.print("S1-");
 
   mySerial.write(148);           // sets lcd cursor to line 1 space 0
  mySerial.print( "S2-"); 

 
  mySerial.write(168);              //sets lcd cursor to line 2 space 0
  mySerial.print("S3-");
 

   mySerial.write(188);              //sets lcd cursor to line3 space 0
  mySerial.print( "S4-"); 
 
  mySerial.write(132);               //sets lcd cursor to line 0 space 4
  mySerial.print (analogRead(A0));
   mySerial.write(152);               //sets lcd cursor to line 1 space 4
  mySerial.print (analogRead(A1));
   mySerial.write(172);                //sets lcd cursor to line 2 space 4
  mySerial.print (analogRead(A2));
    mySerial.write(192);               //sets lcd cursor to line 3 space 4
  mySerial.print (analogRead(A3));
   
   
   
 
  if (analogRead(A3) < MOISTURE_LEVEL)
{
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin1, HIGH);

  else
  {
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin4, LOW);
}
  if (analogRead(A1) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin2, HIGH);
}
  else
  {
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin2, LOW);
}     
  if (analogRead(A2) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin, HIGH);
    digitalWrite(valvePin3, HIGH);
}
  else
  {
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin3, LOW);

  if (analogRead(A0) < MOISTURE_LEVEL)
  {
    digitalWrite(pumpPin,HIGH );
    digitalWrite(valvePin1, HIGH);
}
  else
{
    digitalWrite(pumpPin, LOW);
    digitalWrite(valvePin1, LOW);

}
}
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

its still flickering.....yes i run one pump and four valves , the pump and valve are suppose to turn on when the the sensor is dry..

Think about if some of the four sensors are dry and others are wet...

Your code:
The dry ones are telling the pump "turn on!"
The wet ones are telling the pump "turn off!"

If I was the pump I would probably be flickering too smiley

John
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok how do i change it? and why does it only do it for one valve and non of the others?
« Last Edit: February 12, 2013, 12:52:55 am by s1lim » Logged

Finland
Offline Offline
Jr. Member
**
Karma: 1
Posts: 84
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Would that be pin1? That's because of the first if-else. You don't have anything that would turn valvePin4 HIGH, but then you have 1 in too many places. It does not look logical.

However, you can have more problems when the sensor readings change. Write it in pseudo code what you want happen, then design it further. I think what you want is that the pump would turn on when any of the valves need to be open. Then when all the valves are off, you might as well turn the pump off too. Now what happens is that if any of the valves turn off, so does the pump, and then on the second round it turns on (because of one sensor reading), and off again (because of another reading).

One tip I can give you is to use either int or bool variables for what state each element should be in, and then act on it decisively after the if-else conditions.
Logged

Pages: [1] 2   Go Up
Jump to: