Go Down

Topic: Delaying digitalread command (Read 153 times) previous topic - next topic

zac1999

I am making a metal detecting car that will stop when it senses metal. I have a metal detector connected to an input of the Arduino and when it detects metal, the relay switches off the DC motor. I have encountered a problem though. Once the metal is detected the dc motor turns off which is all good, but as soon as the code starts the loop again, the car will not move as the same bit of metal is detected. How can I put some sort of timer on the digitalRead so it will only read the pin once the car has moved from the first bit of metal that was detected?

Code: [Select]

void loop() {
  // put your main code here, to run repeatedly:
  metalDetected=digitalRead(detectorPin);
  if (metalDetected==LOW) //not detected, assuming active low
  {
  digitalWrite(relayPin, HIGH); //drive the car
  }
  else //it's HIGH, metal is detected
  {
  digitalWrite(relayPin, LOW); //stop the car
  digitalWrite(LED, HIGH); //LED will turn on
  digitalWrite(buzzer, HIGH); //buzzer will activate
  delay(5000); //car will stop for 5 seconds
  digitalWrite(buzzer, LOW); buzzer will turn off
 digitalWrite(LED, LOW); LED will turn off
  }
}

Delta_G

Pseudo:
Code: [Select]


boolean checkingForMetal = true;
unsigned long lastFoundMetal = 0;

if(checkingForMetal){
   if( you found some metal){
       stop
       checkingForMetal = false;
       lastFoundMetal = millis();
}

if( you are moving && millis() - lastFoundMetal > someInterval){
     checkingForMetal = true;
}



If at first you don't succeed, up - home - sudo - enter.

zac1999

I don't really understand that code, how would I add it into my original?

Delta_G

#3
Mar 16, 2017, 12:44 am Last Edit: Mar 16, 2017, 12:45 am by Delta_G
I don't really understand that code, how would I add it into my original?
It's not code, it's pseudo code.  It's just to give you the idea of using a variable to keep track of whether or not you should be looking for metal.  It's not something you can "put into your code".  It's just something to think about.

For the part with millis(), see any tutorial on "Blink Without Delay"  there are way too many for me to repeat it here.

If it is some other part that confuses you, then say which part.

If at first you don't succeed, up - home - sudo - enter.

zac1999

As I am only a beginner in programming, could you give me an example of that in my code?

liudr

As I am only a beginner in programming, could you give me an example of that in my code?
Describe YOUR desired outcome with a sample situation (describe in detail). Your first attempt at explaining what you want and what you actually get were mixed together. If you describe what you want clearly, someone will provide more suitable answers for beginners.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

zac1999

I am making a metal detecting car that drives in an outwards circular motion. When it detects metal, the car will stop. The metal detector is active low. When no metal is detected the relay is activated, causing the DC motor to be on. So when i power the arduino the relay is active. When the car goes over a piece of metal and is detected, the relay will turn off, stopping the dc motor. However, at the moment the car will not move from the first piece of metal as it is continuously sensing that same piece of metal- causing a loop that keeps the car from moving. I want the car to detect the metal, stop for 5-10 seconds (with buzzer and green LED on) and then have the relay turn on again, moving the car.
Here is my full code

Code: [Select]

byte relayPin=2;
byte detectorPin=7;
byte buz=4;
byte gLED=5;
byte rLED=3;
bool metalDetected;


void setup() {
  // put your setup code here, to run once:
pinMode(rLED, OUTPUT);
pinMode(gLED, OUTPUT);
pinMode(buz, OUTPUT);
pinMode(relayPin, OUTPUT);
//we'll assume the detector is active low and needs a pullup
//detector pin normally high, goes low when detects something
pinMode(detectorPin, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  metalDetected=digitalRead(detectorPin);
  if (metalDetected==LOW) //not detected, assuming active low
  {
  digitalWrite(relayPin, HIGH); //drive the car
  digitalWrite(rLED, HIGH); // turn red LED on
  }
  else //it's HIGH, metal is detected
  {
  digitalWrite(relayPin, LOW); //stop the car
  digitalWrite(buz, HIGH); //buzzer will turn on
  digitalWrite(gLED, HIGH); //green LED turns on
  delay(5000); //5 second delay
  digitalWrite(buz, LOW); //turn off buzzer
  digitalWrite(gLED, LOW); //turn green LED off
 
  }
}

MorganS

Why 5-10? Should it stop longer for bigger/closer pieces of metal?

You have to be very specific. Pick a number. It can change after testing shows it needs to change.

I would actually do it with some simple delays: after stopping and buzzing for 5 seconds, engage the motors for 5 seconds without testing for metal.
GoForSmoke: "What GShield? You never mentioned a shield."

liudr

Am I correct that the relay cuts power to your arduino besides motor? If yes, you need to defeat that and let arduino control power to the motor.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

zac1999

The relay cuts power to the dc motor. The arduino controls the relay through its signal pin.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy