Wednesday, October 7, 2009

John's 10/7 Homework

Assignment #1

Video:


Code:

//This code determines the amount of time an LED is lit depending on the amount
// of light a photocell picks up

//We begin by setting a few different int variables. First the LED pin, which is 3,
// the button pin, which is 7, and then the pin for photocell, which is 0
int ledPin = 3;
int buttonPin = 7;
int photoPin = 0;

//Then we set the value of the photocell and the button state, both of which
// will be 0 for the time being. We also set the value of the last button state
// to LOW, meaning the button is not being pressed
int photoValue = 0;
int buttonState = 0;
int lastButtonState = LOW;

// Next we set up some variables which will be used for the debounce. These
// are long's, not int's, because they are going to be measuring milliseconds
// which will quickly become to big to be ints. The debounce delay is set to 50
// for no specific reason other than that it works best
long lastDebounceTime = 0;
long debounceDelay = 50;

//Now we begin the setup, this only occurs once
void setup() {
//In the set up, the LED pin mode is set to OUTPUT while the button and photo
// pin modes are set to INPUT
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(photoPin, INPUT);
//This next bit of code simply allows the print and println's to work
Serial.begin(9600);
}


//Now we begin the loop, this occurs over and over again
void loop() {
//At the beginning of the loop we create a new int, light time (which will
// be the amount of time the LED is lit) and we set it equal to the photo value
// times 100 (so that it will light long enough for us to see it)
int lightTime = photoValue * 100;
//We then digitally read a new int called reading as the button pin
int reading = digitalRead(buttonPin);
//The button state is then also digitally read as the button pin
buttonState = digitalRead(buttonPin);
//We now begin an if statement, which only will occur is the button is pressed
if(buttonState == HIGH) {
//In this case, the photo value is analog read as the photo pin
photoValue = analogRead(photoPin);
//We print the photo value to the serial monitor so we can track changes
Serial.println(photoValue);
}
//These other if statements are for the debounce
//If the reading does not equal the last button state (low)...
if (reading != lastButtonState) {
// ... the last debounce time will be set to the current time
lastDebounceTime = millis();
}
//If the current time minus the last debounce time is greater than the
// debounce delay...
if((millis() - lastDebounceTime) > debounceDelay) {
// ... then the button state is set equal to the reading
buttonState = reading;
}
//Finally, at the end of the loop, the LED pin is digitally written to be
// equal to the value of the button state
digitalWrite(ledPin, buttonState);
//There is then a delay equal to the light time
delay(lightTime);
// and the LED is turned off (LOW)
digitalWrite(ledPin, LOW);
//The last button state is then saved as the reading for the next loop around
lastButtonState = reading;
}

Assignment #2

Video:


Code:

//This code lights up 3 LED's using if statements and a for loop

//Begin by setting 5 ints, three for LED pins (13, 10, and 7) and one for the
// button pin (3) as well as for the button state, which determines whether
// or not the button is pressed.
int ledPinOne = 13;
int ledPinTwo = 10;
int ledPinThree = 7;
int buttonPin = 3;
int buttonState = 0;

//Setup the pin modes of the 3 LED's to OUTPUT and the button pin to INPUT
void setup(){
pinMode(ledPinOne, OUTPUT);
pinMode(ledPinTwo, OUTPUT);
pinMode(ledPinThree, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
//Digitally read the button pin and set it equal to the button state
buttonState = digitalRead(buttonPin);
//Set up an IF statement which determines if the button is pressed.
if(buttonState == HIGH) {
//Set up a for loop which loops a 20 times
for (int i = 0; i <= 20; i++) {
//Digitally write the LED pins a bunch of times to make a neat light show
digitalWrite(ledPinOne, HIGH);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, HIGH);
delay(800);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, LOW);
delay(800);
digitalWrite(ledPinOne, HIGH);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, HIGH);
delay(800);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, LOW);
delay(800);
digitalWrite(ledPinOne, HIGH);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, HIGH);
delay(800);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
delay(800);
digitalWrite(ledPinOne, HIGH);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
delay(600);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, LOW);
delay(600);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, HIGH);
delay(600);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, LOW);
delay(600);
digitalWrite(ledPinOne, HIGH);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
delay(800);
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
delay(800);
}

}
// If the button is not pressed, set the LED to low (off).
else {
digitalWrite(ledPinOne, LOW);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, LOW);
}
}

Assignment #3

Video:


Code:

//This code lights up one LED using if statements and a for loop

//Begin by creating 3 int variables. One for the LED connected to pim 13,
// one for the button connected to pin 2, and one for the button state which
// determines whether or not the button is pressed.

int ledPin = 13;
int buttonPin = 2;
int buttonState = 0;

// Next setup; setting the pin mode of the LED pin to OUTPUT, and the pin
// mode of the button pin to INPUT.

void setup(){
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {

//Digitally read the button pin and set it equal to the button state
buttonState = digitalRead(buttonPin);

//Set up an IF statement which determines if the button is pressed.
if (buttonState == HIGH) {
//Set up a for loop which loops a hundred times
for (int i = 0; i > 100; i++) {
//First digitally write the LED pin as high, turning it on
digitalWrite(ledPin, HIGH);
//Then delay for whatever the counter, i, is equal to
delay(i);
//Then digitally write the LED pin as low, turning it off
digitalWrite(ledPin, LOW);
//Then delay for whatever the counter, i, is equal to
delay(i);
}
}

// If the button is not pressed, set the LED to low (off).
else {
digitalWrite(ledPin, LOW);
}
}

No comments:

Post a Comment