Hi, I'm a newbie. I'm currently working on a project that requires me to make a robot that can cross a gap of 50 cm. It'll be like the first robot you'll see in this video: - YouTube .
Currently I'm stuck in a loop. The infrared sensor is at the bottom of the robot. I tried working around with leds before I move on to DC motors. When the distance is lower than 20 cm; led pin 13 turns on. When the distance is higher; led pin 12 turns on instead. However I only want this to occur once; then I need to drop the bridge by using servo. How can I stop it from happening more than once?
This is the code I use right now:
#include <DistanceGP2Y0A21YK.h>
DistanceGP2Y0A21YK Dist;
int distance;
void setup()
{
Serial.begin(9600);
Dist.begin(A0);
}
void loop()
{
distance = Dist.getDistanceCentimeter();
Serial.print("\nDistance in centimers: ");
Serial.print(distance);
delay(500); //make it readable
if(distance < 10)
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
}
else
{
digitalWrite(13, LOW);
delay (2000);
digitalWrite(12, HIGH);
delay (2000);
}
}
Thanks in advance!
GalanAleni:
How can I stop it from happening more than once?
You need to be clear about what 'it' is.
If 'it' is just to light one or other LED according to the distance, move your current code into setup so it only executes once.
If 'it' is to wait for the distance to increase above your threshold and then do some other action (but only do it once) then you need your sketch to remember whether the action has been taken yet. The simplest form of that would just be a boolean variable that you test before doing the action, and set when you take the action.
if((distance > threshold) and bridgeRaised)
{
lowerBridge();
bridgeRaised=false;
}
However, based on the problem you describe I suspect you'll find this is only the start of what will be a much longer sequence controlling the whole crossing process, and you would be better off using a finite state machine to control the whole sequence. Robin2 posted an excellent example sketch demonstrating a recommended way to implement finite state machines a few weeks ago, so I suggest you find and read that thread.
Hi GalanAleni
When the distance is higher; led pin 12 turns on instead. However I only want this to occur once;
Distance being higher means the sensor is over the gap? So that is when you want to drop the bridge?
I would keep the code in loop() because, I assume, you need to repeatedly check the sensor as your robot moves towards the bridge.
I would add a boolean variable which you set when you reach the bridge. Something like this ...
void loop()
{
static boolean atBridge = false; // Set to false first time in void loop()
if (!atBridge) // If not yet at bridge, check sensor
{
distance = Dist.getDistanceCentimeter();
Serial.print("\nDistance in centimers: ");
Serial.print(distance);
delay(500); //make it readable
if (distance < 10)
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
}
else // Distance >= 10 so now at bridge
{
atBridge = true; // Next time round loop(), distance measuring will not take place
digitalWrite(13, LOW);
delay (2000);
digitalWrite(12, HIGH);
delay (2000);
}
}
if (atBridge)
{
// Code to drop bridge
}
}
Hope this helps
Ray