My project seems pretty simple, but I'm extremely new to programming so it's proving to be tough! My setup is a distance sensor and a button. When someone comes in range of the distance sensor, I have the time (in ms) print to the serial monitor. The same thing occurs with the button. My issue is that the distance is constantly sensing, so it prints a massive amount of numbers in a short period of time. So, is there a way to have the distance sensor print just one number, even if it continues to detect the persons standing in front of it? Thank you for any advice!
const int button1Pin = 2;
const int ledPin = 13;
int distanceSensor; //distance values are 0 to 1023
int button1State;
unsigned long currentTime;
void setup() {
pinMode(button1Pin, INPUT);
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop() {
button1State = digitalRead(button1Pin);
distanceSensor = analogRead(2);
if (distanceSensor > 200){
digitalWrite(ledPin, HIGH);
currentTime = millis(); //milliseconds
if (button1State == LOW){
Serial.println(currentTime);
delay(1000);
//if (button1State == LOW){
// digitalWrite(resetpin, LOW);
//}
}
}else{
digitalWrite(ledPin, LOW);
}
}
[code]