Exercise 01 – Electric Magic Hat

Switch exercise

I hacked into a wireless light switch from As Seen on TV, and controlled it with Arduino

Construction:

Switch –> Arduino –> Relay –> Wireless Light Switch (parent)

Wireless Light Switch (child) –> Christmas Lights

Arduino code:

————————————————————————————–

int ledPin = 13; //relay-control pin
int inPin = 2; //read-in pin
int val = 0;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);
}

void loop()
{
val = digitalRead(inPin);  //read pin 7

delay(100);
if (val == 0)  //if pressed -> randomized flahing
{
digitalWrite(ledPin, HIGH);  //Light On
int TIME = random(50, 300);
delay(TIME);
digitalWrite(ledPin, LOW);  //Light Off
}
else
{
digitalWrite(ledPin, LOW);  //Light Off
}
}

Egress Code Hat

The egress hat allows one to walk around a building to ensure that there is adequate light on the floor for emergency egress (5 footcandles in most municipalities). The LDR sensor is on the top of the hat to ensure nothing blocks it, and the red LED is under the brim, easiliy visible to the wearer, indicating that the area is not in complience

 

Code:

int int LDR = 2;    // select the input pin for the potentiometer
int ledPin = 13;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor

void setup() {
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
}

void loop() {
  val = analogRead(LDR);    // read the value from the sensor
 if (val >= 500) {         // check if the input is HIGH (button released)
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(500);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(500);                  // waits for a second

  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }
           
}

Pants Switch

Imbedding  a switch into the button of a pair of pants.

The following code works in a circuit with two switches wired, but  I had some difficulty imbedding a switch within the zipper. The intent was to get a good estimate for when the user’s zipper or button is accidently left open.

 

/*

 * Pants Switch

 *

 * Reads two switches connected to the fly of a

   pair of pants, to approximate wether the user

   has accidently left his/her fly open. If fly is

   open (zipperDown) but the button is undone too,

   then the program assumes the user has conciously

   opened the fly. If only one is open, than the program

   assumes this is an accident. The program alerts the 

   user with a blinking LED.

 */

 

int buttonSwitchPin = 2;        // pants button switch connected to digital pin 2

int zipperSwitchPin = 3;        // pants zipper switch connected to digital pin 2

int ledPin = 13;                // LED connected to digital pin 13

 

boolean buttonUndone;           // state of the pants button

boolean zipperDown;             // state of the zipper

 

//long prevMillis = 0;            // stores the last millis value of the blinkLED function

long blinkInterval = 200;       // milliseconds per blink of the led

 

void setup(){                          // run once, when the sketch starts

  pinMode(buttonSwitchPin, INPUT);     // sets the digital pin as input

  pinMode(zipperSwitchPin, INPUT);     // sets the digital pin as input

  pinMode(ledPin, OUTPUT);             // sets the digital pin as output

 

}

 

void loop(){                           // run over and over again

  buttonUndone = (digitalRead(buttonSwitchPin) == HIGH);   // check if the pants button is open

  zipperDown = (digitalRead(zipperSwitchPin) == HIGH);     // check if the zipper is down

 

  if( buttonUndone != zipperDown){                         //check if the two states are dissimilar

    blinkLED();

  }

  else{

    digitalWrite(ledPin, LOW);   // sets the LED off

  }  

}

 

void blinkLED(){

  if(millis() % (2*blinkInterval) > blinkInterval){

    digitalWrite(ledPin, HIGH);

  }

  else{

    digitalWrite(ledPin, LOW);

  }

}

Blinking Beanie

Here, I put LED into the back of my beanie and made it blink whenever I pressed on a button that placed in the side of the beanie. The rate LED blinks increase and decreases as the button is pressed longer.

Code

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for a pushbutton)
int val = 0;                    // variable for reading the pin status
long randNumber;
int blinkDuration;
int decrease;
void setup() {
pinMode(ledPin, OUTPUT);      // declare LED as output
pinMode(inputPin, INPUT);     // declare pushbutton as input
blinkDuration = 1000;         // default blinking duration
decrease = 1;                 // indicator where 1 = blinking rate will decrease and 0 = blinking rate will increase
}

void loop(){
val = digitalRead(inputPin);  // read input value

if (val == HIGH) {            // check if the input is HIGH
digitalWrite(ledPin, LOW);  // turn LED OFF
} else {
if (decrease == 1) { //Decrease the blinking rate
if (blinkDuration <= 100) { //Next time loop comes around, increase blinking rate.
decrease = 0;
blinkDuration = 100;
}
else {
blinkDuration = blinkDuration – 50;
}
}
else { //Increase the blinking rate
if (blinkDuration >= 1000) { //Next time loop comes around, decrease blinking rate.
decrease = 1;
blinkDuration = 1000;
}
else {
blinkDuration = blinkDuration + 50;
}
}

digitalWrite(ledPin, HIGH);   // sets the LED on
delay(blinkDuration);                  // waits for a second

digitalWrite(ledPin, LOW);    // sets the LED off
delay(200);        // waits for a second

}
}

Party Underwear

Did you ever wonder how to turn a casual night into a fun game? Here’s the solution..



CIRCUIT

CODE


/*

 * Party Underwear

* by Andre Doria

*

* Turns on and off a light emitting diode(LED) connected to digital

* pin 13. The light is only turned on when the sensed ambient light

* is below a given number, obtained by analogRead() in pin 2 using

* a LDR

*/

int sensPin = 2;    // input pin for the LDR

int ledPin = 13;   // output pin for LEDs

int ambientLight = 0;       // variable to store the value coming from the sensor

void setup() {

pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT

}

void loop() {

delay(100);

ambientLight = analogRead(sensPin);    // read the value from the LDR sensor

if(ambientLight<380){                 // If its dark

digitalWrite(ledPin, HIGH);  // turn the ledPin on

}else{                           // If its bright

digitalWrite(ledPin, LOW);   // turn the ledPin off

 }
}

Red Sox victory hat

I updated a hat to blink a red LED when the wearer presses the “B,” and to turn it off when the “B” is pressed again.  This could used by a fan to display when his or her favorite team wins, which, since it’s the Red Sox, means the hat would be blinking a lot.

Code:

int inPin = 2;         // the number of the input pin
int ledPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long’s because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
pinMode(inPin, INPUT);
pinMode(ledPin, OUTPUT);
}

void loop()
{
reading = digitalRead(inPin);

// if the input just went from LOW and HIGH and we’ve waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() – time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}

digitalWrite(ledPin, state);  //Light or turn off LED based on state
delay(100);
digitalWrite(ledPin, LOW);    //turn LED off, causes blinking of state=HIGH
delay(100);

previous = reading;          //update previous to current reading
}

Bike helmet turn signal

It is dangerous to take your hands off of the handlebars when signaling a turn, so I designed a helmet-mounted turn signal than can be triggered by a small switch. The switch can be mounted on the handlebars or clipped to your wrist so that it dangles within easy reach of your fingers. The signal clips to the strap at the back of the helmet. Since there is not much distance between the left and right sides of the helmet, I used a strip of 6 LEDs that chase from left to right or vice-versa; this is similar to the system that some ambulances use.

 

Arduino board (center) wired to 3-way switch (top) and strip of 6 LEDs (left)

Arduino board (center) wired to 3-way switch (top) and strip of 6 LEDs (left)

Input is from a 3-way toggle switch; center position is “off” and makes no contact with any of the four pins, left and right position are used for left/right turn signal, and those positions make contact with the left and right pairs of contacts, respectively. Each pair of contacts is wired to the +5v rail of the breadboard and to a digital input pin, and those pins are also connected to ground through >1k ohm resistors. There is also a small potentiometer wired to an analog input pin from the middle (variable output) pin, while the other two pins on the pot are wired to ground and +5v, allowing the output pin to range from 0-5v.

The output to the 6 LEDs is through 6 digital pins, wired through 6 ~1k ohm resistors to the cathodes, while the anodes are wired to ground (would it be possible to use a single 1k ohm resistor between the junction of all the anodes and ground?) Also note that we extend the conductors to the LEDs using an 8-conductor CAT-5 cable with RJ-45 connectors. The jacks are wired to the breadboard at one end using 26-gauge solid-core jumpers which can be press-fit to the jack (no stripping required) and at the other end the LEDs cathodes are directly connected to the RJ-45 jack using the same press-fit connections. This does not give them much stability until they are pressed through the cardboard mounting strip (a more waterproof material would be a better long-term choice). The anodes are twisted together with a jumper which is attached to another pin on the RJ-45 jack; this connection should be strengthened with solder.

 

connections between LEDs, 3-way switch, and potentiometer and the Arduino's connectors

connections between LEDs, 3-way switch, and potentiometer and the Arduino

Arduino code


