Skip to content

Instantly share code, notes, and snippets.

@tomconte
Created July 12, 2014 12:50
Show Gist options
  • Save tomconte/ab041e5af66d8d6d5942 to your computer and use it in GitHub Desktop.
Save tomconte/ab041e5af66d8d6d5942 to your computer and use it in GitHub Desktop.
Read various sensor values from an Esplora board and print them to serial port.
/*
** Read various sensor values and print them to serial port.
**
** Thomas Conté @tomconte
*/
#include <Esplora.h>
int lightMin = 1023; // minimum sensor value
int lightMax = 0; // maximum sensor value
boolean calibrated = false; // whether the sensor's been calibrated yet
void setup()
{
Serial.begin(9600); // initialize serial communications with your computer
}
void loop()
{
if (Esplora.readButton(1) == LOW) {
calibrate();
}
int celsius = Esplora.readTemperature(DEGREES_C);
int loudness = Esplora.readMicrophone();
int light = Esplora.readLightSensor();
Serial.print("T:");
Serial.print(celsius);
Serial.print("_");
Serial.print("M:");
Serial.print(loudness);
Serial.print("_");
Serial.print("L:");
Serial.print(light);
Serial.println();
// wait a second before reading again:
delay(1000);
}
// From EsploraLightCalibrator sample
void calibrate() {
// tell the user what do to using the serial monitor:
Serial.println("While holding switch 1, shine a light on the light sensor, then cover it.");
// calibrate while switch 1 is pressed:
while(Esplora.readButton(1) == LOW) {
// read the sensor value:
int light = Esplora.readLightSensor();
// record the maximum sensor value:
if (light > lightMax) {
lightMax = light;
}
// record the minimum sensor value:
if (light < lightMin) {
lightMin = light;
}
// note that you're calibrated, for future reference:
calibrated = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment