Wednesday, October 7, 2009

Joe Cohen's 3 In-Class Exercises

#1


/* Code by Wm. Joe Cohen for
Physical Computing Studio */

int ledPin = 5;
int photoPin = 0;
int photoValue = 0;
int lightValue = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(photoPin, INPUT);
Serial.begin(9600);
}

void loop() {
photoValue = analogRead(photoPin);
lightValue = photoValue;
//lightValue = photoValue / 4;
Serial.println(lightValue);
analogWrite(ledPin, lightValue);
delay(100);
}

______________________________________________________

#2


/* Code by Wm. Joe Cohen for
Physical Computing Studio */

int timer = 50;
int ledPins[] = {
7, 6, 5, 4, 3 };
int pinCount = 5;
int photoPin = 0;
int photoValue = 0;


void setup() {
int thisPin;
for (int thisPin = 0; thisPin <>
pinMode(ledPins[thisPin], OUTPUT);
}
pinMode(photoPin, INPUT);
Serial.begin(9600);
}


void loop() {

photoValue = analogRead(photoPin);

// lighter environment (values of 30+) moves the LED flow one way
if(photoValue >= 30) {
for (int thisPin = 0; thisPin <>
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
Serial.println(photoValue);
}
}


// darker environment (values of 29-) moves the LED flow the opposite way
if(photoValue <= 29) {
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
Serial.println(photoValue);
}
}
}

______________________________________________________

#3


/* Code by Wm. Joe Cohen for
Physical Computing Studio */

int timer = 50;
int ledPins[] = {
7, 6, 5, 4, 3 };
int pinCount = 5;
int photoPin = 0;
int photoValue = 0;

void setup() {
int thisPin;
for (int thisPin = 0; thisPin <>
pinMode(ledPins[thisPin], OUTPUT);
}
pinMode(photoPin, INPUT);
Serial.begin(9600);
}

void loop() {

photoValue = analogRead(photoPin);
int newTimer1;
int newTimer2;

// lighter environment (values of 30+) moves the LED flow one way
// and the LEDs move 10 times slower than 'time'
if(photoValue >= 30) {
for (int thisPin = 0; thisPin <>
newTimer1 = timer * 10;
digitalWrite(ledPins[thisPin], HIGH);
delay(newTimer1);
digitalWrite(ledPins[thisPin], LOW);
Serial.println(photoValue);
}
}


// darker environment (values of 29-) moves the LED flow the opposite way
// and the LEDs move 2 times faster than 'time'
if(photoValue <= 29) {
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
newTimer2 = timer / 2;
digitalWrite(ledPins[thisPin], HIGH);
delay(newTimer2);
digitalWrite(ledPins[thisPin], LOW);
Serial.println(photoValue);
}
}
}

No comments:

Post a Comment