Why this no work?!

Ok, I’m reading a rain sensor and a solar panel, trying to make it to where an actuator will open IF it is not raining AND if it’s daytime.

Only the rain sensor portion is triggering the relays for the actuator. It’s as if the analog read of the solar voltage doesn’t even work, however, on the serial monitor I can see the voltage print.

I’m sure I’m just being stupid and am missing something obvious, but this has been frustrating me for days. Help is you can!

//Use constants for the relay pins in case you need to change these later
const int relay1 = 6; //Arduino pin that triggers relay #1
const int relay2 = 7; //Arduino pin that triggers relay #2
// lowest and highest sensor readings:
const int sensorMin = 0; // sensor minimum
const int sensorMax = 1024; // sensor maximum
int sensorValue = 0;

void setup() {
// initialize serial communication:
Serial.begin(9600);

//Set pinMode to OUTPUT for the two relay pins
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);

}

void extendActuator() {
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
}

void retractActuator() {
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
}

void loop() {
// read the sensor on analog A0:
int sensorReading = analogRead(A0);
// map the sensor range (four options):
// ex: ‘long int map(long int, long int, long int, long int, long int)’
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

// range value:
switch (range) {
case 0: // Sensor getting wet
Serial.println(“Flood”);
break;
case 1: // Sensor getting wet
Serial.println(“Rain Warning”);
break;
case 2: // Sensor dry - To shut this up delete the " Serial.println(“Not Raining”); " below.
Serial.println(“Not Raining”);
break;
}
delay(500); // delay between reads

Serial.println(sensorReading);
delay(500);

// read the sensor on analog A1:
int sensorValue = analogRead(A1);
// map the sensor range (four options):
// ex: ‘long int map(long int, long int, long int, long int, long int)’
float voltage = sensorValue * (5.0 / 1023.0);

Serial.println(voltage);

if (analogRead(A0) > 900 && analogRead(A1) > 2.00){
// Open Actuator:
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);

}
else if (analogRead(A0) < 900 && analogRead(A1) < 2.00){
// Close Actuator:
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);

}
else if (analogRead(A0) < 900 && analogRead(A1) > 2.00){
// Close Actuator:
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);

delay(500);
}
}

You have calculated voltage but never use it in your tests. But given that you're comparing what you get from analogRead to 2.0, it looks like you think you're using voltage.

if (analogRead(A0) > 900 && analogRead(A1) > 2.00){

Did you mean

if (analogRead(A0) > 900 && voltage > 2.00){

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

wildbill:
You have calculated voltage but never use it in your tests. But given that you’re comparing what you get from analogRead to 2.0, it looks like you think you’re using voltage.

Ok Wildbill, I see what you’re saying. I need to either compare the calculated voltage from my equation or change my if statement to reflect the input that’s actually being received on A1.

When I try what AWOL has suggested I still get no response.

So I modified the code to this. It seems like it wants to function but I have to trigger the water sensor before it will register the solar voltage and close or open the relay:

if (analogRead(A0) > 900 && (analogRead(A1) * (5.0 / 1023.0)) > 2.00){
// Open Actuator:
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);

}
else if (analogRead(A0) < 900 && (analogRead(A1) * (5.0 / 1023.0)) < 2.00){
// Close Actuator:
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);

}
else if (analogRead(A0) < 900 && (analogRead(A1) * (5.0 / 1023.0)) > 2.00){
// Close Actuator:
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);

There’s got to be a cleaner way to write that.

TheMemberFormerlyKnownAsAWOL:

if (analogRead(A0) > 900 && analogRead(A1) > 2.00){

Did you mean

if (analogRead(A0) > 900 && voltage > 2.00){

TomGeorge:
Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Sorry Tom, I'll make those changes as I post.

I'll draw out the schematic and upload it.

Also, adding that line of code for converting the A1 to voltage didn't work.

Post your latest code.

It seems overly complicated though. All you need is conditions for one if. If it's true, open the actuator else close it.

I’ve changed the A1 print to print the unconverted signal strength. With the lights on I’m getting over 500 and with the lights off it’s closer to 100. So I’ve changed the code to reflect this reading instead of reading voltage. There is still no change. Only the water sensor is tripping the relays. It’s as if the && function isn’t right.

//Use constants for the relay pins in case you need to change these later
const int relay1 = 6; //Arduino pin that triggers relay #1
const int relay2 = 7; //Arduino pin that triggers relay #2
// lowest and highest sensor readings:
const int sensorMin = 0;     // sensor minimum
const int sensorMax = 1024;  // sensor maximum
int sensorValue = 0; 



void setup() {
// initialize serial communication:
  Serial.begin(9600);

//Set pinMode to OUTPUT for the two relay pins
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
 


}

void extendActuator() {
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
}

void retractActuator() {
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
}



void loop() {
  // read the sensor on analog A0:
  int sensorReading = analogRead(A0);
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
  
  // range value:
  switch (range) {
 case 0:    // Sensor getting wet
    Serial.println("Flood");
    break;
 case 1:    // Sensor getting wet
    Serial.println("Rain Warning");
    break;
 case 2:    // Sensor dry - To shut this up delete the " Serial.println("Not Raining"); " below.
    Serial.println("Not Raining");
    break;
  }
  delay(500);  // delay between reads

Serial.println(sensorReading);
delay(500);


 // read the sensor on analog A1:
  int sensorValue = analogRead(A1);
  float voltage = sensorValue; //* (5.0 / 1023.0);

Serial.println(voltage);


if (analogRead(A0) > 900 && analogRead(A1) > 500.00){
// No Rain & Daylight - Open Actuator:
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, LOW);
  

}
else if (analogRead(A0) < 900 && analogRead(A1) < 500.00){
// Rain & No Daylight - Close Actuator:
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, HIGH);

}
else if (analogRead(A0) < 900 && analogRead(A1) > 500.00){
// Rain & Daylight - Close Actuator:
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, HIGH);
  
delay(500);
}
}

wildbill:
Post your latest code.

It seems overly complicated though. All you need is conditions for one if. If it’s true, open the actuator else close it.

Perhaps it is overly complicated. I’ll just else everything after condition one and see what happens.

wildbill:
Post your latest code.

It seems overly complicated though. All you need is conditions for one if. If it's true, open the actuator else close it.

Whelp, wouldn't you know it. That worked.

if (analogRead(A0) > 900 && analogRead(A1) > 500.00){
// No Rain & Daylight - Open Actuator:
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, LOW);
  

}
else  {
// Close Actuator:
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, HIGH);


delay(500);
}
}

Hi,
Instead of analogRead the same inputs all the time repeatedly through your code.
Can I suggest you read ALL you inputs at the start of the loop and save them to variables.
Then use the variables throughout your code.

In other words take a snapshot of your inputs and use them in your code.

Tom... :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.