Computational Craft Midterm
I plan to make a lamp that could work like a sandglass but instead of sand, it’s light that moves in it.
Arduino Code:
const int tiltswitch = 4;
const int ledTop1 = 3;
const int ledTop2 = 11;
const int ledBottom1 = 9;
const int ledBottom2 = 6;
// Pin left to right 3 11 9 6 which is top to bottom
int switch1 = 0;
int switch2 = 0;
int side;
int light[4] = {0, 0, 0, 0};
//int light2 =0;
//int light3 =0;
//int light4 =0;
int lightspeed =2;
void setup() {
// put your setup code here, to run once:
pinMode(tiltswitch, INPUT_PULLUP);
pinMode(ledTop1, OUTPUT);
pinMode(ledTop2, OUTPUT);
pinMode(ledBottom1, OUTPUT);
pinMode(ledBottom2, OUTPUT);
Serial.begin(9600);
}
void loop() {
switch2 = digitalRead(tiltswitch);
Serial.print(switch1);
Serial.print(“,”);
Serial.println(side);
if ((switch1 == 0) && (switch2 == 1)) {
side = 1;
}
// side1 upwards
if ((switch1 == 1) && (switch2 == 0)) {
side = 0;
}
//side0 upwards
// digitalWrite(ledTop1, HIGH);
// delay(500);
// digitalWrite(ledTop1, LOW);
// digitalWrite(ledTop2, HIGH);
// delay(500);
// digitalWrite(ledTop2, LOW);
// digitalWrite(ledBottom1, HIGH);
// delay(500);
// digitalWrite(ledBottom1, LOW);
// digitalWrite(ledBottom2, HIGH);
// delay(500);
// digitalWrite(ledBottom2, LOW);
// delay(500);
// pin test
if (side == 0) {
analogWrite(ledTop1, light[0]);
analogWrite(ledTop2, light[1]);
analogWrite(ledBottom1, light[2]);
analogWrite(ledBottom2, light[3]);
light[0] += lightspeed*0.5;
light[3] -= lightspeed;
if (light[0] >= 50) {
light[1] += lightspeed*0.5;
light[2] -=lightspeed;
}
numbercheck();
} ;
if (side == 1) {
analogWrite(ledTop1, light[0]);
analogWrite(ledTop2, light[1]);
analogWrite(ledBottom1, light[2]);
analogWrite(ledBottom2, light[3]);
light[3] += lightspeed*0.5;
light[0] -= lightspeed;
if (light[3] >= 50) {
light[2] += lightspeed*0.5;
light[1] -= lightspeed;
}
numbercheck();
}
switch1 = switch2;
// set last loop
delay(100);
// make stady
}
void numbercheck() {
for(int i = 0; i <= 3; i++) {
if (light[i] >= 150) {
light[i] = 150;
}
if (light[i] <= 0) {
light[i] = 0;
}
}
}