Skip to content

Instantly share code, notes, and snippets.

@azend
Created December 9, 2012 04:42
Show Gist options
  • Save azend/4243348 to your computer and use it in GitHub Desktop.
Save azend/4243348 to your computer and use it in GitHub Desktop.
Codeshield Example - Use the Force
/* Use the Force
Checks whether or not the user has the force by getting the light
value from photoresistor on the Codeshield and turns on the relay
if the user does in fact have the force. This code expects that
the relay is hooked into some other device to make a more resounding
force effect (such as a floor lamp).
The circuit:
* Photoresistor plugged into pin 5 (or soldered onto the Codeshield).
* Relay plugged into pin 2 (or soldered onto the Codeshield).
No other hardware is required other than what is already mounted
on the Codeshield.
created 2012
by Verdi R-D
This example code is in the public domain.
We are not affiliated with LucasArts or LucasFilms in any way.
We just like to make our childhood come to life.
http://www.diyode.com/codeshield/<some later define-able url>
*/
// Define useful names for the components
// that we'll use so that it is easier
// to remember later on
#define PHOTO A5
#define RELAY 2
// Create a global placeholder for
// storing the status of the force
boolean forceEnabled = false;
void setup () {
// Configure inputs and outputs
pinMode(PHOTO, INPUT); // Used to check the status of the force
pinMode(RELAY, OUTPUT); // Provide feedback on the status of the force
}
void loop () {
// Get the current state of the force
boolean currentForce = checkForce();
// Check if the current force is different
// from the last time we checked it
if ( currentForce != forceEnabled ) {
// If we have suddenly gotten the force
if ( currentForce ) {
enableForce();
}
// Otherwise, we have just lost the force
else {
disableForce();
}
}
// Delay to give the relay time to flip and the
// sensors to change
delay(50); // Don't check the force quickly young padawan
}
boolean checkForce() {
// Create a placeholder for storing the status of
// the force.
boolean force = false; // Default it to not having the force
// Check if the force is strong with this one
if (analogRead(PHOTO) < 511) {
force = true; // It is
}
// Return the status of the force
return force;
}
void enableForce() {
// The force has been enabled
// Turn on the force (the relay)
digitalWrite(RELAY, HIGH);
forceEnabled = true;
}
void disableForce() {
// The force has been disabled
// Turn off the force (the relay)
digitalWrite(RELAY, LOW);
forceEnabled = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment