|
Intro to Physical Computing Syllabus Research & Learning Other Class pages
ITP Help Pages |
First Week
Code from class
//first, let's name the pins, so we can refer to them later by name
#define LEDPin0 5
#define LEDPin1 6
//setup is where we initialize things
void setup(){
//here we are declaring LEDPin0 & LEDPin1 as outputs
pinMode(LEDPin0, OUTPUT);
pinMode(LEDPin1, OUTPUT);
}
//loop is your main program, it does this over and over and over again
void loop(){
//Turn one led on, and turn the other off
digitalWrite(LEDPin0, HIGH);
digitalWrite(LEDPin1, LOW);
//wait a moment, or 250 milliseconds (1/4 of a second)
delay(250);
//Turn one led off, and turn the other on
digitalWrite(LEDPin0, LOW);
digitalWrite(LEDPin1, HIGH);
//wait again before repeating
delay(250);
}
//first, let's name the pins, so we can refer to them later by name
#define LEDPin 5
#define SwitchPin 2
//setup is where we initialize things
void setup(){
//here we are declaring the LEDPin as an output (it will light up)
pinMode(LEDPin, OUTPUT);
//here we declare the SwitchPin as an input (it listens for a digital on/off)
pinMode(SwitchPin, INPUT);
}
//loop is your main program, it does this over and over and over again
void loop(){
//an if statement is a conditional, saying "if this thing is
//[true, greater than, not equal to...], then do this other thing"
//in English, we would say "read the value on the SwitchPin. If the value is equal to 1...
if(digitalRead(SwitchPin)==1) {
//turn the LEDPin on
digitalWrite(LEDPin, HIGH);
}
else{ //if there is a different condition (ex. the SwitchPin == 0), do this instead
//turn the LED off
digitalWrite(LEDPin, LOW);
}
}
|