Skip to content

Instantly share code, notes, and snippets.

@gregorykelleher
Created July 27, 2015 12:01
Show Gist options
  • Save gregorykelleher/707dd256d137452d68e2 to your computer and use it in GitHub Desktop.
Save gregorykelleher/707dd256d137452d68e2 to your computer and use it in GitHub Desktop.
Proximity Sensor w/o delay()
int sensorPin = 0;
int ledPin = 4;
int ledState = LOW;
unsigned long previousMillis = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
int val = analogRead(sensorPin);
val = map(val, 0, 700, 500, 20);
val = constrain(val, 20, 500);
Serial.println(val);
long interval = val;
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment