Making Things Interactive

February 28, 2008

3: LadyAda Tutorials

Filed under: 3: LadyAda Tutorials — jmarshman @ 11:27 pm

Documentation of tutorials (unable to post until now):

Simple circuit, three LED’s

Simple switch

Alternating switch

Alternating switch w/ changing 3 LED’s


/*
 Alternating switch
 */

int switchPin = 2;              // switch is connected to pin 2
int ledPin = 13;                // led is connected to pin 13
int val;                        // variable for reading the pin status
int buttonState;                // variable to hold the last button state

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(ledPin, OUTPUT);      // set the led pin as an output
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val

  if (val != buttonState) {          // the button state has changed
    if (val == LOW) {                // check if the button is pressed
      Serial.println("Button just pressed");
      digitalWrite(ledPin, LOW);
    } else {                         // the button is not pressed
      Serial.println("Button just released");
      digitalWrite(ledPin, HIGH);
    }
  }

  buttonState = val;                 // save the new state in our variable
}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

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

Blog at WordPress.com.

%d bloggers like this: