Created
April 16, 2015 14:51
-
-
Save xseignard/7829a2922d506865ae13 to your computer and use it in GitHub Desktop.
sharp 2y0a21
This file contains hidden or 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 IR = A0; | |
| const int MIN_MEASURE = 10; | |
| const int MAX_MEASURE = 80; | |
| void setup() { | |
| Serial.begin(9600); | |
| pinMode(IR, INPUT); | |
| } | |
| void loop() { | |
| Serial.println(computeValue()); | |
| delay(100); | |
| } | |
| int computeValue() { | |
| // convert it to centimeters | |
| float cm = pow(3027.4/analogRead(IR), 1.2134); | |
| // if lower than the MIN_MEASURE, convert it to MIN_MEASURE | |
| if (cm < MIN_MEASURE) cm = MIN_MEASURE; | |
| // if greater to the MAX_MEASURE, convert it to MAX_MEASURE | |
| if (cm > MAX_MEASURE) cm = MAX_MEASURE; | |
| return cm; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment