-
-
Save blinkinglight/30e84d3b5b91a304151a221df87e6c0a to your computer and use it in GitHub Desktop.
This file contains 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
// only 1 sensor to test | |
const int led = 8; | |
const int echo = 3; | |
const int trig = 2; | |
const long wait = 1000; //cooldown time for the sensor | |
unsigned long last = 0; | |
int spots = 4; // the number im trying to update each time the sensors are triggered | |
int distance; | |
long duration; | |
void setup() { | |
Serial.begin (9600); | |
pinMode(trig, OUTPUT); // Sets the trigPin as an Output | |
pinMode(echo, INPUT); // Sets the echoPin as an Input | |
pinMode(led, OUTPUT); | |
} | |
bool s1state; | |
int logs1distance; | |
void loop() { | |
unsigned long timePassed = millis(); | |
// Write a pulse to the HC-SR04 Trigger Pin | |
digitalWrite(trig, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trig, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trig, LOW); | |
// Measure the response from the HC-SR04 Echo Pin | |
duration = pulseIn(echo, HIGH); | |
// Determine distance from duration | |
// Use 343 metres per second as speed of sound | |
distance = (duration / 2) * 0.0343; | |
// Send results to Serial Monitor | |
if (distance <= 20 && s1state == false) { | |
s1state = true; | |
last = timePassed; | |
logs1distance = distance; | |
} | |
if (timePassed - last >= wait){ // im trying to have a 1 second delay here without using the delay function soo that 2 sensors could be triggered at once | |
last = timePassed; | |
everysecond(); | |
} | |
} | |
void everysecond() { | |
if(s1state == true) { | |
s1state = false; | |
Serial.print(logs1distance); | |
Serial.println(" cm"); | |
digitalWrite (led, HIGH); | |
last = millis(); | |
spots += 1; | |
Serial.print(spots); | |
Serial.println(" Spots Left"); | |
} else { | |
digitalWrite (led, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment