Created
September 6, 2013 17:01
-
-
Save arielchuri/6466625 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* EMERGING OBJECTS | |
* CLASS 4 / PART 2 | |
* +-----+ | |
* | +-+ | | |
* TOUCH | | | | | |
* SENSOR| +---+ | |
* +-----+ | |
* ARDUINO | | |
* +-----------+ | | |
* | GND|-----------------+ | | |
* | | | | | |
* | 13|-[R220]--[LED>]--O | | |
* | 12| | | | |
* | ~11|-[R220]--[LED>]--+ | | |
* | ~10| | | |
* +--[PHOTO]--|5V ~9| | | |
* | |GND 8| | | |
* O---[R10K]--|GND 7| | | |
* | | ~6| | | |
* +-----------|A0 ~5| | | |
* |A1 4|----[R1M]-----------O | |
* |A2 ~3| | | |
* |A3 2|--------------------+ | |
* |A4 1| | |
* +-----------+ | |
* | |
*/ | |
// Setup the touch sensor. | |
#include <CapacitiveSensor.h> | |
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); | |
// Setup the light sensor | |
int sensorPin = A0; | |
int sensorValue = 0; | |
boolean dark = false; | |
// Set up the LEDs | |
int debugLed = 13; | |
int Led0 = 11; | |
// Set up the fading | |
byte fade; | |
// Set up the blinking | |
long previousMillis = 0; | |
boolean blinkLedState = LOW; | |
long interval = 1000; // interval at which to blink (milliseconds) | |
void setup() { | |
pinMode(debugLed, OUTPUT); | |
pinMode(Led0, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
// Light Sensor Section | |
lightSense(); | |
//Check the touch sensor | |
touchSense(); | |
// blink LED section | |
blinkLED(); | |
// Light LEDs Section | |
displayRoutine(); | |
} | |
void touchSense(){ | |
long start = millis(); | |
long total1 = cs_4_2.capacitiveSensor(30); | |
/* | |
Serial.print(millis() - start); // check on performance in milliseconds | |
Serial.print("\t"); // tab character for debug windown spacing | |
Serial.println(total1); // print sensor output 1 | |
delay(10); // arbitrary delay to limit data to serial port | |
*/ | |
} | |
void lightSense(){ | |
sensorValue = analogRead(sensorPin); | |
if ( sensorValue <= 400 ) { | |
dark = true; | |
} | |
else { | |
dark = false; | |
} | |
} | |
void blinkLED(){ | |
//Blink section | |
// check to see if it's time to blink the LED; that is, if the | |
// difference between the current time and last time you blinked | |
// the LED is bigger than the interval at which you want to | |
// blink the LED. | |
unsigned long currentMillis = millis(); | |
if(currentMillis - previousMillis > sensorValue/4) { | |
// save the last time you blinked the LED | |
previousMillis = currentMillis; | |
// if the LED is off turn it on and vice-versa: | |
blinkLedState = !blinkLedState; | |
} | |
} | |
void displayRoutine(){ | |
digitalWrite(debugLed, blinkLedState); // turn the LED on or off | |
analogWrite(Led0, fade); | |
/* | |
Serial.print("timer= "); | |
Serial.print(timer); | |
Serial.print("\t previousTimer= ") ; | |
Serial.print(previousTimer); | |
Serial.print("\t fade= "); | |
Serial.println(fade); | |
*/ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment