Dear all, my Automated Crab Claw is almost working (see sketch below).
The one problem I have, is that sometimes it takes (way) too long to detect someone aproaching it, as a result of using "delay()".
Therefore, I thought about integrating the detection function in an interrupt routine.
However: the detection function (Blinking Led: blinking rate depending on detection distance) is also using delay() and in this case I can't find a way to work around this.
The attached picture shows what the random funtions are doing: this is working great.
But as soon as someone is within 200cm of distance (or closer to the sensor), "increase blinking rate depending on detection distance" should take over...
Here is the code, any help will be highly appreciated!
/*
As long as the Ping))) sensor doesn't detect anything at a distance closer than 200cm:
- a solenoid is randomly triggered for 1 to 10 times (easy snap sequence);
- when triggered, the solenoid stays powered for 0,04 seconds, than is turned off for a SHORT period, randomly between 0,05 to 1 second until the next trigger pulse
- after the last pulse of a sequence, the solenoid is turned off for a LONG period, randomly between 15 minutes (900 sec) and 60 minutes (3600 sec)
As soon as the Ping))) sensor detects an object at a distance of 200cm or closer:
- the trigger rate of the solenoid is changed depending on detection distance (the shorter the distance, the higher the trigger frequency)
- the minimum
Note: solenoid is connected to LedPin 13.
Ping))) instructions are based upon http://www.arduino.cc/en/Tutorial/Ping
(Sketch created by David A. Mellis 3 Nov 2008, modified 30 Aug 2011 by Tom Igoe)
*/
int pingPin = 2; // Connect SIG from the Ping))) sensor Arduino's Digital pin 2
int ledPin = 13; // Note: the solenoid is also connected to pin 13
int limitCm = 200; //
long randOffSHORT = 0;
long randOffLONG = 0;
long int duration, distanceInches, distanceCm;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // initialize the digital pin as an output.
}
void loop()
{
pinMode(pingPin, OUTPUT); // initialize the pingPin as an OUTPUT.
digitalWrite(pingPin, LOW);
delayMicroseconds(2); // a short LOW pulse is given beforehand, to ensure a clean HIGH pulse:
digitalWrite(pingPin, HIGH);
delayMicroseconds(5); // the PING))) is triggered by a HIGH pulse of 5 microseconds.
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT); // initialize the pingPin as an INPUT.
duration = pulseIn(pingPin, HIGH);
distanceInches = microsecondsToInches(duration);
distanceCm = microsecondsToCentimeters(duration);
checkLimit();
delay(100);
}
void checkLimit()
{
if (distanceCm > limitCm){ // randomised 'easy snap sequence' is activated
randOffLONG = random(900000, 3600000);
int blinkNumber = random(1, 11);
for (int a = 0; a < blinkNumber; a ++)
{
randOffSHORT = random(50, 1000); // Define length of random Off period, in this case between 0,05 and 1 second
digitalWrite(ledPin, HIGH); // when ledPin = HIGH, solenoid is activated
delay(40); // 40 millisec is minimum time needed, to pull solenoid core completely into its coil
digitalWrite(ledPin, LOW); // turn off LED, un-trigger Solenoid
delay(randOffSHORT); // wait for short random period
}
delay(randOffLONG); // wait for long random period
}
else // start of snap frequency that is depending on detection distance
{
Serial.println(distanceCm); // THIS IS THE FUNCTION I WOULD LIKE TO CALL USING AN INTERRUPT, "increase blinking rate depending on detection distance"
digitalWrite(ledPin, HIGH); // when ledPin = HIGH, solenoid is activated
delay(40); // 40 millisec is minimum time necessary to pull core completely into coil
digitalWrite(ledPin, LOW); // turn off LED, un-trigger Solenoid
delay(40); // safety delay, as 40 millisec is the shortest possible time between triggers
delay(distanceCm*distanceCm/20); // extra delay; will decrease quickly when approaching sensor
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
