The purpose of our first assignment in physical computing was to become familiar with circuits, the arduino microcontroller and building circuits on a breadboard.
To begin I translated the circuit to the breadboard. Afterward I compiled and uploaded the program to the arduino microcontroller.
End result: When the switch is off, the red led is on. When the circuit is switched on, the red led turns off and the yellow led turns on.
Switch Off
Switch On
// declare variables:
int switchPin = 2; // digital input pin for a switch
int yellowLedPin = 3; // digital output pin for an LED
int redLedPin = 4; // digital output pin for an LED
int switchState = 0; // the state of the switch
void setup() {
pinMode(switchPin, INPUT); // set the switch pin to be an input
pinMode(yellowLedPin, OUTPUT); // set the yellow LED pin to be an output
pinMode(redLedPin, OUTPUT); // set the red LED pin to be an output
}
if (switchState == 1) {
// if the switch is closed:
digitalWrite(yellowLedPin, HIGH); // turn on the yellow LED
digitalWrite(redLedPin, LOW); // turn off the red LED
}
else {
// if the switch is open:
digitalWrite(yellowLedPin, LOW); // turn off the yellow LED
digitalWrite(redLedPin, HIGH); // turn on the red LED
}
}
Physical Computing – Week 1
The purpose of our first assignment in physical computing was to become familiar with circuits, the arduino microcontroller and building circuits on a breadboard.
To begin I translated the circuit to the breadboard. Afterward I compiled and uploaded the program to the arduino microcontroller.
End result: When the switch is off, the red led is on. When the circuit is switched on, the red led turns off and the yellow led turns on.
Switch Off
Switch On
// declare variables:
int switchPin = 2; // digital input pin for a switch
int yellowLedPin = 3; // digital output pin for an LED
int redLedPin = 4; // digital output pin for an LED
int switchState = 0; // the state of the switch
void setup() {
pinMode(switchPin, INPUT); // set the switch pin to be an input
pinMode(yellowLedPin, OUTPUT); // set the yellow LED pin to be an output
pinMode(redLedPin, OUTPUT); // set the red LED pin to be an output
}
void loop() {
// read the switch input:
switchState = digitalRead(switchPin);
if (switchState == 1) {
// if the switch is closed:
digitalWrite(yellowLedPin, HIGH); // turn on the yellow LED
digitalWrite(redLedPin, LOW); // turn off the red LED
}
else {
// if the switch is open:
digitalWrite(yellowLedPin, LOW); // turn off the yellow LED
digitalWrite(redLedPin, HIGH); // turn on the red LED
}
}
PComp-Wk1-Video from Steve Aquillano on Vimeo.