HELP: Code for school project, rain sensing motor

Hi guys, i’m in desperate need of some expert assistance. I have a school project due on friday and ive bitten off more than i can chew in the coding department.

i need a motor to turn one way when it gets a signal from a rain sensor, and stop when it reaches a switch.

i have managed to create code capable of activating the motor upon receiving the signal from the rain sensor, but im stumped when it comes to the switch. Also the code i made works with a small motor (3V?) but i cannot get the larger motor to work with the motor shield at all.

ive tried (and failed) to create a bit of code that does this:

  1. motor is activated when a signal is received from the rain sensor
  2. motor stops when switch is depressed
  3. motor reverses when second switch is activated
  4. code resets

components that i have:
V1 motor shield
9-12V motor
rain sensor (4 pins)
arduino uno

code so far

#include <AFMotor.h>

AF_DCMotor motor(2);
const int sensorMin = 0;     // sensor minimum
const int sensorMax = 1024;  // sensor maximum
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
motor.setSpeed(255);
  motor.run(RELEASE);
}

int i;

void loop() {
// read the input on analog pin 0:
int sensorReading = analogRead(A0);
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

switch (range) {
 case 0:    // Sensor getting wet
    motor.run(FORWARD);
    break;
 case 1:    // Sensor getting wet
    motor.run(FORWARD);
    break;
 case 2:    // Sensor dry - To shut this up delete the " Serial.println("Not Raining"); " below.
    motor.run(BACKWARD);
    break;
  }
  delay(1);  // delay between reads
}

Cheers! :slight_smile:

Can you post a schematic of your wiring? What is powering the motor (not the Arduino, I hope)?

const int sensorMax = 1024;

The highest number that analogRead can return is 1023.

int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

Put a print statement after this to see what is actually returned with a range of inputs (sensorReading). You may not be getting what you expect.

The map() call is silly. You want to do one thing when the sensor value is above a threshold, and another when it is below the threshold. That means an if statement with an else statement, not a map() and switch statement.

Once you start the motor moving, you need to read the state of a (properly wired) limit switch, and stop the motor when the switch becomes pressed. a while statement and a digitalRead() call are all that is required.

Sorry if i miss interpret something that you say, im very new to this, never learnt any electronics ate school etc.

Ok so ive got the motor shield powered through the external power input with a 9v battery. The battery turns the motor just fine when i connect it directly. i have removed the power jumper.

sorry its not a propper wiring diagram, but im sure it’ll help you understand the mess ive made…

21397692_1395044820602615_271867778_n.jpg

The highest number that analogRead can return is 1023.

So?

Put a print statement after this to see what is actually returned with a range of inputs (sensorReading). You may not be getting what you expect.

Using 1023 in the map call would result in it returning 0, 1, 2, or 3. By using 1024 in the call, the map() function will return 0, 1, or 2, which is what OP wrote code to deal (uselessly) with.

Ok so ive got the motor shield powered through the external power input with a 9v battery.

No. The shield is powered by the Arduino. The motor is powered by the (next-to-useless) 9V battery (if you are talking about the rectangular PPV battery).

The battery turns the motor just fine when i connect it directly.

What kind of shield are you using? Some shields drop a lot of current to supply a regulated voltage to the motor. Yours may be dropping too much of the little current available.

PaulS:
The map() call is silly. You want to do one thing when the sensor value is above a threshold, and another when it is below the threshold. That means an if statement with an else statement, not a map() and switch statement.

Once you start the motor moving, you need to read the state of a (properly wired) limit switch, and stop the motor when the switch becomes pressed. a while statement and a digitalRead() call are all that is required.

Yeah, the if & else statements seem like a better way of doing it. I made this bit of code by cutting and pasting bits from various examples that i found throughout the web and it kinda worked but i understand that its in no way the best way of doing it.

How would i go about implementing the if & else? i understand it in my mind of how it would go, but nim not sure about how to actually code it.

what im after is a

IF sensor reads (1 or whatever the threshold is) then activate motor
i dont get how the switch bit to tell the motor to stop fits in. Is this an if inside an if? like if switch reads 0 then continue the motor, else stop the motor, something like that?

ELSE motor does not run

insert delay
Loop

PaulS:
No. The shield is powered by the Arduino.

Yeah, thats what i meant, sorry

PaulS:
What kind of shield are you using? Some shields drop a lot of current to supply a regulated voltage to the motor. Yours may be dropping too much of the little current available.

Im using an Iduino motor shield. I believe its a knock off of the V1 adafruit motor shied

Im using an Iduino motor shield. I believe its a knock off of the V1 adafruit motor shied

Those "links" don't work.

int rainSensorValue = analogRead(rainSensorPin);
if(rainSensorValue > 682)
{
   // It's raining
   closeTheWindow();
}
else
{
   // It's not raining
   openTheWindow();
}

Now, create a function to close the window.

void closeTheWindow()
{
   // Turn the motor on
   while(digitalRead(limitSwitchPin) == HIGH) // or LOW, depending on what not-pressed means
   {
      // Do nothing
   }
   // Turn the motor off. The limit switch became pressed
}

You need to replace the first and last comments with real code to start and stop the motor.

Alrighty, ive had a red hot go, but need to clarify some things.

here is what ive made; its by no means finished

#include <AFMotor.h>

AF_DCMotor motor(2);
const int sensorMin = 0;     // sensor minimum
const int sensorMax = 1024;  // sensor maximum
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
motor.setSpeed(200);
  motor.run(RELEASE);
}

int i;

void loop() {
// read the input on analog pin 0:
int rainSensorValue = analogRead(A0);
if(rainSensorValue > 682)
{

// It's raining
   closeTheWindow(motor.run(FORWARD));
}
else
{
   // It's not raining
   openTheWindow(motor.run(RELEASE));
}

void closeTheWindow() 
{
   // Turn the motor on
   while(digitalRead(limitSwitchPin) == HIGH) // or LOW, depending on what not-pressed means
   {
      motor.run(RELEASE)// Do nothing
   }
   // Turn the motor off. The limit switch became pressed
}
{
   while(digitalRead(limitSwitchPin) == HIGH) // second switch to open the window
      motor.run(BACKWARD)
}

is the void closeTheWindow() that comes after the void loop stating an action to perform if CloseTheWindow is activated?

also how would i incorporate the last bit to open the window (reset the system)? is it in its own void loop or something?

cheers for the assistance, its greatly appreciated :slight_smile:

void loop() {
// read the input on analog pin 0:
int rainSensorValue = analogRead(A0);
if(rainSensorValue > 682)
{

Consistent placement of curly braces indicates that you know what you are doing. Even if you don't, it's important to appear as though you do. All { go on lines by themselves, in my book.

Proper indenting is important, too, in reading the code. Tools + Auto Format, if you can't indent as you go.

is the void closeTheWindow() that comes after the void loop stating an action to perform if CloseTheWindow is activated?

If CloseTheWindow is true, yes. The function takes no arguments, so don't try passing it one.

{
   while(digitalRead(limitSwitchPin) == HIGH) // second switch to open the window
      motor.run(BACKWARD)
}

All executable code, like this, must be in a function. You do not need to keep telling the motor to run backwards. It doesn't have Alzheimers.