Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Final code for class 4 of Emerging Objects
/*
* EMERGING OBJECTS
* CLASS 4 / PART 3
*
* +-----+
* | +-+ |
* 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);
boolean touch = false;
long total1;
long touchTimer;
// Setup the light sensor
int sensorPin = A0;
int sensorValue = 0;
boolean dark = false;
// Set up the LEDs
int arduinoLED = 13;
int Led0 = 11;
// Variable to keep track of the brightness of the LED on pin 11.
byte fade0 = 0;
// Variable to keep track of wether the arduinoLED is on or off.
boolean arduinoLEDState = LOW;
//Setup the logic
int state = 0;
/*
state 0 - not touched, not in darkness
state 1 - not touched, in darkness
state 2 - touched, not in darkness
state 3 - untouched, not in darkness
state 4 - touched, in darkness
state 5 - untouched, in darkness
*/
void setup() {
pinMode(arduinoLED, OUTPUT);
pinMode(Led0, OUTPUT);
Serial.begin(9600);
Serial.println("=======================");
}
void loop() {
// print variables to the serial port
debug();
switch (state) {
case 0:
fade0 = 0;
lightSense();
if ( dark == true ){
state = 1;
}
touchSense();
if ( touch == true ){
state = 2;
touchTimer = 100;
}
break;
case 1:
fade0 = 255;
lightSense();
if ( dark == false ){
state = 0;
}
touchSense();
if ( touch == true ){
state = 3;
touchTimer = 100;
}
break;
case 2:
fade0 = 255;
touchSense();
touchTimer--;
if ( touchTimer == 0 ){
state = 0;
}
if ( touch == false && touchTimer <= 90){
state = 3;
}
break;
case 3:
//Serial.println("pow");
touchTimer--;
if ( touchTimer == 0 ){
state = 0;
}
touchSense();
if ( touch == true ){
touchTimer = 100;
state = 4;
}
break;
case 4:
fade0 = 0;
touchTimer--;
if ( touchTimer == 0 ){
state = 0;
}
touchSense();
if ( touch == false && touchTimer <= 90){
state = 5;
}
break;
case 5:
touchTimer--;
if ( touchTimer == 0 ){
state = 0;
}
touchSense();
if ( touch == true ){
touchTimer = 100;
state = 2;
}
break;
}
// Light LEDs Section
displayRoutine();
}
void touchSense(){
long start = millis();
total1 = cs_4_2.capacitiveSensor(30);
if ( total1 >= 30){
touch = true;
}
else {
touch = false;
}
/*
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;
state = 1;
}
else {
dark = false;
}
}
void displayRoutine(){
digitalWrite(arduinoLED, arduinoLEDState); // turn the LED on or off
analogWrite(Led0, fade0);
}
void debug(){
Serial.print("lightsensor= ");
Serial.print(dark);
Serial.print(" | ");
Serial.print(sensorValue);
Serial.print("\t touchsensor= ") ;
Serial.print(touch);
Serial.print(" | ");
Serial.print(total1);
Serial.print("\t state= ");
Serial.print(state);
Serial.print("\t T= ");
Serial.println(touchTimer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment