• tumblr
  • youtube
  • mail

Links to final NIGHTLIGHT

2 downloadable videos,

one in the light,

IMG_2298.MOV

one in the dark

IMG_2299.MOV

I found inspiration in this video:

and this was the code inputted to the Arduino:

// Set up capacitive sensor

#include <CapacitiveSensor.h> // the capacitive sensor library

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4, 2); //setup pins 4 and 2 for the sensor. 2 has the probe.

long touchThreshold = 1000; // What the touch sensor must pass to register a touch.

// Set up the photo sensor

int darkState;

int darkThreshold = 200;

// Set up LED display

int ledState; // Determines which frame of the animation.

int animeSpeed = 8000; // Speed at which the LEDs change frames.

int maxBrightness = 0; // Maximum brightness for LEDs. Used for fading on and off the light.

int onoffSpeed = 2; // Speed to turn the lights on/off.

int ledsVal[] = {0, 0, 0, 0, 0, 0}; // The brightness value of all of the pins.

int ledsMax[] = {1000, 1000, 1000, 1000, 1000, 1000}; // What each LED will fade up to.

int ledsMin[] = {5, 5, 5, 5, 5, 5}; // What each LED will fade down to.

int ledsAttack[] = {20,20,20,20,20,20}; // Each LED can have its own increase speed

int ledsDecay[] = {20,20,20,20,20,20}; // and decrease

int ledsDir[] = {0, 0, 0, 0, 0, 0};

/*

* You can set a behavior for an LED.

* 0 = fade out or stay off.

* 1 = fade off and on starting upwards.

* 2 = fade off and on starting downwards.

* 3 = fade up or stay on.

* 4 = stay at current value.

* Example:

* ledsVal[2] = 127;

* ledsDir[2] = 3;

* These two lines would set the LED on pin 6 to medium brightness and tell it to fade up to max brightness.

*/

// Variables for writing to the LEDs. You should not need to change these:

int leds[] = { 3, 5, 6, 9, 10, 11}; // array of all the pwm pins

int fadeSpeed = 1; // length of time for when to change the fade out of the led

int fadePreviousMillis;

int pinCount = 6; // how many pwm pins

int ledPin = 13; // use the Arduino’s built-in led for debugging

int prevLedTime; // a variable for the fade timing

// Variables for logic

int i; // a variable later use in the logic

int prevTime; // a variable for the logic timing

int interval = 500; // length of time for logic time (unused so far)

int touched; // Just touched?

int stateVar; // State of the light

boolean lightVar; // Lights on?

void setup() {

  for (int thisPin = 0; thisPin < pinCount; thisPin++) { // set the 6 led pins to output

    pinMode(leds[thisPin], OUTPUT);

  }

  pinMode(ledPin, OUTPUT); // set the debug led to output

randomSeed(analogRead(1)); // used for the random function in one of the animations.

  Serial.begin(9600);

  Serial.println();

// debug();

}

void loop() {

darkSense();

  touchSense(); // call the function “touchSense” and put the result in touched

  logic(); // call the function “logic”

  anime02(); // call the function “fade”

  updateLED(); // changes the leds based on the commands you set in anime()

// debug();

}

next tab

display:

int anime02() {

    int currentTime = millis(); // check the time

    if (currentTime – prevLedTime >= animeSpeed) { //if enough time has gone by:

        if (ledState > 2) ledState = 0;

        Serial.println(ledState);

        switch (ledState) {

            case 0:

                for (int i = 0; i < 6; i++) {\

                    ledsMax[i] = 1000 / 6 * i;

                    ledsAttack[i] = 20;

                    ledsDecay[i] = 20;

                    ledsDir[i] = 3;

                }

                animeSpeed = 500;

                break;

            case 1:

                for (int i = 0; i < 6; i++) {

                    ledsMax[i] = 1000;

                    ledsDir[i] = 1;

                }

                animeSpeed = 1000;

                break;

            case 2:

                for (int i = 0; i < 6; i++) {

                    ledsAttack[i] = 10;

                    ledsDecay[i] = 10;

                }

                ledsDir[0] = 3;

                ledsDir[1] = 0;

                ledsDir[2] = 3;

                ledsDir[3] = 0;

                ledsDir[4] = 3;

                ledsDir[5] = 0;

                animeSpeed = 1000;

                break;

        }

        prevLedTime = currentTime;

        ledState++;

    }

}

