Pages: [1]   Go Down
Author Topic: Simple DC motor + IR sensor  (Read 866 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
On no soul doth God Place a burden greater than it can bear. It gets every good that it earns, and it suffers every ill that it earns.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Croatia
Offline Offline
God Member
*****
Karma: 13
Posts: 513
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Offline Offline
Full Member
***
Karma: 5
Posts: 113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you need a flag smiley-wink
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
Logged

For whom does the clock pulse? It pulses for you!

Offline Offline
Newbie
*
Karma: 0
Posts: 4
On no soul doth God Place a burden greater than it can bear. It gets every good that it earns, and it suffers every ill that it earns.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: