2020年12月28日 星期一

final

process 

import processing.serial.*;////

Serial myPort;////

PImage pic;

PImage car;

PImage bg;

PImage die;

PImage heart;

float [] picX=new float[10];


float [] picY=new float[10];

float carX=235, carY=450;

int score=50;

int h=0;


void setup() {

  size(513, 615);


  myPort = new Serial(this, "COM5",9600);////

  

  for (int i=0; i<10; i++) {

    picX[i] = random(70, 295);

    picY[i] = -i*100;

  }

  pic = loadImage("pic.png");///三角椎

  car = loadImage("car.png");

  bg = loadImage("bg.png");

  die =loadImage("die.png");

}

float vx=0;

//float vy=0;

void draw() {

  if (h==0) {

    image(bg, 0, 0, 513, 615);

    //background(255);

    //carX=mouseX-47;   

 while(myPort.available()>=1)

     {

       int x = myPort.read();

       ///int y = myPort.read();

      //int sw = myPort.read();

       vx= (x-128)/32;

      // vy=(y-128)/32;

       println(vx);

       //carY=y;

     }

     carX += vx;

    // carY += vy;

    image(car, carX, carY, 50, 100);


    for (int i=0; i<7; i++) {

      image(pic, picX[i], picY[i], 60, 60);

      picY[i]+=2;

      if (picY[i]>615)

      {

        picY[i]=-183;  

        picX[i]=random(70, 295);///三角錐重生

      }

      if (picY[i]+60>carY) {

        if (carX<picX[i]+58 && picX[i]<carX+48) //2cm

        {///車碰到三角錐

          score-=10;

         picY[i]=-150;

        }

      }

    }

    textSize(50);

    text(score,50, 100);

  }

  if (score==0)

  {

    h=1;

    image(die, -35, 100, 600, 350);

  }

}

void keyPressed() {

  if (keyCode == ENTER)

  {

    h=0;

    score=50;

  }

}

arduino

void setup()

{

  pinMode(2, INPUT_PULLUP);//拉高的Input

  Serial.begin(9600);//開始設定USB的傳輸速度

}

void loop() {//1000Hz (1000 fps)

  int x = analogRead(A0);//0...1023

  //int y = analogRead(A1);//0...1023

 // int sw= digitalRead(2);//HIGH(放開)

  Serial.write (x/4);//第1個byte

  //Serial.write(y/4);//第2個byte

  //Serial.write (sw); //第3個byte

  delay(20);//1000ms/20ms => 50fps,變慢了

}












__________________________________________________________________








processing

 import processing.serial.*;////

Serial myPort;////

int count=1800;

PImage candy;

PImage monster;

PImage bg;

PImage die;

PImage heart;

float [] candyX=new float[10];

float [] candyY=new float[10];

float monsterX=235, monsterY=450;

int score=150;

int h=0;

void setup() {

  size(513, 615);

  myPort = new Serial(this, "COM3", 9600);////

  for (int i=0; i<10; i++) {

    candyX[i] = random(70, 295);

    candyY[i] = -i*100;

  }

  candy = loadImage("candy.png");///三角椎

  monster = loadImage("monster.png");

  bg = loadImage("bg.jpg");

  die =loadImage("die.png");

}

float vx=0;

//float vy=0;

void draw() {

  if (h==0) {

    image(bg, 0, 0, 513, 615);

    //background(255);

    //monsterX=mouseX-47;

    while (myPort.available()>=1)

    {

      int x = myPort.read();

      ///int y = myPort.read();

      //int sw = myPort.read();

      vx= (x-128)/32;

      // vy=(y-128)/32;

      println(vx);

      //monsterY=y;

    }

    monsterX += vx;

    // monsterY += vy;

    for (int i=0; i<7; i++) {

      image(candy, candyX[i], candyY[i], 60, 60);

      candyY[i]+=2;

      if (candyY[i]>615)

      {

        candyY[i]=-300;  //-183

        candyX[i]=random(70, 295);///三角錐重生

      }

      if (candyY[i]+60>monsterY) {

        if (monsterX<candyX[i]+58 && candyX[i]<monsterX+48) //2cm

        {///車碰到三角錐

          score+=10;

          candyY[i]=-150;

        }

      }

    }

    if (count<0) {

      background(0, 0, 0);

      h=1;

    } else {

      count--;

    }

    textSize(50);

    fill(25, 25, 250, 250);

    text("Time: "+int(count/60), 250, 50);

    textSize(50);

    text(score, 50, 100);

  }

  if (count<0 || score<1000)

  {

    h=1;

    image(die, -35, 100, 600, 350);

  }

}

void keyPressed() {

  count=1800;

  if (keyCode == ENTER)

  {

    h=0;

    score=150;

  }

}


arduino

void setup()

{

  pinMode(2, INPUT_PULLUP);//拉高的Input

  Serial.begin(9600);//開始設定USB的傳輸速度

}

void loop() {//1000Hz (1000 fps)

  int x = analogRead(A0);//0...1023

  //int y = analogRead(A1);//0...1023

 // int sw= digitalRead(2);//HIGH(放開)

  Serial.write (x/4);//第1個byte

  //Serial.write(y/4);//第2個byte

  //Serial.write (sw); //第3個byte

  delay(20);//1000ms/20ms => 50fps,變慢了

}

期末

 arduino:

void setup()

{

   pinMode(2, INPUT_PULLUP);

   Serial.begin(9600);  

}

void loop()

{

  int x=analogRead(A0);  

  int y=analogRead(A1);

  int sw=digitalRead(2);  


  Serial.write(x/4);

  Serial.write(y/4);

  Serial.write(sw);

  delay(20);

}

processing:

import processing.serial.*;

Serial myPort;

PImage img;

PImage img2;

void setup(){

  size(1200,700);

  img=loadImage("galaxy.jpg");

  img2=loadImage("aim.jpg");

  myPort = new Serial(this,"COM3",9600);

}

int aimX=600,aimY=350;

float vx=0, vy=0;

void draw()

{

  image(img,0,0,1200,700);

  while(myPort.available()>=3){

    int x=myPort.read();

    int y=myPort.read();

    int sw=myPort.read();

    vx=(x-118)/16.0;//-4.0...+4.0

    vy=(y-118)/16.0;//-4.0...+4.0

    println(x+" "+y);

  }

  aimX+=vx;

  aimY+=vy;

  image(img2,aimX-50,aimY-50,100,100);

}

下次進度:

1.去背圖片

2.加入按鈕功能

3.計時器

4.記分板

5.

2020/12/28

 

期末



#include <Keypad.h>    // 引用Keypad程式庫

#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27,16,2); 

#define KEY_ROWS 4 // 按鍵模組的列數

#define KEY_COLS 4 // 按鍵模組的行數


#define BUZZER 8


#define NOTE_C1 261 //F,C

#define NOTE_C2 293 //E,D

#define NOTE_C3 329 //D,E

#define NOTE_C4 349 //C,F

#define NOTE_C5 391 //B,G

#define NOTE_C6 440 //3,A

#define NOTE_C7 493 //6,B

#define NOTE_C8 532 //9,C



#define NOTE_C9 587 //A,D

#define NOTE_C10 659 //2,E

#define NOTE_C11 698 //5,F

#define NOTE_C12 783 //8,G

#define NOTE_C13 880 //0,A

#define NOTE_C14 987 //1,B

#define NOTE_C15 1046 //4,C

#define NOTE_C16 1174 //7,D



// 依照行、列排列的按鍵字元(二維陣列)

char keymap[KEY_ROWS][KEY_COLS] = {

  {'F', 'B', 'A', '0'},

  {'E', '3', '2', '1'},

  {'D', '6', '5', '4'},

  {'C', '9', '8', '7'}

};

byte colPins[KEY_COLS] = {7, 6, 5, 4};     // 按鍵模組,行1~4接腳。

byte rowPins[KEY_ROWS] = {12, 11, 10, 9}; // 按鍵模組,列1~4接腳。 

// 初始化Keypad物件

// 語法:Keypad(makeKeymap(按鍵字元的二維陣列), 模組列接腳, 模組行接腳, 模組列數, 模組行數)

Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, KEY_ROWS, KEY_COLS);

void setup(){

  Serial.begin(9600);

  lcd.init();


lcd.backlight();//是否開啟背光

/*

lcd.setCursor(2,0);//設定游標位置


lcd.print("TAIWAN 2021");//寫入字串


lcd.setCursor(1,1);//設定游標位置


lcd.print("Happy New Year");//寫入字串

*/

void loop(){

  // 透過Keypad物件的getKey()方法讀取按鍵的字元

  char key = myKeypad.getKey();

  

  if (key){  // 若有按鍵被按下…

    Serial.println(key);  // 顯示按鍵的字元

    

     if(key=='F') {tone(BUZZER, NOTE_C1, 200);lcd.setCursor(0,0);lcd.print("0");}

     if(key=='E') {tone(BUZZER, NOTE_C2, 200);lcd.setCursor(1,0);lcd.print("1");}

     if(key=='D') {tone(BUZZER, NOTE_C3, 200);lcd.setCursor(2,0);lcd.print("2");}

     if(key=='C') {tone(BUZZER, NOTE_C4, 200);lcd.setCursor(3,0);lcd.print("3");}

     if(key=='B') {tone(BUZZER, NOTE_C5, 200);lcd.setCursor(4,0);lcd.print("4");}

     if(key=='3') {tone(BUZZER, NOTE_C6, 200);lcd.setCursor(5,0);lcd.print("5");}

     if(key=='6') {tone(BUZZER, NOTE_C7, 200);lcd.setCursor(6,0);lcd.print("6");}

     if(key=='9') {tone(BUZZER, NOTE_C8, 200);lcd.setCursor(7,0);lcd.print("7");}


    if(key=='A') {tone(BUZZER, NOTE_C9, 200);lcd.setCursor(8,0);lcd.print("8");}

    if(key=='2') {tone(BUZZER, NOTE_C10, 200);lcd.setCursor(9,0);lcd.print("9");}

    if(key=='5') {tone(BUZZER, NOTE_C11, 200);lcd.setCursor(10,0);lcd.print("A");}

    if(key=='8') {tone(BUZZER, NOTE_C12, 200);lcd.setCursor(11,0);lcd.print("B");}

    if(key=='0') {tone(BUZZER, NOTE_C13, 200);lcd.setCursor(12,0);lcd.print("C");}

    if(key=='1') {tone(BUZZER, NOTE_C14, 200);lcd.setCursor(13,0);lcd.print("D");}

    if(key=='4') {tone(BUZZER, NOTE_C15, 200);lcd.setCursor(14,0);lcd.print("E");}

    if(key=='7') {tone(BUZZER, NOTE_C16, 200);lcd.setCursor(15,0);lcd.print("F");}

  }

}

喵~Processing手機APP~

Processing 安裝 Android

  • Android 模式
    • 需要SDK
  • Android SDK 安裝
    • 去官網下載 Command line toolshttps://developer.android.com/studio
    • cmdline-tools 拉到 C槽
    • 在 cmdline-tools 裡新增一個 lastest 資料夾
    • 將檔案拉進 lastest
    • 開啟小黑 cmd 輸入指令

Bill_week16

 今天老師跟我們要討論期末作品的討論,內容是用搖桿來操控其中的作品,而我的作品是做小朋友下樓梯所以要想辦法使用搖桿操縱小朋友左右走動。

2020年12月21日 星期一

week15 BOJI Hans

 

上傳進度:已上傳 230981 個位元組 (共 230981 個位元組)。





🗿week15


 

圖片

//int a=10

size(600,600);

PImage img=loadImage("img.jpg");

PVector pt=new ector(10 , 20, 0);

image(img ,0 ,0);

println(pt.x);5

//以前要用的時候

float userX=10,userY=20,userZ=0;

float user2X=30,user2Y=40,user2Z=0;

//現在可以用的物件,來讓他有條理

PVector user=new PVector(10,20,0);

PVector user2=new PVector(10,20,0);

_______________________________________________________________________

arduino

void setup() {

  // put your setup code here, to run once:

  pinMode(2,INPUT_PULLUP);

  Serial.begin(9600);

}


void loop() {

  // put your main code here, to run repeatedly:

  int x=analogRead(A0);

  int y=analogRead(A1);

  int sw=digitalRead(2);


  Serial.write(x/4);

  Serial.write(y/4);

  Serial.write(sw);

  delay(20);  

}

processing

PVector user;

PVector Y,v=null;

void setup(){

    size(600,600);

    user = new PVector(100,300.);

    Y=new PVector(100,300);

}

void draw(){

  background(255);

  line(user.x,user.y,Y.x,Y.y);

  textSize(40);

  fill(255,0,0); text("Y",Y.x,Y.y);

  fill(255); ellipse(user.x, user.y,20,20);

  if(v!=null) user.add(v);

}

void mouseReleased(){

  PVector diff = PVector.sub(Y,user);

  v = diff.div(10);

}___________________________________________________



processing

import processing.serial.*;

Serial myPort;

void setup(){

size(256,256);

myPort = new Serial(this, "COM6",9600);

}

int bal1X=0,bal1Y=0;

void draw()

{

 if(myPort.available()>=3)

 {

   int x = myPort.read();

   int y = myPort.read();

   int sw = myPort.read();

   bal1X=x;

   bal1Y=y;

 }

 ellipse(bal1X,bal1Y,10,10);

}


Arduino程式碼

void setup()

{

  pinMode(2, INPUT_PULLUP);//拉高的Input

  Serial.begin(9600);//開始設定USB的傳輸速度

}

void loop() {//1000Hz (1000 fps)

  int x = analogRead(A0);//0...1023

  int y = analogRead(A1);//0...1023

  int sw= digitalRead(2);//HIGH(放開)

  

  Serial.write (x/4);//第1個byte

  Serial.write(y/4);//第2個byte

  Serial.write (sw); //第3個byte

  delay(20);//1000ms/20ms => 50fps,變慢了

}


喵~PVector~

 Proecssing~PVector~

  • 比較:
    • 以前:(僅宣告)
      • float uX=10,uY=20,uZ=0;
      • float u2X=30,u2Y=40,u2Z=0;
    • 現在:(可利用PVector裡寫好的函式庫)
      • PVector u=new PVector(10,20,0);
      • PVector u2=new PVector(30,40,0);
    • 注意:檔名不可取到函數名(如:PVector、int、PImage......)
  • 憤怒鳥(angry brid)
    • Processing程式
      • PVector bird;
      • PVector Y,v=null;
      • void setup(){
      •   size(600,400);
      •   bird=new PVector(150,300);//只在setup() new一次
      •   Y=new PVector(150,300);
      • }
      • void draw(){
      •   background(255);
      •   textSize(40);
      •   fill(255,0,200);
      •   text("Y",Y.x-10,Y.y+30);
      •   fill(255);
      •   ellipse(bird.x,bird.y,20,20);
      •   line(bird.x,bird.y,Y.x,Y.y);
      •   if(v!=null){
      •     bird.add(v);
      •     v.y+=0.98;
      •   }
      • }
      • void mouseDragged(){//滑鼠拖曳
      •   bird.x=mouseX;
      •   bird.y=mouseY;
      •   v=null;
      • }
      • void mouseReleased(){
      •   PVector diff=PVector.sub(Y,bird);//減法
      •   v=diff.div(4);//除法
      • }
  • 搖桿(x,y,sw)結合Processing
    • 搖桿
      • GND - GND
      • +5V - 5V
      • URX - A0
      • URY - A1
      • SW - 2
    • Arduino
      • void setup() {
      •   pinMode(2,INPUT_PULLUP);
      •   Serial.begin(9600);
      • }
      • void loop() {//1000Hz(1000fps)
      •   int x=analogRead(A0);//0~1023
      •   int y=analogRead(A1);//0~1023
      •   int sw=digitalRead(2);//HIGH放開
      •   Serial.write(x/4);//第1個byte
      •   Serial.write(y/4);//第2個byte
      •   Serial.write(sw);//第3個byte
      •   delay(20);//1000ms/20ms => 50fps,變慢
      • }
    • Processing程式
      • import processing.serial.*;
      • Serial myPort;
      • void setup(){
      •   size(256, 256);
      •   myPort = new Serial(this,"COM4",9600);
      • }
      • int ballX=0,ballY=0;
      • void draw(){
      •   if(myPort.available()>=3){//延遲少一點 if 改 while
      •     int x=myPort.read();//0~255變成-128~+127
      •     int y=myPort.read();
      •     int sw=myPort.read();
      •     ballX=x;
      •     ballY=y;
      •   }
      •   ellipse(ballX,ballY,10,10);
      • }
  • ↑憤怒鳥結合搖桿↑(Arduino同上↑)
    • import processing.serial.*;
    • Serial myPort;
    • PVector bird;
    • PVector Y,v=null;
    • void setup(){
    •   size(600,400);
    •   myPort = new Serial(this,"COM4",9600);
    •   bird=new PVector(150,300);
    •   Y=new PVector(150,300);
    • }
    • void draw(){
    •   background(255);
    •   if(myPort.available()>=3){
    •     int x=myPort.read();
    •     int y=myPort.read();
    •     int sw=myPort.read();
    •     bird.x=100+x-128;
    •     bird.y=900+x-128;
    •   }
    •   textSize(40);
    •   fill(255,0,200);
    •   text("Y",Y.x-10,Y.y+30);
    •   fill(255);
    •   ellipse(bird.x,bird.y,20,20);
    •   if(v!=null){
    •     bird.add(v);
    •     v.y+=0.98;
    •   }
    • }

                                  week15

                                  1. 使用PVector叫出圖形


                                  2.

                                  PVector user;                                    1.宣告PVector,讓setup()跟draw()用的到

                                  void setup()
                                  {
                                    
                                    size(600,600);

                                    user = new PVector(10,20);            2.只在setup()做一次

                                  }

                                  void draw()
                                  {
                                    
                                      background(255);
                                   
                                      ellipse(user.x,user.y,20,20);            3.使用PVector的值

                                  }




                                  3.可以拉出線

                                  PVector user;
                                  PVector Y;
                                  void setup()
                                  {
                                    size(600,400);
                                    user = new PVector(100,300);
                                    Y=new PVector(100,300);
                                  }
                                  void draw()
                                  {
                                    background(255);
                                    line(user.x,user.y,Y.x,Y.y);
                                    textSize(40);
                                    fill(255,0,0); text("Y",Y.x-17,Y.y+20);
                                    fill(255);ellipse(user.x,user.y,20,20);
                                  }
                                  void mouseDragged()
                                  {
                                    user.x=mouseX;
                                    user.y=mouseY;
                                  }
                                  void mouseReleased()
                                  {

                                  }



                                  4.射出彈珠

                                  PVector user;
                                  PVector Y , v = null;     //V是速率
                                  void setup()
                                  {
                                    size(600,400);
                                    user = new PVector(100,300);
                                    Y=new PVector(100,300);
                                  }
                                  void draw()
                                  {
                                    background(255);
                                    line(user.x,user.y,Y.x,Y.y);
                                    textSize(40);
                                    fill(255,0,0); text("Y",Y.x-17,Y.y+20);
                                    fill(255);ellipse(user.x,user.y,20,20);
                                    if(v!=null) user.add(v);
                                  }
                                  void mouseDragged()
                                  {
                                    user.x=mouseX;
                                    user.y=mouseY;
                                  }
                                  void mouseReleased()
                                  {
                                    PVector diff =PVector.sub(Y,user);
                                    v=diff.div(10);
                                  }



                                  5.射出的彈珠加上重力加速度

                                  PVector user;
                                  PVector Y , v = null;     //V是速率
                                  void setup()
                                  {
                                    size(600,400);
                                    user = new PVector(100,300);
                                    Y=new PVector(100,300);
                                  }
                                  void draw()
                                  {
                                    background(255);
                                    if(mousePressed)line(user.x,user.y,Y.x,Y.y);
                                    textSize(40);
                                    fill(255,0,0); text("Y",Y.x-17,Y.y+20);
                                    fill(255);ellipse(user.x,user.y,20,20);
                                    if(v!=null) 
                                      {
                                        user.add(v);
                                        v.y+=0.98;
                                      }
                                  }
                                  void mouseDragged()
                                  {
                                    user.x=mouseX;
                                    user.y=mouseY;
                                    v=null;
                                  }
                                  void mouseReleased()
                                  {
                                    PVector diff =PVector.sub(Y,user);
                                    v=diff.div(4);
                                  }



                                  6.監視器回傳搖桿值




                                  7.P語言畫出搖桿位置

                                                                                                  arduino:
                                  void setup()
                                  {
                                     pinMode(2, INPUT_PULLUP);
                                     Serial.begin(9600);  
                                  }
                                  void loop()
                                  {
                                    int x=analogRead(A0);  
                                    int y=analogRead(A1);
                                    int sw=digitalRead(2);  

                                    Serial.write(x/4);
                                    Serial.write(y/4);
                                    Serial.write(sw);
                                    delay(20);
                                  }

                                                                                              processing:

                                  import processing.serial.*;
                                  Serial myPort;
                                  void setup(){
                                    size(256,256);
                                    myPort = new Serial(this,"COM3",9600);
                                  }
                                  int ballX=0,ballY=0;
                                  void draw()
                                  {
                                    background(255);
                                    if(myPort.available()>=3){
                                      int x=myPort.read();
                                      int y=myPort.read();
                                      int sw=myPort.read();
                                      ballX=x;
                                      ballY=y;
                                    }
                                    ellipse(ballX,ballY,10,10);
                                  }





                                  Carousel~W15

                                   

                                  PVector user;//外面宣告,setup & draw看得到
                                  void setup(){
                                    size(600,600);
                                    user = new PVector(10, 20);//只在setup()
                                  }
                                  void draw(){
                                  background(255);
                                  ellipse( user.x, user.y,20,20);//用它的值
                                  }



                                  憤怒鳥

                                  PVector user;

                                  PVector Y, v=null;

                                  void setup(){

                                    size(600, 400);

                                    user = new PVector(100, 300);//(2)只在setu

                                    Y = new PVector(100, 300);

                                  }

                                  void draw(){

                                    background(255);

                                    line( user.x, user.y, Y.x, Y.y);

                                    textSize(40);

                                    fill(255,0,0); text("Y", Y.x, Y.y);

                                    fill(255); ellipse( user.x, user.y,20,20);

                                    if(v!=null) user.add(v);

                                  }

                                  void mouseDragged(){

                                    user.x=mouseX;

                                    user.y=mouseY;

                                  }

                                  void mouseReleased(){

                                  PVector diff = PVector. sub(Y,user);

                                  v = diff.div(10);

                                  }


                                  傳輸速率

                                  void setup(){

                                  pinMode (2, INPUT_PULLUP) ; //拉高的Input

                                  Serial.begin (9600);//開始設定USB的傳輸速度

                                  }

                                  void loop() {//1000Hz (1000 fps)

                                  int x = analogRead (A0);//0...1023

                                  int y = analogRead(A1);//0...1023

                                  int sw= digitalRead(2);//HIGH(放開)or LOW(按下去)

                                  Serial.write (x);

                                  Serial.write ('+') ;

                                  Serial.write(y) ;

                                  Serial.write ('+') ;

                                  Serial.write (sw) ;

                                  Serial.write(' ');

                                  delay (20) ;//1000ms/20ms => 50fps,變慢了

                                  }



                                  void setup()
                                  {
                                    pinMode(2, INPUT_PULLUP);//拉高的Input
                                    Serial.begin(9600);//開始設定USB的傳輸速度
                                  }
                                  void loop() {//1000Hz (1000 fps)
                                    int x = analogRead(A0);//0...1023
                                    int y = analogRead(A1);//0...1023
                                    int sw= digitalRead(2);//HIGH(放開)
                                    
                                    Serial.write (x/4);//第1個byte
                                    Serial.write(y/4);//第2個byte
                                    Serial.write (sw); //第3個byte
                                    delay(20);//1000ms/20ms => 50fps,變慢了
                                  }

                                  processing

                                  import processing.serial.*;

                                  Serial myPort;

                                  void setup(){

                                  size(256,256);

                                  myPort = new Serial(this, "COM6",9600);

                                  }

                                  int bal1X=0,bal1Y=0;

                                  void draw()

                                  {

                                   if(myPort.available()>=3)

                                   {

                                     int x = myPort.read();

                                     int y = myPort.read();

                                     int sw = myPort.read();

                                     bal1X=x;

                                     bal1Y=y;

                                   }

                                   ellipse(bal1X,bal1Y,10,10);

                                  }



                                  KIO Week15

                                   Week15

                                  複習期中考前的程式

                                  Vector向量的用法


                                  做出類似彈弓的小遊戲


                                  add,sub,div用法


                                  arduino搖桿讀取


                                  processing 搖桿應用


                                  與彈弓結合