int debug() { // receive a variable when called and put it into “x”

    Serial.print(“stateVar =\t”);

    Serial.print(stateVar);

    Serial.print(“\ttouched =\t”);

    Serial.print(touched);

    Serial.print(“\tlightVar =\t”);

    Serial.print(lightVar);

    Serial.print(“\tdarkVar =\t”);

    Serial.print(darkState);

    Serial.println();

}

/*

   int display() { // send the ledFade amount to each of the leds

   for (int n = 0; n < 6; n++) {

   analogWrite(leds[n], ledsVal[n]);

   }

   }

*/

int updateLED() {

    switch (lightVar) { // Check to see if the lights should be on or off.

        case (0):

            if (maxBrightness > 0) maxBrightness = maxBrightness – onoffSpeed;

            maxBrightness = max(maxBrightness, 0);

            break;

        case (1):

            if (maxBrightness < 255) maxBrightness = maxBrightness + onoffSpeed;

            maxBrightness = min(maxBrightness, 255);

            break;

    }

    unsigned long currentMillis = millis();

    if (currentMillis – fadePreviousMillis >= fadeSpeed) {

        for (int i = 0; i < pinCount; i++) {

            switch (ledsDir[i]) {

                case 0: // stay at 0 or fade down to ledsMin

                    if (ledsVal[i] > ledsMin[i]) {

                        ledsVal[i] = ledsVal[i] – ledsDecay[i];

                        ledsVal[i] = max(ledsVal[i], ledsMin[i]);

                    }

                    break;

                case 1: // fading up and down – up

                    ledsVal[i] =ledsVal[i] + ledsAttack[i];

                    if (ledsVal[i] >= ledsMax[i]) {

                        ledsDir[i] = 2;

                        ledsVal[i] = ledsMax[i];

            }

                    break;

                case 2: // fading up and down – down

                    ledsVal[i] = ledsVal[i] – ledsDecay[i];

                    if (ledsVal[i] <= ledsMin[i]) {

                        ledsDir[i] = 1;

                        ledsVal[i] = ledsMin[i];

                    }

                    break;

                case 3: // stay at 1000 or fade up to 1000

                    if (ledsVal[i] < ledsMax[i]){

                        ledsVal[i] = ledsVal[i] + ledsAttack[i];

                        ledsVal[i] = min(ledsVal[i], ledsMax[i]);

                    }

                    break;

                default:

                    Serial.println(“default”);

                    break;

            }

            int x = map(ledsVal[i], 0, 1000, 0, maxBrightness);

            analogWrite(leds[i], x);

            fadePreviousMillis = currentMillis;

        }

           // Serial.println(ledsVal[1]);

    }

}

logic:

int logic() {

    switch (stateVar) {

        case (0): // daytime and lights are off

            lightVar = 0;

            digitalWrite(ledPin, LOW);

            if (touched == 1) {

                lightVar = !lightVar;

                stateVar = 1;

                Serial.println(“State 1”);

                touched = 2;

            } else if (darkState == 1) {

                stateVar = 2;

                Serial.println(“State 2”);

            }

            break;

        case (1): // touched

            digitalWrite(ledPin, HIGH);

            if (touched == 1) {

                stateVar = 0;

                Serial.println(“State 0”);

                touched = 2;

            }

            break;

        case (2): // night time lights on

            lightVar = 1;

            if (touched == 1) {

                lightVar = !lightVar;

                stateVar = 1;

                Serial.println(“State 1”);

                touched = 2;

            } else if (darkState == 0) {

                stateVar = 0;

                Serial.println(“State 0”);

            }

    }

}

sense:

int touchSense() { // a function to check the touch sensor and return the result

    long start = millis();

    long touchVar =  cs_4_2.capacitiveSensor(30);

    delay(10);

    switch (touched) {

        case 0:

            if (touchVar >= touchThreshold) touched = 1;

            break;

        case 1:

            break;

        case 2:

            if (touchVar < touchThreshold) touched = 0;

            break;

    }

}

int darkSense() {

    int darkVar = analogRead(A0);

//    Serial.println(darkVar);

    if (darkVar < darkThreshold) {

        darkState = 1;

    } else {

        darkState = 0;

    }

}

Leave a reply

Skip to toolbar