Digital Input/Output

Part 1

Pretty straight forward stuff. Here’s the code with with pin 12

// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  12;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Part 2

All I did was add a couple codes of line and make the circuit as shown on fritzing except I used pin 5 instead of 4. Added a ledpin2. I added an addition digitalWrite(ledPin2, HIGH) when ledPin1 was low and vice versa. Here’s the code

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin1 =  3;      // the number of the LED pin
const int ledPin2 =  5;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);

}
else {
// turn LED off:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
}

Part 3

After understanding how this worked it was pretty fun. I added another two LEDs and made them turn on by delays in increments of 250 turn on and off in sequence. Here is my code.

// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin1 =  3;      // the number of the LED pin
const int ledPin2 =  5;      // the number of the LED pin
const int ledPin3 =  6;      // the number of the LED pin
const int ledPin4 =  7;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
delay(250);
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin3, HIGH);
delay(750);
digitalWrite(ledPin4, HIGH);
delay(1000);
}
else {
// turn LED off:
digitalWrite(ledPin1, LOW);
delay(250);
digitalWrite(ledPin2, LOW);
delay(500);
digitalWrite(ledPin3, LOW);
delay(750);
digitalWrite(ledPin4, LOW);
delay(1000);
}
}

Advertisements

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