03 Switch and Blink – Matt Morrill

I fashioned my circuit for part 1 out of a stapler and electrical tape.

Here is the video demonstrating the other circuits:

Finally, my code for the circuits in the video:

1 Toggle:

/*
Turns 2 LEDs on and off based on a switch
*/

int switchPin = 2;
int led1Pin = 8;
int led2Pin = 9;

void setup()
{
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(switchPin, INPUT);
}

void loop()
{
digitalWrite(led1Pin, digitalRead(switchPin));
digitalWrite(led2Pin, digitalRead(switchPin));
}

2 Toggle:

/*
Turns 2 LEDs on and off with 2 switches
*/

int switch1Pin = 2;
int switch2Pin = 3;
int led1Pin = 8;
int led2Pin = 9;

void setup()
{
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(switch1Pin, INPUT);
pinMode(switch2Pin, INPUT);
}

void loop()
{
if(digitalRead(switch1Pin) == HIGH)
{
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
}
if(digitalRead(switch2Pin) == HIGH)
{
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
}
}

Blink Speed:

/*
Controls the blink speed of 2 LEDs with a 2 switches
*/

int switch1Pin = 2;
int switch2Pin = 3;
int led1Pin = 8;
int led2Pin = 9;
int delaySpeed;

void setup()
{
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(switch1Pin, INPUT);
pinMode(switch2Pin, INPUT);
delaySpeed = 300;
}

void loop()
{
if(digitalRead(switch1Pin) == HIGH)
{
delaySpeed-=100;
if(delaySpeed<100)
delaySpeed=100;
}
if(digitalRead(switch2Pin) == HIGH)
{
delaySpeed+=100;
}
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
delay(delaySpeed);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
delay(delaySpeed);
}

Advertisements

~ by mmorrill on February 8, 2010.

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: