Created
July 27, 2015 12:01
-
-
Save gregorykelleher/707dd256d137452d68e2 to your computer and use it in GitHub Desktop.
Proximity Sensor w/o delay()
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
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