prototype testing
1:10 scale model of an aqua yüzey
/*
stalkerguard.pde – shake if something comes near
(c) kimo karvinen & tero karvinen http://botbook.com
updated 2010 – joe saavedra; based on code by david a. mellis and tom igoe
*/
boolean enableLED = true; // LED
const int ledPin2 = 10;
const int ledPin = 9;
const int pingPin = 6;
const int motorPin = 5;
long int duration, distanceInches, distanceCm;
int limitCm = 15;
int ledValue = 0;
void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop()
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
digitalWrite(ledPin2, HIGH);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
distanceInches = microsecondsToInches(duration);
distanceCm = microsecondsToCentimeters(duration);
checkLimit();
delay(100);
checkLED();
delay(100);
}
void checkLimit()
{
if (distanceCm > limitCm){
digitalWrite(motorPin, HIGH);
} else {
digitalWrite(motorPin, LOW);
}
}
void checkLED()
{
if (distanceCm < limitCm){
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}