顯示具有 week15 標籤的文章。 顯示所有文章
顯示具有 week15 標籤的文章。 顯示所有文章

2017年12月25日 星期一

week15

作期末作業
Arduino程式碼

// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);
}

void loop() {
  int w=digitalRead(SW_pin);
  int x=analogRead(X_pin);
  int y=analogRead(Y_pin);
  Serial.write(w);
  Serial.write(x/4);
  Serial.write(y/4);
  /*
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");*/
  delay(50);
}

Processing程式碼

import processing.serial.*;
Serial myPort;

void setup(){
  size(200,200);
  myPort = new Serial(this, "COM7", 115200);
}
int w, x=128, y=128;
void draw(){
  background(255);
  while(myPort.available() > 0 ){
    w=myPort.read();
    x=myPort.read();
    y=myPort.read();
  }
  ellipse(x,y, 20,20);
}

Week 15 邱

#第15週

期末作品:皮卡丘打排球-------------------------------------------------------------------------------------------------------------------------

◎接上搖桿

程式碼:

const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);
}

void loop() {
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(500);
}

  • 沒有按:1
  • 按下去:0





◎數值簡單化



程式碼:



const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(115200);
}
void loop() {
int s=digitalRead(SW_pin);
int x=analogRead(X_pin);
int y=analogRead(Y_pin);
Serial.print("w");
Serial.print(s);
Serial.print("x");
if(x/4=='w' || x/4=='x' || x/4=='y' ) Serial.write('v');
else Serial.write(x/4);
Serial.print("y");
if(y/4=='w' || y/4=='x' || y/4=='y' ) Serial.write('v');
else Serial.write(y/4);
delay(50);
// Serial.print("Switch: ");
// Serial.print(digitalRead(SW_pin));
/* Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_pin));
Serial.print("\n\n");*/
// delay(500);
}





◎連到Processing


程式碼:

import processing.serial.*;
Serial myPort;
void setup(){
size(300,300);
myPort = new Serial(this, "COM7", 115200);
}
int t=0, s=48, x=128, y=128;
void draw(){
background(255);
while(myPort.available() > 0){
t = myPort.read();
if(t=='w') s = myPort.read();
else if(t=='x') x=myPort.read();
else if(t=='y') y=myPort.read();
}
if(s==49)fill(255,0,0);
else fill(0,255,0);
ellipse(x, y, 30,30);
}





◎代入之前期中的程式碼

程式碼:
import processing.serial.*;
Serial myPort;
//import ddf.minim.*;
//Minim minim;
//AudioPlayer bgm;
PImage imgBall,imgP1,imgP2;
void setup(){
   size(800,600);
   textSize(50);
   myPort = new Serial(this, "COM7", 115200);
   imgBall=loadImage("Ball.png");
   imgP1=loadImage("P1.png");
   imgP2=loadImage("P2.png");
   imageMode(CENTER);
   //minim=new Minim(this);
   //bgm=minim.loadFile("pika.mp3",2048);
   //bgm.play();
   //bgm.loop();
}
int t=0, s=48, x=128, y=128;
float ballX=120,ballY=150,ballVX=0,ballVY=0;
float p1X=100,p1Y=430,p1VX=0,p1VY=0;
float p2X=700,p2Y=430,p2VX=0,p2VY=0;
Boolean bGG=false;
int p1S=0,p2S=0; ///Score

void draw(){
  background(#BFEAFF); ///sky
  while(myPort.available() > 0){
    t = myPort.read();
  if(t=='w') s = myPort.read();
  else if(t=='x') x=myPort.read();
  else if(t=='y') y=myPort.read();
  }
  //if(s==49)fill(255,0,0);
  //else fill(0,255,0);
  //ellipse(x, y, 30,30);
   noStroke();fill(#D1C0A6);rect(0,500,800,100); ///floor
   fill(#A6ACAF);rect(400,320,10,240); ///cylider
   image(imgP1,p1X,p1Y,200,200);
   image(imgP2,p2X,p2Y,200,200);
   image(imgBall,ballX,ballY,80,80);

   fill(#484646);
   text(p1S,100,70);
   text(p2S,700,70);
   
   if(p1S==15){
    bGG=true;
    String p1win = "p1 is winner !!";
    fill(#E85050);
    text(p1win, 230, 150, 500, 200);
   }
   if(p2S==15){
    bGG=true;
    String p2win = "p2 is winner !!";
    fill(#E85050);
    text(p2win, 230, 150, 500, 200);
   }
   
   if(ballX<=360 && ballY>=500-40){
       bGG=true; ///if ball touch the floor : GameOver
       if(bGG)
       {
         p2S++;
         ballX=680;ballY=150;
         p1X=100;p1Y=450;
         p2X=700;p2Y=450;
         ballVX=0;ballVY=0;
         p1VX=0;p1VY=0;
         p2VX=0;p2VY=0;
         bGG=false;
       }
   }
   if(ballX>=450 && ballY>=500-40){
      bGG=true; ///if ball touch the floor : GameOver
      if(bGG)
       {
         p1S++;
         ballX=120;ballY=150;
         p1X=100;p1Y=450;
         p2X=700;p2Y=450;
         ballVX=0;ballVY=0;
         p1VX=0;p1VY=0;
         p2VX=0;p2VY=0;
         bGG=false;
       }
   }
   
   if(bGG)return;
   ballX += ballVX; ballY += ballVY;
   ballVY += 0.98;
   
   p1X+=p1VX;
   if(p1X<80) p1X=80; 
   if(p1X>300) p1X=300;
   p1Y += p1VY; p1VY+=0.98;
   
   p2X+=p2VX;
   if(p2X<500) p2X=500;
   if(p2X>720) p2X=720;
   p2Y += p2VY; p2VY+=0.98;

   if(ballX<50){ballX=50;ballVX=-ballVX;} 
   if(ballX>800-50){ballX=800-50;ballVX=-ballVX;}
   if(ballY<50){ballY=50;ballVY=-ballVY;}
   if(ballY>800-50){ballY=800-50;ballVY=-ballVY;}
   
   if(330<ballX && ballX<470 && 270<ballY &&ballY<560)
     {ballVX=-ballVX;ballVY=-ballVY;}
  
   if(p1Y>=500-50) {p1VY=0; p1Y=500-50;}
   if(p2Y>=500-50) {p2VY=0; p2Y=500-50;}
   if(dist(p1X,p1Y,ballX,ballY)<=80)ballVY=-ballVY;
   if(dist(p2X,p2Y,ballX,ballY)<=80)ballVY=-ballVY;
   if(dist(p1X,p1Y,ballX,ballY)<=80) ballVX=(80-ballVX)/6.0;
   if(dist(p2X,p2Y,ballX,ballY)<=80) ballVX=-(80-ballVX)/6.0;
}








week15 彣禎的上課筆記

老師範例:
#processing
import processing.serial.*;
Serial myPort;
void setup(){
size(300,300);
myPort = new Serial(this, "COM7", 115200);
}
int t=0, s=48, x=128, y=128;
void draw(){
background(255);
while(myPort.available() > 0){
t = myPort.read();
if(t=='w') s = myPort.read();
else if(t=='x') x=myPort.read();
else if(t=='y') y=myPort.read();
}
if(s==49)fill(255,0,0);
else fill(0,255,0);
ellipse(x, y, 30,30);
}
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 3; // analog pin connected to Y output
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(115200);
}
void loop() {
int s=digitalRead(SW_pin);
int x=analogRead(X_pin);
int y=analogRead(Y_pin);
Serial.print("w");
Serial.print(s);
Serial.print("x");
if(x/4=='w' || x/4=='x' || x/4=='y' ) Serial.write('v');
else Serial.write(x/4);
Serial.print("y");
if(y/4=='w' || y/4=='x' || y/4=='y' ) Serial.write('v');
else Serial.write(y/4);
delay(50);
// Serial.print("Switch: ");
// Serial.print(digitalRead(SW_pin));
/* Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_pin));
Serial.print("\n\n");*/
// delay(500);
}


進度:
#Processing
PImage imgcat,imgrat,imgcave,imglife;
import processing.serial.*;
Serial myPort;
void setup(){
  size(500,800);
  myPort=new Serial(this,"COM7",115200);
  imgcat=loadImage("cat.png");
  imgrat=loadImage("rat.png");
  imgcave=loadImage("cave.png");
  imglife=loadImage("life.png");
  for(int i=0;i<60;i++){
    ratX[i]=100+int(random(3))*150;
    ratY[i]=random(-8000);
    imageMode(CENTER);
  }
}
float []ratX=new float[80];
float []ratY=new float[80];
float catX=250,catY=700;
int score=0;
int life=3;
boolean Gameover=false;
int stage=0,time=0;
int t=0, s=48, x=128, y=128;
void draw(){
  background(129,214,125);
  if(stage==0)
  {
    fill(11,35,152); textSize(60);
    text("Click to Start",68,350);
  }
  else if(stage==1){
    if(life==0)stage=2;
  }else if(stage==2){
    fill(11,35,152); textSize(40);
    text("Game Over!!",48,350);
    text("You score is "+score,100,400);
  }
  fill(#286A25); rect(0,740,600,100);
  image(imglife,420,100,100,100);
  image(imgcave,100,10,200,120);
  image(imgcave,250,10,200,120);
  image(imgcave,400,10,200,120);
  fill(255,0,0);textSize(30); text("Score:"+score,10,100);
  textSize(40); text(":"+life,450,100);
  image(imgcat,catX,catY,150,150);
  if(stage==1){
  for(int i=0;i<60;i++){
    image(imgrat,ratX[i],ratY[i],70,70);
    ratY[i]+=2;
    if(dist(catX,catY,ratX[i],ratY[i])<75){
      ratY[i]=100+random(-8000);
      score+=5;
    }
    if(Gameover) return;
    if(ratY[i]>=700) {
      ratY[i]=100+random(-8000);
      life-=1;
     
      if(life==0) {
        Gameover=true;
      }
    }
  }
}
}
void keyPressed(){
  if(key=='a') catX-=75;
  if(key=='s') catX+=75;
}
void mousePressed(){
  if(stage==0) {stage=1;}
  if(stage==2) {stage=0;}
}

week15 ShiRo


期末作品-測試
😊光敏電阻
光敏電阻圖樣
 ➤課本範例:
Ardiuno 


實體接角
➤接角圖

➤測試圖

顯示紅色-遮B、G

顯示藍色-遮R、G

顯示藍紅色-遮R、B






Week15 是賴小沫的

✎光敏電阻

👀透過光敏電阻改變燈泡顏色

Arduino_ColorMixingLamp範例檔
/*
  Arduino Starter Kit example
 Project 4  - Color Mixing Lamp

 This sketch is written to accompany Project 3 in the
 Arduino Starter Kit

 Parts required:
 1 RGB LED
 three 10 kilohm resistors
 3 220 ohm resistors
 3 photoresistors
 red green and blue colored gels

 Created 13 September 2012
 Modified 14 November 2012
 by Scott Fitzgerald
 Thanks to Federico Vanzati for improvements

 http://www.arduino.cc/starterKit
 This example code is part of the public domain
 */

const int greenLEDPin = 9;    // LED connected to digital pin 9
const int redLEDPin = 10;     // LED connected to digital pin 10
const int blueLEDPin = 11;    // LED connected to digital pin 11
const int redSensorPin = A0;  // pin with the photoresistor with the red gel
const int greenSensorPin = A1;   // pin with the photoresistor with the green gel
const int blueSensorPin = A2;   // pin with the photoresistor with the blue gel
int redValue = 0; // value to write to the red LED
int greenValue = 0; // value to write to the green LED
int blueValue = 0; // value to write to the blue LED
int redSensorValue = 0; // variable to hold the value from the red sensor
int greenSensorValue = 0; // variable to hold the value from the green sensor
int blueSensorValue = 0; // variable to hold the value from the blue sensor
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  // set the digital pins as outputs
  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
}
void loop() {
  // Read the sensors first:
  // read the value from the red-filtered photoresistor:
  redSensorValue = analogRead(redSensorPin);
  // give the ADC a moment to settle
  delay(5);
  // read the value from the green-filtered photoresistor:
  greenSensorValue = analogRead(greenSensorPin);
  // give the ADC a moment to settle
  delay(5);
  // read the value from the blue-filtered photoresistor:
  blueSensorValue = analogRead(blueSensorPin);
  // print out the values to the serial monitor
  Serial.print("raw sensor Values \t red: ");
  Serial.print(redSensorValue);
  Serial.print("\t green: ");
  Serial.print(greenSensorValue);
  Serial.print("\t Blue: ");
  Serial.println(blueSensorValue);
  /*
  In order to use the values from the sensor for the LED,
  you need to do some math. The ADC provides a 10-bit number,
  but analogWrite() uses 8 bits. You'll want to divide your
  sensor readings by 4 to keep them in range of the output.
  */
  redValue = redSensorValue / 4;
  greenValue = greenSensorValue / 4;
  blueValue = blueSensorValue / 4;
  //  print out the mapped values
  Serial.print("Mapped sensor Values \t red: ");
  Serial.print(redValue);
  Serial.print("\t green: ");
  Serial.print(greenValue);
  Serial.print("\t Blue: ");
  Serial.println(blueValue);
  /*
  Now that you have a usable value, it's time to PWM the LED.
  */
  analogWrite(redLEDPin, redValue);
  analogWrite(greenLEDPin, greenValue);
  analogWrite(blueLEDPin, blueValue);
}

👀組裝

👀測試結果
此為所有光敏電阻照射到光的結果
 此為控制藍色數值的光敏電阻照射到光的結果
 此為控制紅色數值的光敏電阻照射到光的結果
 此為控制綠色數值的光敏電阻照射到光的結果

期末作品

👀關卡設計

17/12/25 德宇的筆記Week15

目標一:期末作品製作


已決定與期中作品相同,製作雪球大戰

遊戲方式:
由類比搖桿作為作為上下左右鍵的移動,
而按下鍵則為發射鍵。

透過此方式來操作期中的Processing遊戲