Created
June 26, 2015 16:14
-
-
Save StefKors/0a7c4806d772367d5fd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
Servo myServo; // maakt het servo object zodat we die kunnen besturen | |
int servoPin = 9; // de pin waar de servo op is aangesloten | |
int pos = 90; // maakt een variabele om de positie van de servo op te slaan | |
long randNumber; | |
boolean scared = false; | |
#define trigPin 13 | |
#define echoPin 12 | |
#define led 11 | |
#define led2 10 | |
void setup() { | |
Serial.begin (9600); | |
myServo.attach(servoPin); // zegt op welke pin myServo is aangesloten. | |
pinMode(trigPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
pinMode(led, OUTPUT); | |
pinMode(led2, OUTPUT); | |
} | |
void loop() { | |
long duration, distance; | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
duration = pulseIn(echoPin, HIGH); | |
distance = (duration / 2) / 29.1; | |
Serial.print(pos); | |
Serial.print(" - "); | |
Serial.print(distance); | |
Serial.print(" - "); | |
if (distance < 100) { // de afstand voor aan/uit | |
digitalWrite(led, HIGH); // als de afstand groter is gaat het rode lampje aan | |
digitalWrite(led2, LOW); | |
randNumber = random(0, 2); | |
Serial.print(randNumber); | |
if (!scared) { | |
if (randNumber != 0) { | |
pos = 30; | |
} else { | |
pos = 150; | |
} | |
scared = true; | |
} | |
} else { | |
pos = 90; | |
scared = false; | |
} | |
//pos = max(0, min(180, pos)); | |
myServo.write(pos); // draai de servo naar positie pos | |
Serial.println(); | |
delay(200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment