Digital I/O: Box Switch

I made a switch (SPST) from the small box my Arduino charger came in.  When the box is closed, the switch returns 1.  When the box is open, the switch returns 0.

The step/reset and counter programs are demonstrated in the YouTube video.  My second switch was a large toggle switch that I bought.

I probably should have used a sturdier box for my switch, or made it activate by flattening it to one side or the other.  Also, I learned that you can’t upload to the Arduino while digital ports 0 and 1 are connected to sensors.

Advertisements

2 Responses to “Digital I/O: Box Switch”

  1. Program for step/reset:

    /*
    Assignment 3 program, part 2
    Three LEDs are operated from pins 2-4
    The LEDs start blinking on 2.5 seconds, off 2.5 seconds
    While the Step switch (a box) is closed, the LED blink rate is increased
    Whenever the Reset switch (a toggle switch) is closed, the blink rate is reset to 2.5 seconds
    */

    //sensors
    int boxPin = 8;
    int togglePin = 9;
    //leds
    int led0 = 3;
    int led1 = 4;
    int led2 = 2;
    //keep track of LED state
    int ledsOn = HIGH;
    //counter variable for LED blink rate
    int counter = 1;

    void setup(){
    pinMode(boxPin, INPUT);
    pinMode(togglePin, INPUT);
    pinMode(led0, OUTPUT);
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    }
    void loop(){
    //get sensor values
    int boxClosed = digitalRead(boxPin);
    int switchOn = digitalRead(togglePin);

    //speed up blinking if counter switch on (the box is closed)
    if(boxClosed == 1){
    counter++;
    }
    //reset counter if reset switch on (the toggle is on)
    if(switchOn == 1){
    counter = 1;
    }
    //change LED state highlow
    if(ledsOn == HIGH){
    ledsOn = LOW;
    }
    else{
    ledsOn = HIGH;
    }
    digitalWrite(led0,ledsOn);
    digitalWrite(led1,ledsOn);
    digitalWrite(led2,ledsOn);

    //wait time based on counter
    delay((int) 2500 / counter);
    }

  2. /*
    Assignment 3 program, part 3
    Three LEDs are operated from pins 2-4
    The LEDs represent a binary counter
    */

    //sensors
    int boxPin = 8;
    int togglePin = 9;
    //leds
    int led0 = 3;
    int led1 = 4;
    int led2 = 2;

    void setup(){
    pinMode(boxPin, INPUT);
    pinMode(togglePin, INPUT);
    pinMode(led0, OUTPUT);
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    }
    void loop(){
    //get sensor values
    int boxClosed = digitalRead(boxPin);
    int switchOn = digitalRead(togglePin);

    if(boxClosed == HIGH){
    digitalWrite(led0,HIGH);
    digitalWrite(led1,HIGH);
    if(switchOn == HIGH){
    digitalWrite(led2,HIGH);
    }
    else{
    digitalWrite(led2,LOW);
    }
    }
    else{
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    if(switchOn == HIGH){
    digitalWrite(led0,HIGH);
    }
    else{
    digitalWrite(led0,LOW);
    }
    }
    delay(10);
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: