Skip to content

Instantly share code, notes, and snippets.

@rupl
Last active August 29, 2015 14:03
Show Gist options
  • Save rupl/13f6efe882f4ffbaf0ff to your computer and use it in GitHub Desktop.
Save rupl/13f6efe882f4ffbaf0ff to your computer and use it in GitHub Desktop.
Modified light sensor tutorials for LilyPad Arduino USB. It only differs in that the LilyPad USB has a different (smaller) set of analog pins, so I used a different address, and increased the delay to a half second so it would be easier to read the numbers. Original tutorial is complements of Leah Buechley, found here: http://web.media.mit.edu/~…
/*
* LilyPad tutorial: sensing (sensors)
*
* Reads data from a LilyPad light sensor module
* and then sends that data to the computer
* so that you can see the sensor values
*
* Original: http://web.media.mit.edu/~leah/LilyPad/08_sensors.html
*/
int ledPin = 13; // LED is connected to digital pin 13
int sensorPin = 2; // light sensor is connected to analog pin 2 (A2)
int sensorValue; // variable to store the value coming from the sensor
void setup()
{
pinMode(ledPin, OUTPUT); // sets the ledPin to be an output
Serial.begin(9600); // initialize the serial port
digitalWrite(ledPin, HIGH); // turn the LED on
}
void loop() // run over and over again
{
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); // send that value to the computer
delay(500); // delay for 1/2 of a second
}
/*
* LilyPad tutorial: light sensor + LED indicator
*
* Reads data from a LilyPad light sensor module and then sends that data to the computer
* so that you can see the sensor values. It turns the internal LED on when the sensor
* detects LOW light, and turns the LED off when it detects HIGH light. Like a button!
*
* Original: http://web.media.mit.edu/~leah/LilyPad/08_sensors.html
*/
int ledPin = 13; // LED is connected to digital pin 13
int sensorPin = 2; // light sensor is connected to analog pin 2 (A2)
int sensorValue; // variable to store the value coming from the sensor
void setup()
{
pinMode(ledPin, OUTPUT); // sets the ledPin to be an output
Serial.begin(9600); // initialize the serial port
digitalWrite(ledPin, HIGH); // turn the LED on
}
void loop() // run over and over again
{
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); // send that value to the computer
// If the sensor detects a LOW amount of light, turn ON the LED.
// This makes the light sensor behave sort of like a button.
if (sensorValue < 100) {
digitalWrite(ledPin, HIGH);
}
// When a moderate amount of light is detected, turn OFF the LED.
else {
digitalWrite(ledPin, LOW);
}
// delay for 1/100 of a second
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment