How to activate an LED using a water sensor... please help me.

Oh dear. My friend and I are working on a project that can be summed up by this: a water activated lock. Everything is working fine, and we are having no trouble with the servos or the water sensor. Our problem is turning on one freaking LED that we want to be activated when the water sensor is activated, and turned off when the sensor is turned off. It’s not a hardware problem, because it turns on when we put the sensor in water, but it doesn’t turn off. We have tried if statements and else statements with no luck. I am relatively new to Arduino (started last summer), and I would greatly appreciate assistance. The code is attached below, and copied and pasted right here. Thanks in advance!

#include <Servo.h>

Servo myservo;

int pos = 0;
int ledPin = 2;
void setup() {
myservo.attach(9);
}

void loop() {
int sensorValue = analogRead(A0);
if (sensorValue > 200)
{
for (pos = 100; pos <= 100; pos += 1) {
myservo.write(pos);
delay(7);
{
if (sensorValue > 200)
{ digitalWrite(ledPin, HIGH);

}
else{
digitalWrite(ledPin, LOW);
}
}
}
{
if (sensorValue = 0)
for (pos = 0; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(7);

}
}
}
else {
myservo.write(15);
}
}

Water_Lock_Project.ino (614 Bytes)

it look as though the code

 else{
        digitalWrite(ledPin, LOW);
      }

will never be executed as it is within the if statement

void loop() {
  int sensorValue = analogRead(A0);
  if (sensorValue > 200)
  {
   ...

use code tabs!

horace:
it look as though the code

 else{

digitalWrite(ledPin, LOW);
      }



will never be executed as it is within the if statement


void loop() {
  int sensorValue = analogRead(A0);
  if (sensorValue > 200)
  {
  ...



use code tabs!

Ok, I'm not quite sure what code tabs are, but I think I know what you mean that it can't be in that if statement.

      if (sensorValue = 0)

= for assignment
== for testing equality

UKHeliBob:

      if (sensorValue = 0)

= for assignment
== for testing equality

Not sure why we would need to change that…

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool. I recommend you to use the standard IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we’re trying to read your code.

The choice of whether to put braces on their own line or not is up to you but you need to pick one way of doing it an be consistent, otherwise it makes your code hard to read and just looks really sloppy.

JoeSchmo7702:
Not sure why we would need to change that…

Then you need to do some research and write some simple test programs until you are sure. It’s extremely important to understand this sort of basic stuff before you attempt a more complex project.

Read the “Warning” section of this page:
https://www.arduino.cc/en/Reference/If

      if (sensorValue = 0)

Do you want to assign 0 to sensorValue or test whether it is equal to 0 ?