This is a new version of the evil spider. I’ve replaced the LED in the head with two smaller LEDs in the eyes. This way the brightening is easier to see.
Some close-up photos are at the end of this post – if you fear spiders, scroll slowly down to the image with breadboard 😉
(BTW: at 00:10 you can see Daniel)
The source code is rather simple:
/* Spider - sketch for spider control * * 12. Apr 2012 danimath created * 02. May 2012 danimath added blinking * **********************************************************************/ byte usPin = 8; // ultrasound pin unsigned long usDuration; // ultrasound duration unsigned int cm; // roughly calculated cm byte ledPin = 3; // LED pin int brightness; // LED brightness int min_threshold = 15 ; // treshold for blinking LED const int max_threshold = 100; // treshold for brightning LED const int mesDelay = 250; // msec between measurements //#define DEBUG /* setup - initialise things * **********************************************************************/ void setup () { #ifdef DEBUG Serial.begin (9600); #endif analogWrite (ledPin, 255); usDuration = getDist (); // warm up measurement delay (2500); analogWrite (ledPin, 0); } /* loop - the ever lasting loop * **********************************************************************/ void loop () { usDuration = getDist (); cm = usDuration / 58; // int is enough for this #ifdef DEBUG Serial.print ("usDuration: "); Serial.println (usDuration); #endif if (cm < min_threshold) { if (brightness == 0) brightness = 255; else brightness = 00; } else if (cm < max_threshold) brightness = map (cm, 0, max_threshold, 200, 0); else brightness = 0; analogWrite (ledPin, brightness); delay (mesDelay); } /* getDist - gets the distance from SRF05 * * return value: micro seconds * **********************************************************************/ unsigned long getDist () { unsigned long retVal; pinMode (usPin, OUTPUT); digitalWrite (usPin, LOW); delayMicroseconds (2); digitalWrite (usPin, HIGH); delayMicroseconds (10); digitalWrite (usPin, HIGH); pinMode (usPin, INPUT); retVal = pulseIn (usPin, HIGH); #ifdef DEBUG Serial.print (retVal); Serial.print (", "); Serial.print (retVal / 58.0); Serial.println (" cm"); #endif return (retVal); }
The hardware is just as simple as the software. This image is created with Fritzing. Of course I didn’t use a breadboard in the final construction.
As promised here are the close-ups:
(Repost from my old blog, 2012-05-03)