/* turn signal
ripple through a row of LEDs in one of two directions
speed (aka 1/slowness) is controlled by an analog input
*/
boolean debug = false; // show debug messages on serial output
int leftButtonPin = 2; // connect to left side of 3-way switch
int rightButtonPin = 3; // connect to right side of 3-way switch
int rowWidth = 6; // number of LEDs
int firstLedPin = 4; // lowest pin number of LEDs - connect to adjacent pins in ascending order
int knobPin = 4; // connected to variable pin of potentiometer knob

int rippleDirection = 1; // can be set to +/- 1 for left-to-right or right-to-left rippling, respectively
int currentRipplePos = -1; // can be set from 0-(rowWidth-1) to indicate currently "active" LED, or set to -1 to indicate no active LED
int leftButton; // value read from left button pin; should be LOW or HIGH
int rightButton; // value read from right button pin; should be LOW or HIGH
boolean lightState[6]; // zero-indexed array of booleans indicating whether LED at that index should be off (false) or on (true)
int slowness; // delay per cycle, in milliseconds; set by knob position, scaled between min and max
int minSlowness = 5; // min delay
int maxSlowness = 500; // max delay
boolean justSwitched = false; // flag for the reset actions at the beginning of a left- or right-moving cycle; reset when switch is set to "off"
unsigned long lastMoveTime = millis(); // reset each time LED position changes
int rawKnobPos; // analog value of knob pin

void setup(){
// set all the LED pins to LOW (off) and digital output mode
int i;
for (i = 0; i < rowWidth; i = i + 1) {
lightState[i] = false;
pinMode(firstLedPin + i, OUTPUT);
}
// for debugging output
Serial.begin(9600);
}

void loop(){
// read value of knob pin and set slowness to that value, scaled between min and max
rawKnobPos = analogRead(knobPin);
slowness = map(rawKnobPos, 0, 1024, minSlowness, maxSlowness);

// only show debug code when needed, as it adds a delay and limits the min slowness
if (debug == true){
Serial.print("current pos: ");
Serial.print(currentRipplePos);
Serial.print(" direction: ");
Serial.print(rippleDirection);
Serial.print(" rawKnobPos: ");
Serial.print(rawKnobPos);
Serial.print(" slowness: ");
Serial.print(slowness);
}

// read button pins and make LEDs ripple if switch is not "off"
leftButton = digitalRead(leftButtonPin);
rightButton = digitalRead(rightButtonPin);
if ((rightButton == HIGH || leftButton == HIGH)){

if (leftButton ==HIGH){
if (debug == true){Serial.println(" left button on");}
rippleDirection = 1; // ripple from left to right
if (justSwitched == true){ // setup steps for the first cycle after switch moves from off to left
currentRipplePos = 0; // start with the left-most LED
justSwitched = false; // don't run setup steps again
lastMoveTime = millis(); // reset delay timer
}
} else {
if (debug == true){Serial.println(" right button on");}
rippleDirection = -1; // ripple from left to right
if (justSwitched == true){ // setup steps for the first cycle after switch moves from off to right
currentRipplePos = rowWidth - 1; // start with the right-most LED
justSwitched = false; // don't run setup steps again
lastMoveTime = millis(); // reset delay timer
}
}
// if switch is in "off" position
} else {
currentRipplePos = -1; // no active LED
justSwitched = true; // ready to run setup steps nect time switched on
// set all LEDs to LOW (off)
int j;
for (j = 0; j lastMoveTime + slowness){
// reset timer
lastMoveTime = millis();
// only make LEDs ripple if they are not set to off (position = -1)
if (currentRipplePos != -1){
// toggle the led at the current position; on->off or off->on
if (lightState[currentRipplePos] == false){
lightState[currentRipplePos] = true;
} else {
lightState[currentRipplePos] = false;
}

// if the position is at the end of the row, go to other end
if (currentRipplePos == (rowWidth - 1) && rippleDirection == 1){
currentRipplePos = 0;
} else if (currentRipplePos == 0 && rippleDirection == -1){
currentRipplePos = rowWidth - 1;
} else {
currentRipplePos = (currentRipplePos + rippleDirection);
}

}
// update the LED pins with the new values so the lights go blinky-blinky
int k;
for (k = 0; k < rowWidth; k = k + 1) {
if (lightState[k] == true){
digitalWrite(firstLedPin + k, HIGH);
} else {
digitalWrite(firstLedPin + k, LOW);
}
}
}

}