Tuesday, October 6, 2009

Joe Cohen | 8/7/09 | Parts 1, 2 & 3

#1

video

*In this setup, the microcontroller reads light via photocell. This light is converted into a number which is used as part of a value to determine the length of time the LED stays lit. The less amount of light, the less amount of time the LED stays lit (often does not light at all, especially with those fat zero values). The more amount of light, the brighter the LED will be.

/* Code by Wm. Joe Cohen for
Physical Computing Studio
Due 10/6/09 | Part I */

int ledPinA = 3;
int buttonPin = 8;
int buttonState = 0;
int lastButtonState = LOW;
int photoPin = 0;
int photoValue = 0;

long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {

pinMode(ledPinA, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(photoPin, INPUT);
Serial.begin(9600);
}

void loop() {
int lightTime = photoValue * 100;
int reading = digitalRead(buttonPin);
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH) {
photoValue = analogRead(photoPin);
Serial.println(photoValue);
}
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
}
digitalWrite(ledPinA, buttonState);
delay(lightTime);
digitalWrite(ledPinA, LOW);
lastButtonState = reading;
}

I have set values to my variables for an LED, button, and photocell. Debounce time and delay is initialized as long. In the setup, the LED pin is set to output, the button pin is set to input, the photocell pin is set to input, and a serial print reading is established. Now for the loop. A variable 'lightTime' is created to hold the values that are being input from the photocell x 100. The state of the button is read. When the button is pressed, the values from the photocell are read and printed in the serial monitor. A debounce is thrown in to prevent double takes of input. The light turns on from the fact that the button is pressed. The high values keep the LED lit for longer and the low values allow for a short amount of time or none at all.
__________________________________________________

#2

video

/* Code by Wm. Joe Cohen for
Physical Computing Studio
Due 10/6/09 | Part II */

int ledPinA = 2;
int ledPinB = 3;
int ledPinC = 4;
int buttonPin = 8;
int buttonState = 0;


void setup() {

pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
pinMode(ledPinC, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {

buttonState = digitalRead(buttonPin);
if(buttonState == HIGH) {

for (int x = 0; x<100;>
//lights go one way
digitalWrite(ledPinA, HIGH);
delay(x);
digitalWrite(ledPinA, LOW);
delay(x);
digitalWrite(ledPinB, HIGH);
delay(x);
digitalWrite(ledPinB, LOW);
delay(x);
digitalWrite(ledPinC, HIGH);
delay(x);
//lights go the other way
digitalWrite(ledPinC, LOW);
delay(x);
digitalWrite(ledPinB, HIGH);
delay(x);
digitalWrite(ledPinB, LOW);
delay(x);
digitalWrite(ledPinA, HIGH);
delay(x);
digitalWrite(ledPinA, LOW);
delay(x);
}
}
delay(1);
}

I have set values to my variables for 3 LEDs on separate digital pins, as well as a button pin. In the setup, all the LEDs are set to output and the button is used for input. Loop time! The state of the button is read. If the button is pressed, all 3 LEDs light up with delays according to an incrimental variable 'x'. The lights bounce back and forth, as they get slower and slower. The loop is delayed for 1 millisecond and waits for the next time the button is pressed.
__________________________________________________

#3

video


/* Code by Wm. Joe Cohen for
Physical Computing Studio
Due 10/6/09 | Part III */

int ledPin = 3;
int buttonPin = 8;
int buttonState = 0;
int lastButtonState = LOW;

long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {

pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
int reading = digitalRead(buttonPin);
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH) {
for(int x = 0; x<100;>
digitalWrite(ledPin, HIGH);
delay(x/2);
digitalWrite(ledPin, LOW);
delay(x*2);
digitalWrite(ledPin, HIGH);
delay(x);
digitalWrite(ledPin, LOW);
delay(x);
}
}
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
}
digitalWrite(ledPin, buttonState);
lastButtonState = reading;
}

I have set values to my variables for 1 LED, a button, and debounce longs. In the setup, the LED is set as output and the button is set as input. The loop begins. The state of the button is read. If the button is pressed, the LED starts to pulse according to delays set to an incremental variable called 'x' along with minor alterations in the first two delays (division and multiplication). The code is debounced.

1 comment: