Skip to content

Instantly share code, notes, and snippets.

@xseignard
Created April 16, 2015 14:51
Show Gist options
  • Save xseignard/7829a2922d506865ae13 to your computer and use it in GitHub Desktop.
Save xseignard/7829a2922d506865ae13 to your computer and use it in GitHub Desktop.
sharp 2y0a21
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