Go Down

Topic: Simple DC motor + IR sensor (Read 911 times) previous topic - next topic

Nauman

Hello guys,
i am making my project in which i am using SHARP 2D120X IR sensor which stops the 12V DC motor (using with L293D), once it detects any obstacle and marks the surface with DENSO sprayer. Sorry for silly questions but i am new to Arduino UNO, and just making some codes after alot of reading.
Basically my code works but i want to stop the sprayer after 1st time it runs. no matter for how long the IR senses the obstacle.
My code is as under:

int motor1 = 13;
int motor2 = 12;
int pump1 = 11;
int pump2 = 10;

int sensorPin = 0;
int sensorVal;
void setup()
{               
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(pump1, OUTPUT);
pinMode(pump2, OUTPUT);
}
void loop()
{
 
  digitalWrite(pump1, LOW);
  digitalWrite(pump2, LOW);
  digitalWrite(motor1, HIGH);
  digitalWrite(motor2, LOW) ; 
  sensorVal = analogRead(sensorPin);
  if(sensorVal < 500)
  {
  digitalWrite(motor1,HIGH);
  }
  else
  {
   digitalWrite(motor1,LOW);
   delay(500);
   digitalWrite(pump1, HIGH);
   delay(3000);
   digitalWrite(pump1, LOW);
   delay(500);
  }
}



After the detection the sprayer(pump) runs again and again until i don't remove the obstacle. How can i write a code in which i can get advantage by taking analog input and using its value to manage my actions.
thanks. and sorry for bad english.

Shpaget

Use [ code] tags for your code. It makes life prettier, and grumpy people less so.

Your sprayer sprays continuously because loop loops over and over again. You need to remember that you have sprayed and not spray again.

AWOL

OP, To save you the embarrassment of explaining why you didn't read the prominent thread at the top of this section, I've deleted your identical thread on this same subject in another section.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0AlphaOmega

you need a flag ;)
It's like a paint spray that say's...." I've sprayed"

boolean sprayFlag = LOW;

only spray if the flag is low, when you spray, set the flag high.

Some other criteria will set it low again
For whom does the clock pulse? It pulses for you!

Nauman

@ 0AlphaOmega
Sir, can you tell me a sample code for this! It would be appreciate able.
and thanks for the reply

Go Up