S3191908 4Bs hybrid project
From Physical Programming
Pouring milk light
My idea was to simulate the motion of pouring milk through light effect, so I chose to use the tilt switch. While the 'light liquid' is poured out from the milk carton, the cup would light up.
[IMG]
[/IMG]
[IMG]
[/IMG]
[IMG]
[/IMG]
[IMG]
[/IMG]
code:
int tiltSwitch = 9; /* tilt switch connects to ground and pin 9 */ int led1 = 10; /* led 1 connects to pin 12 and ground */ int led2 = 11; /* led 2 connects to pin 11 and ground(blue cord)*/ boolean tiltState = true; boolean lastState = true;
void setup() {
pinMode(tiltSwitch, INPUT);
digitalWrite(tiltSwitch, HIGH);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
/* the tilt switch turns both leds on */
}
void loop(){
tiltState = digitalRead(tiltSwitch);
if (tiltState != lastState) {
if (tiltState) {
for(int i=0; i<=255; i++) {
analogWrite(led1, i);
analogWrite(led2, 255 - i);
delay(10);
}
/* while the tilt switch is on, LED 1 would fade off slowly and LED2 would fade on */
}
else {
for(int i=0; i<=255; i++) {
analogWrite(led2, i);
analogWrite(led1, 255 - i);
delay(10);
/* while tilt switch is turned off, LED 1 would fade on slowly and LED 2 would fade off*/
}
}
lastState = tiltState;
}
}

