Coding a rain water sensor to a 180 servo

Hi I am new to Arduino and this is my first post to the forum.

I have worked through the project book and now it is time for me to design my first project, the project is for a university project. A bench that has a moving canopy and when it rains the canopy moves over the bench. I am far from a programmer or computer genius and require some help with the coding.

I have tried to manipulate code out of the book to make the water sensor move the servo that is it connected to with no luck.

I have the water sensor connected up to A0 and the Servo to pin 9. I literally want the servo to move 180 when it senses rain water on the sensor.

Dose any one have any idea what kind of programme language or wording I should be using and what order I should be writing it in e.g. code the water sensor before the servo or vice-versa?

I have attached a photo of the set up.

This has made me mind boggled :confused: :o :frowning: :relaxed: :cold_sweat: :roll_eyes:

Thanks so much in advance :smiley:

I have tried to manipulate code out of the book to make the water sensor move the servo that is it connected to with no luck.

Luck has precious little to do with programming.

What happened that you didn't want?
What didn't happen that you did want?

I am not sure how to program the sensor to make the servo react.

I programmed the sensor to the pin and the servo but I'm not sure what i need to tell the sensor to tell the servo?

Thanks for the reply

This is my code

#include <Servo.h>
Servo myServo;
int const potPin = A0;
int potVal;
int angle;
void setup() {
// initialize serial communication at 9600 bits per second:
myServo.attach (9);
Serial.begin(9600);
}

void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0); Serial.println(sensorValue);
delay(100);}

int sensorValue = LOW;
potVal = analogRead(potPin);
===Serial.print("potVal; ");
Serial.print(angle);
angle = map(potVal 1, 1023,1 179);
Serial.print(", angle; ");
Serial.println(angle);
myServo.write(angle);
delay(20);
}

}

===Serial.print("potVal; ");

Wassat?

I think I'm surprised that code even compiles, so it's hard to say whether it works or not.

oo forgot to take that out i just sent it to a friend to try and make some sense of it ignore the === it was just me highlighting an error code sorry

11050426:
I'm not sure what i need to tell the sensor to tell the servo?

In general you don't tell the sensor anything. Your sketch gets information from the sensor. Your sketch then acts on that information, possibly by positioning the servo.

Do you know how to get the information from your sensor?
Do you know how to use the servo library to position your servo?
Do you know when and to where you want the servo to move?

Once you figure out those three simple steps you will be able to write your sketch.

No - where can I find this information?
I think so
and yes

So the first step is to figure out your "rain sensor". Typically you would start with where you bought the sensor. Often they will provide a pointer to an example sketch or a specialized library to get data from the sensor. Did the vendor have any such information? Can you find another vendor of the same device that has better information?

I got it from Ebay but I will look in to it now

What happens if you just use this part of your code

#include <Servo.h>
Servo myServo;
int const potPin = A0;
int potVal;
int angle;
void setup() {
// initialize serial communication at 9600 bits per second:
myServo.attach (9);
Serial.begin(9600);
}
void loop() {
     // read the input on analog pin 0:
   int sensorValue = analogRead(A0); 
   Serial.println(sensorValue);
   delay(500);}
}

If the values you see on the Serial Monitor can be interpreted as properly detecting the presence or absence of rain you are more than half way there.

Note that I increased the delay() just for testing. And strictly speaking the servo stuff is irrelevant, but I did not bother to remove it.

...R

For the servo, you might look at the "knob" servo example code in the IDE. That code positions a servo based on a voltage input to the arduino.

What happens if you just use this part of your code

It doesn't compile?

Robin2:
What happens if you just use this part of your code

#include <Servo.h>

Servo myServo;
int const potPin = A0;
int potVal;
int angle;
void setup() {
// initialize serial communication at 9600 bits per second:
myServo.attach (9);
Serial.begin(9600);
}
void loop() {
    // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  delay(500);}
}



If the values you see on the Serial Monitor can be interpreted as properly detecting the presence or absence of rain you are more than half way there.

Note that I increased the delay() just for testing. And strictly speaking the servo stuff is irrelevant, but I did not bother to remove it.

...R

Thanks for your help.....

I am receiving values of 0 - 1023 on the serial monitor for the rain sensor and when it gets we the value drops. So i think this is working.

I used that part of the code you said and the servo moved 90 degrees when i uploaded it but when the rain sensor gets wet it is not doing anything.

zoomkat:
For the servo, you might look at the "knob" servo example code in the IDE. That code positions a servo based on a voltage input to the arduino.

Im sorry I'm a bit new where is the "Knob" servo example?

Isn't there one in the IDE?

I have the servo and the Rain sensor working together now using this code.

Now I just need to know how to make the servo turn a full 180 degrees and stay there while it has water on it.

Any ideas?

#include <Servo.h>
Servo myServo;

int const potPin = A0;
int potVal;
int angle;

void setup() {
myServo.attach(8);

Serial.begin(9600);

}

void loop() {
potVal = analogRead(potPin);
Serial.print("potval; ");
Serial.print(potVal);
angle = map(potVal, 1 - 1023, 1023, 0, 150);
Serial.print (", angle: ");
Serial.println(angle);

myServo.write(angle);
delay(15);

I am up and running thanks for the help!

angle = map(potVal, 1 - 1023, 1023, 0, 150);

potval is never, ever going anywhere near -1022

The potvalue is at a constant 1023 or just below and drops when water is applied to the sensor..... dose this mean its working backwards?