Making Things Interactive

February 5, 2008

Switch/Counter 3 Way Light

Filed under: 4: Counting Sensor Input,Assignments,Christopher Bridgman — cbridgma @ 12:32 am

So, originally, I wanted to do this with a tilt sensor, but due to time, I was unable to get one. So what I did was create a switch that would toggle between 4 different light settings. I could imagine taking a light with a tilt sensor and programming this to light up each mode to a different angle (this kicked my butt enough!) I know to some people, this would be easy, but I had a hard time. It was really only because i tried doing it on my own instead of trying to look things up on the Ladyada site. That did wonders for me. But any way. Here is a video of the final product and code.



int redPin = 12;                  // Red LED connected to digital pin 12
int greenPin = 11;                // Green LED connected to digital pin 11
int bluePin = 10;                   // Blue LED connected to digital pin 10
int switchPin = 9;                   // Swtitch Connected to digital pin 9                        
int val;
int val2;
int buttonState;
int lightMode = 0;

void setup()                      // run once, when the sketch starts
{
  pinMode(redPin, OUTPUT);        // sets the digital pin as output
  pinMode(greenPin, OUTPUT);      // sets the digital pin as output
  pinMode(bluePin, OUTPUT);       // sets the digital pin as output
  pinMode(switchPin, INPUT);      // sets the digital pin as input
  
  buttonState = digitalRead(switchPin);
}

void loop()                       // run over and over again
{
  val = digitalRead(switchPin); //Read the pin
  delay(10);
  val2 = digitalRead(switchPin);
  if (val == val2)
  {
    if (val !=buttonState)
    {
      if (val == LOW)
      {
        if (lightMode == 0) {          // if its off
          lightMode = 1;               // turn red light on
        } else {
          if (lightMode == 1) {        // if red light on
            lightMode = 2;             // turn green light on
          } else {
            if (lightMode == 2) {      // if green light on
              lightMode = 3;           // turn blue light on
            } else {
			  if (lightMode == 3) { //  if blue light on 
                lightMode = 0;           // turn light off!

}}}}}}
        buttonState = val;
}
  if (lightMode == 0) {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
  }
  
 
  if (lightMode == 1) {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
  }


  if (lightMode == 2) {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, LOW);
  
  
  }
  if (lightMode == 3) {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, HIGH);
  }
}
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

Create a free website or blog at WordPress.com.

%d bloggers like this: