Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define trigPin 11
- #define echoPin 10
- #define led LED_BUILTIN
- #define movePin 8
- #define ledPin 9
- void setup() {
- Serial.begin (9600);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(movePin, INPUT);
- pinMode(led, OUTPUT);
- pinMode(ledPin, OUTPUT);
- }
- void loop() {
- long duration, distance;
- digitalWrite(trigPin, LOW); // Added this line
- delayMicroseconds(2); // Added this line
- digitalWrite(trigPin, HIGH);
- // delayMicroseconds(1000); - Removed this line
- delayMicroseconds(10); // Added this line
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance = (duration/2) / 29.1;
- if (distance < 4) { // This is where the LED On/Off happens
- // ruch
- int val = digitalRead(movePin); // read input value
- if (val == HIGH) {
- Serial.println("OTWIERANIE!!xd");
- digitalWrite(ledPin, HIGH);
- }
- digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
- }
- else {
- digitalWrite(led,LOW);
- }
- if (distance >= 200 || distance <= 0){
- Serial.println("Out of range");
- }
- else {
- Serial.print(distance);
- Serial.println(" cm");
- }
- delay(500);
- digitalWrite(ledPin, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement