顯示具有 07160156_朱政洋 標籤的文章。 顯示所有文章
顯示具有 07160156_朱政洋 標籤的文章。 顯示所有文章

2021年1月14日 星期四

Yang_Week14

Week14

Joystick滑動-變換音頻:

 void setup() {

  pinMode(8,OUTPUT);

}

void loop() {

  int now =analogRead(A0); 0~511 Hz

  tone(8,now);

}



























Joystick滑動-燈光閃爍程度

void setup() {

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

  for(int i=2;i<=13;i++){

    pinMode(i,OUTPUT);

  }

}

void loop() {

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

  int now =analogRead(A0);

  int level =now/(512/13);

  for(int i=2;i<=13;i++){

    if(i<level)digitalWrite(i,HIGH);

    else digitalWrite(i,LOW);

  }

}

Joystick滑動-滑動顯示字母:

void setup() {

   put your setup code here, to run once:

  for(int i=2;i<=13;i++){

    pinMode(i,OUTPUT);

  }

  Serial.begin(9600);

}


void loop() {

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

  int now =analogRead(A0);

  int level =now/(512/13);

  for(int i=2;i<=13;i++){

    if(i<level)digitalWrite(i,HIGH);

    else digitalWrite(i,LOW);

  }

  Serial.write('A'+level);

  delay(100);

}










Joystick滑動-滑動顯示血條:

Processing:

import processing.serial.*;

Serial myPort;

void setup(){

   size(765,100);

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

}

int level=0;

void draw(){

 background(255);

 fill(255,0,0);

 rect(0,0,level*50,100);

 if(myPort.available()>0){

    int now=myPort.read(); 

    level=now;

     }

}

arduino:

void setup() {

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

  for(int i=2;i<=13;i++){

    pinMode(i,OUTPUT);

  }

  Serial.begin(9600);

}

void loop() {

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

  int now =analogRead(A0);

  int level =now/4;

  for(int i=2;i<=13;i++){

    if(i<level)digitalWrite(i,HIGH);

    else digitalWrite(i,LOW);

  }

  Serial.write(level);

  delay(20);

}





















2020年12月21日 星期一

Yang_Week15

 Week15

 PVector:

PVector pt;

PVector pt;

PImage img;

void setup(){

    size(600,600);

    img=loadImage("img.jpg");

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

}

void draw(){

      image(img,0,0);

      println(pt.x);

}











拉彈弓:

PVector pt,Y;

PImage img;

void setup(){

  size(600,600);

  Y=new PVector(100,300);

  pt=new PVector(100,300);

}

void draw(){

  background(255);

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

  textSize(40);

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

  fill(255);ellipse(pt.x,pt.y,30,30);

}

void mouseDragged(){

  pt.x=mouseX;

  pt.y=mouseY;

}











拉弓並彈出:

PVector pt,Y,v;

PImage img;

void setup(){

  size(600,600);

  Y=new PVector(100,300);

  pt=new PVector(100,300);

}

void draw(){

  background(255);

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

  textSize(40);

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

  fill(255);ellipse(pt.x,pt.y,30,30);

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

}

void mouseDragged(){

  pt.x=mouseX;

  pt.y=mouseY;

}

void mouseReleased(){

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

  v=diff.div(10);

}











Joystick控制移動

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:

import processing.serial.*;

Serial myPort;

void setup(){

  size(256,256);

  myPort = new Serial(this,"COM4",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;

   }

   fill(255,0,0);

   ellipse(ballX,ballY,10,10);

}






























2020年12月7日 星期一

Yang_Week13

Arduino

JoyStick

JoyStick接Maker-Uno:
















按joystick發出聲音:

void setup() {

  pinMode(2,INPUT_PULLUP);

  pinMode(8,OUTPUT);

}

void loop() {

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

  if(digitalRead(2)==LOW)tone(8,572,100);

  else noTone(8);

}










按joystick顯示按下&放開顯示0,1 :

void setup() {

  Serial.begin(9600);

  pinMode(2, INPUT_PULLUP);

  pinMode(8, OUTPUT);

}

int s = 0;

void loop() {

  if (digitalRead(2) == LOW && s == 0) {

    s = 1;

    Serial.write("1");

  }

  if (digitalRead(2) == HIGH && s == 1) {

    s = 0;

    Serial.write("0");

  }

  delay(100);

}









Joystick &Processing

按Joystick Processing的background

Processing:

import processing.serial.*;

Serial myPort;

void setup(){

   size(300,200);

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

}

int button=0;

void draw(){

  if(myPort.available()>0){  ///接收資料

   int now =myPort.read();

   println("You read is :" +now); ///顯示文字編號

   if(now=='1')button=1;

   else button=0;

  }

  if(button==1)background(255,255,0);

  else background(0);

}









網域連線傳輸

Processing:

import hypermedia.net.*;

UDP udp;

void setup(){

  size(400,400);

  udp=new UDP(this,6000);

  udp.send("hello world","120.125.70.53",6100);  ///傳輸對象

}

void mousePressed(){

  udp.send("mousePressed","120.125.70.53",6100);

}





 




2020年12月6日 星期日

Yang_week08

 Week08

辨識滑鼠位置顏色

PImage img;

color nowColor;

void setup(){

  size(500,500);

  img=loadImage("01.png");

}

void draw(){

    image(img,0,0);

    fill(nowColor);rect(0,0,30,30);

}

void mouseMoved(){

 loadPixels();

 int now=width*mouseY+mouseX;

 nowColor =pixels[now];  

}











2020年11月30日 星期一

Yang_Week12

 

Aurduino+maker-uno


BUZZER 8 & noTone(使用BUZZER 8和靜音) :

#define C 523
#define D 659
#define E 784 
void setup() {
  pinMode(8,OUTPUT);
  tone(8,C);
  delay(1000);
  tone(8,D);
  delay(1000);
  tone(8,E);
  delay(1000);
  noTone(8);靜音
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(2)==LOW)tone(8,E,100);
}









Button 2(使用button2):

#define C 523
#define D 659
#define E 784 
void setup() {
  pinMode(2,INPUT_PULLUP);
  pinMode(8,OUTPUT);
  tone(8,C);
  delay(1000);
  tone(8,D);
  delay(1000);
  tone(8,E);
  delay(1000);
  noTone(8);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(2)==LOW)tone(8,E,100);
}



Processing & Aurduino+Maker-uno(Processing連結Aurduino) :
  • Processing
import processing.serial.*;
Serial myPort;
void setup(){
   size(200,200);
   myPort=new Serial(this,"COM4",9600);
}
void draw(){
  
}
void keyPressed(){
 if(key=='1')myPort.write("1");
 if(key=='3')myPort.write("3");
 if(key=='5')myPort.write("5"); 
}
void mousePressed(){
   myPort.write("1"); 
}

  • Aurduino+Maker-uno
#define C 523
#define D 659
#define E 784 
void setup() {
  Serial.begin(9600);
  pinMode(2,INPUT_PULLUP);
  pinMode(8,OUTPUT);
  tone(8,C);
  delay(1000);
  tone(8,D);
  delay(1000);
  tone(8,E);
  delay(1000);
  noTone(8);
}

void loop() {
  while(Serial.available()>0){
    int now =Serial.read();
    if(now=='1')tone(8,C,100);
    if(now=='3')tone(8,D,100);
    if(now=='5')tone(8,E,100);
  }
}


















完整鋼琴鍵:


  • Aurduino:
#define C 523 ///設置音階
#define D 587
#define E 659 
#define F 689 
#define G 784 
#define A 880 
#define B 988 

void setup() {
  Serial.begin(9600);
  pinMode(2,INPUT_PULLUP);
  pinMode(8,OUTPUT);
  tone(8,C);
  delay(1000);
  tone(8,D);
  delay(1000);
  tone(8,E);
  delay(1000);
  noTone(8);
}

void loop() {  ///按鍵設置
  while(Serial.available()>0){
    int now =Serial.read();
    if(now=='1')tone(8,C,100);
    if(now=='2')tone(8,D,100);
    if(now=='3')tone(8,E,100);
    if(now=='4')tone(8,F,100);
    if(now=='5')tone(8,G,100);
    if(now=='6')tone(8,A,100);
    if(now=='7')tone(8,B,100);
    if(now=='0')noTone(8);
  }
}


  • Processing:
import processing.serial.*;
Serial myPort;
void setup(){
   size(200,200);
   myPort=new Serial(this,"COM4",9600);
}
void draw(){
  
}
void keyPressed(){ ///設置按鍵
 if(key=='1')myPort.write("1");
 if(key=='2')myPort.write("2");
 if(key=='3')myPort.write("3");
 if(key=='4')myPort.write("4");
 if(key=='5')myPort.write("5");
 if(key=='6')myPort.write("6");
 if(key=='7')myPort.write("7");
  
}
void keyReleased(){///設置靜音鍵
   myPort.write("0"); 
}
void mousePressed(){
   myPort.write("1"); 
}





























Yang_Week11

 

Aurduino+maker-uno

marker-uno:
























音頻對照表:








歌曲創作:
  • #define C 523///定義音符頻率
  • #define D 587
  • #define E 659
  • #define F 698
  • #define G 784
  • #define A 880
  • int music[]={C,B,F,D,E,A,B,B,C,,G,A,B,,F,D,E,D,E,A};///0是不播音
  • void setup() {
  •   // put your setup code here, to run once:
  •   delay(1000);
  •   pinMode(8,OUTPUT);
  •   for(int i=0;i<sizeof(music)/sizeof(int);i++){
  •     tone(8,music[i],100);///播放(Pin8是蜂鳴器)
  •     delay(250);///音之間的間隔
  •   }
  • }
  • void loop() {}

參考網頁:https://github.com/CytronTechnologies/MakerUno_Examples




2020年10月26日 星期一

Yang_week07

 Week07

程式作圖

*做頂點

size(500,500);

for(int a=0;a<360;a+=60){///做正多邊形

    float a2=radians(a);

    float x=100+cos(a2)*50;

    float y=100+sin(a2)*50;

    ellipse(x,y,2,2);

}









*做多邊形

size(500,500);

beginShape();

for(int a=0;a<360;a+=60){///做正多邊形

    float a2=radians(a);

    float x=100+cos(a2)*50;

    float y=100+sin(a2)*50;

    vertex(x,y);

}

fill(255,0,0);

endShape(CLOSE);








*圖形旋轉

void setup(){

  size(500,500);

}

void draw(){

  beginShape();

  for(int a=0;a<360;a+=120){///做正多邊形

      float a2=radians(a+mouseX);

      float x=100+cos(a2)*50;

      float y=100+sin(a2)*50;

      vertex(x,y);

  }

  fill(0,0,225);

  endShape(CLOSE);

}









*圖形旋轉隨滑鼠移動

void setup(){

  size(500,500);

}

void draw(){

  background(255);

  beginShape();

  for(int a=0;a<360;a+=120){///做正多邊形

      float a2=radians(a+mouseX);

      float x=mouseX+cos(a2)*50;

      float y=mouseY+sin(a2)*50;

      vertex(x,y);

  }

  fill(0,0,225);

  endShape(CLOSE);

}









*三角形隨著每一幀轉動

void setup(){

  size(500,500);

}

void draw(){

  //background(255);

  beginShape();

  for(int a=0;a<360;a+=120){///做正多邊形

      float a2=radians(a+frameCount);//frameCount表示第幾個frame

      float x=mouseX+cos(a2)*50;

      float y=mouseY+sin(a2)*50;

      vertex(x,y);

  }

  fill(0,0,225);

  endShape(CLOSE);

}









*多邊形陣列

void setup(){

  size(500,500);

}

void draw(){

  int s=0;

  background(255);

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

     for(int j=0;j<5;j++){

       beginShape();

       for(int a=0;a<360;a+=120){///做正多邊形

          float a2=radians(a+frameCount*2);//frameCount表示第幾個frame

          float x=(j+1)*100+cos(a2)*35-50;

          float y=(i+1)*100+sin(a2)*35-60;

          vertex(x,y);

          if(s%2==0){

            fill(0,0,255);

            s++;

          }

          if(s%2==1){

            fill(255,0,0);

            s++;

          }

        }

         endShape(CLOSE);

         beginShape();

       for(int a=0;a<360;a+=120){///做正多邊形

          float a2=radians(a+frameCount*2);//frameCount表示第幾個frame

          float x=(j+1)*100+cos(a2)*20-50;

          float y=(i+1)*100+sin(a2)*20-60;

          vertex(x,y);

          if(s%2==0){

            fill(0,0,255);

            s++;

          }

          if(s%2==1){

            fill(0,0,225);

            s++;

          }

        }

         endShape(CLOSE);

         beginShape();

       for(int a=0;a<360;a+=120){///做正多邊形

          float a2=radians(a+frameCount*2);//frameCount表示第幾個frame

          float x=(j+1)*100+cos(a2)*10-50;

          float y=(i+1)*100+sin(a2)*10-60;

          vertex(x,y);

          if(s%2==0){

            fill(0,255,0);

            s++;

          }

          if(s%2==1){

            fill(0,255,0);

            s++;

          }

        }

         endShape(CLOSE);

     } 

  }

  endShape(CLOSE);

}








*多邊形陣列(2.0)

void setup(){

  size(500,500);

}

void draw(){

  int s=0;

  background(255);

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

     for(int j=0;j<9;j++){

       s++;

       beginShape();

       for(int a=0;a<360;a+=120){///做正多邊形

          float a2=radians(a-frameCount*1);//frameCount表示第幾個frame

          float x=(j+1)*50+cos(a2)*20-10;

          float y=(i+1)*50+sin(a2)*20-10;

          vertex(x,y);

           if(s%2==0)stroke(255,0,0);

           else stroke(0,0,255);

        }

         endShape(CLOSE);

         beginShape();

       for(int a=0;a<360;a+=120){///做正多邊形

          float a2=radians(a+frameCount*2);//frameCount表示第幾個frame

          float x=(j+1)*50+cos(a2)*15-10;

          float y=(i+1)*50+sin(a2)*15-10;

          vertex(x,y);

          if(s%2==0)stroke(255,0,0);

          else stroke(0,0,255);

        }

         endShape(CLOSE);

         beginShape();

       for(int a=0;a<360;a+=120){///做正多邊形

          float a2=radians(a-frameCount*4);//frameCount表示第幾個frame

          float x=(j+1)*50+cos(a2)*10-10;

          float y=(i+1)*50+sin(a2)*10-10;

          vertex(x,y);

          if(s%2==0)stroke(255,0,0);

          else stroke(0,0,255);

        }

         endShape(CLOSE);

     } 

  }

}







2020年10月19日 星期一

Yang_Week06

 Week06

TEXT:

ex1:

size(100,100);

fill(255,0,0);

textSize(40);

text("Hello",30,30);






ex2:

size(500,400);

rect(100,100,200,100);

fill(255,0,0);

textSize(40);

textAlign(LEFT,TOP);

text("Hello",100,100);












ex3:

size(800,300);

fill(255,0,0);

PFont font1=createFont("Times New Roman",30);

textFont(font1);

text("中文vs.En",30,30);

PFont font2=createFont("標楷體",30);

textFont(font2);

text("中文vs.En",30,130);









Array:

ex1:

int [][]A={{1,1,1},{0,1,0},{0,1,1},{1,1,1}};

size(300,400);

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

   for(int j=0;j<3;j++){

    if(A[i][j]==0)fill(0);

    else fill(0,0,255,125);

    rect(j*100,i*100,100,100);

   }

}









ex2:

int [][]A={{2,2,2,2},{0,1,0,1},{1,0,1,0},{2,2,2,2}};

PImage ball,eevee;

void setup(){

  size(400,400);

  ball=loadImage("ball.png");

  eevee=loadImage("eevee.png");

}

void draw(){

  background(0);

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

     for(int j=0;j<4;j++){

      if(A[i][j]==2)image(ball,j*100,i*100,100,100);

      if(A[i][j]==1)image(eevee,j*100,i*100,100,100);

     }

  } 

}










ex3:

int [][]A={{2,2,2,2},{0,1,0,1},{1,0,1,0},{2,2,2,2}};

PImage ball,eevee;

void mousePressed(){

   int i=int(mouseY/100);

   int j=int(mouseX/100);

   A[i][j]=0;

}

void setup(){

  size(400,400);

  ball=loadImage("ball.png");

  eevee=loadImage("eevee.png");

}

void draw(){

  background(0);

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

     for(int j=0;j<4;j++){

      if(A[i][j]==2)image(ball,j*100,i*100,100,100);

      if(A[i][j]==1)image(eevee,j*100,i*100,100,100);

     }

   ellipse(mouseX,mouseY,25,25);

   text("mouseX"+mouseX+" mouseY:"+mouseY,100,100);

}