Skip to content

Instantly share code, notes, and snippets.

@solon
Created February 7, 2011 15:07
Show Gist options
  • Select an option

  • Save solon/814501 to your computer and use it in GitHub Desktop.

Select an option

Save solon/814501 to your computer and use it in GitHub Desktop.
Arduino program to divide an analog sensor into a number of discrete ranges and trigger a different WAV file when the sensor value enters one of the ranges.
// these are constants that won't change while the code is running
const int lowerBound = 0;
const int threshold1 = 350;
const int threshold2 = 700;
const int upperBound = 1024;
// these variables will change
int sensorValue = 0;
int lastRange = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(0);
if (lowerBound <= sensorValue && sensorValue < threshold1) {
if (lastRange != 1) {
Serial.println("1.wav");
lastRange = 1;
}
}
else if (threshold1 <= sensorValue && sensorValue < threshold2) {
if (lastRange != 2) {
Serial.println("2.wav");
lastRange = 2;
}
}
else if(threshold2 <= sensorValue && sensorValue <= upperBound) {
if (lastRange != 3) {
Serial.println("3.wav");
lastRange = 3;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment