期末作品_彈珠台 03161122_陳昱宏 0310755_郭芳蓁



// LAB8 - 讀取光敏電阻 (v1)
int photocellPin0 = 0;
int photocellPin1 = 1;
int photocellPin2 = 2;
int photocellPin3 = 3;
int photocellPin4 = 4;
int photocellPin5 = 5;
int photocellPin6 = 6;// 光敏電阻 (photocell) 接在 anallog pin 2
int photocellVal0 = 0;
int photocellVal1 = 0;
int photocellVal2 = 0;
int photocellVal3 = 0;
int photocellVal4 = 0;
int photocellVal5 = 0;
int photocellVal6 = 0;// photocell variable
int minLight = 50;
void setup() {
Serial.begin(9600);
}
void loop() {
// 讀取光敏電阻並輸出到 Serial Port
photocellVal0 = analogRead(photocellPin0);
photocellVal1 = analogRead(photocellPin1);
photocellVal2 = analogRead(photocellPin2);
photocellVal3 = analogRead(photocellPin3);
photocellVal4 = analogRead(photocellPin4);
photocellVal5 = analogRead(photocellPin5);
photocellVal6 = analogRead(photocellPin6);
if(photocellVal0<minLight){
//Serial.println("0"); //48
}
delay(100);
if(photocellVal1<minLight){
Serial.println("1"); //49
}
delay(100);
if(photocellVal2<minLight){
Serial.println("2"); //50
}
delay(100);
if(photocellVal3<minLight){
Serial.println("3"); //51
}
delay(100);
if(photocellVal4<minLight){
Serial.println("4"); //52
}
delay(100);
if(photocellVal5<minLight){
Serial.println("5"); //53
}
delay(100);
if(photocellVal6<minLight){
Serial.println("6"); //54
}
delay(100);
}
import processing.serial.*;
Serial serial;
int sensorValue;
PImage background;
PImage [] no = new PImage [7];
PImage [] yesRed = new PImage [7];
PImage [] yesGreen = new PImage [7];
int mX, mY;
boolean [] light = new boolean [7];
int number, rand;
int time = 5;
boolean [] green = new boolean [7];
boolean [] greenscore = new boolean [7];
PImage over;
int score;
void setup() {
size(533, 800);
serial = new Serial(this, "COM4", 9600);
over = loadImage("over.PNG");
for(int i=0; i<7; i++){
no[i] = loadImage("no.png");
yesRed[i] = loadImage("yes.png");
yesGreen[i] = loadImage("yesyes.png");
light[i] = false;
green[i] = false;
greenscore[i] = false;
}
mX = 0;
mY = 0;
score = 0;
}
void draw() {
background = loadImage("bg.png");
background(background);
for(int i=0; i<7; i++){
image(no[i], 10+66*i, 510, 30, 27);
}
if( serial.available() > 0) { // If data is available,
sensorValue = serial.read(); // read it and store it in val
println(sensorValue);
if(sensorValue>=48 && sensorValue<=55){
green[sensorValue-48] = true;
}
}
if(time>=0){
if(mX>=35 && mX<=75 && mY>=656 && mY<=695){
mX = 0;
mY = 0;
number = (int)random( 1, 6);
while(number>0){
rand = (int)random( 0, 7);
light[rand] = true;
number--;
}
}
for(int i=0; i<7; i++){
if(light[i] == true){
image(yesRed[i], 10+66*i, 510, 30, 27);
if(green[i] == true){
image(yesGreen[i], 10+66*i, 510, 30, 27);
if(greenscore[i] == false){
greenscore[i] = true;
score+=8;
}
}
}
}
textSize(50);
fill(255, 0, 0);
text(time, 150, 110);
text(score, 400, 110);
}
if(time<0){
image(over, 50, 370, 450, 53);
}
}
void mouseClicked(){
time--;
mX = mouseX;
mY = mouseY;
for(int i=0; i<7; i++){
light[i] = false;
green[i] = false;
greenscore[i] = false;
}
}
沒有留言:
張貼留言