2020年12月7日 星期一

week ╯13 ╮

 week13👀❤✌👤

♥2槽輸入,8槽喇叭輸出,按下按鈕後state=1、並輸出1,放開則state=0、並輸出0

♥程式碼-

#define button 2

#define buzzer 8


void setup()

{

  Serial.begin(9600);

  pinMode(2,INPUT_PULLUP);

  pinMode(8,OUTPUT);

}

int state=0;

void loop()

{

  if(digitalRead(2)==LOW && state==0)

  {

    state=1;

    Serial.write("1");

  }

  if(digitalRead(2)==HIGH && state==1)

  {

   state=0;

    Serial.write=("0");;

  }

  delay(100);

}

---

♥processing

♥程式碼-

import processing.serial.*;

Serial myPort;

void setup(){

    size(300,200);

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

}

int button=0;

void draw(){

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

    int now = myPort.read();

    println("right now you read is:"+now);

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

    else button=0;

  }

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

  else background(255);

}


---

♥download->UDP

♥點擊方框就能將資料傳輸到"120.125.70.53"

♥程式碼-

import hypermedia.net.*;

UDP udp;

void setup(){

    size(300,200);

    udp= new UDP(this, 6000);

    udp.send("hello world", "120.125,70,53", 6100);

}

void draw(){

 

}

void mousePressed(){

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

}



weeeeeeeeeeeeek13

按按鈕發出聲音 (Arduino)

void setup() {

  Serial.begin(9600);

  pinMode(2,INPUT_PULLUP);

  pinMode(8,OUTPUT);

}

int state=0;

void loop() {

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

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

    state=1;

    Serial.write("1");

  }

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

    state=0;

    Serial.write("0");

  }

  delay(100);

}

配合Arduino 更改視窗顏色

import processing.serial.*;

Serial myPort;

void setup()

{

  size(300,200);

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

}

int button=0;

void draw()

{

  if(myPort.available()>0)

  {

    int now = myPort.read();

    println("現在你讀的其實是:"+now);

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

    else button=0;

  }

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

  else background(255);

}

跟別台電腦作互動

import hypermedia.net.*;

UDP udp;

void setup()

{

  size(300,200);

  udp = new UDP(this,6000);

  udp.send("Connect","120.125.70.53");

}


void draw()

{

  

}


void mousePressed()

{

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

}

week13

 import hypermedia.net.*;

UDP udp;

void setup() {

  size(800, 800);

  udp=new UDP(this, 6000);

  udp.send("hello world","120.125.70.53",6100);

  

}

void draw() {

}

void mousePressed(){

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

}

Sherry:MakerUno、搖桿、麵包板

  2020/12/07

(一)MakerUno、搖桿、麵包板

1. https://makeruno.com.my/getting-started/ 下載 CH341 Driver for Windows here

2. 接線









3. Arduino

void setup() {

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

  Serial.begin(9600);

  pinMode(2,INPUT_PULLUP);

  pinMode(8,OUTPUT);

}

int state=0;

void loop() {

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

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

    state=1;

    Serial.write("1");

  }

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

    state=0;

    Serial.write("0");  

  }

  delay(100);

}

4. Processing (注意port 的部分)

import processing.serial.*;

Serial myPort;

void setup(){

  size(300,200);

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

  

}

int button=0;

void draw(){

  if(myPort.available()>0){

    int now=myPort.read();

    println("u are reading:" +now);

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

    else button=0;

  }

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

  else background(255);

}


5. Processing (try_2) (記得要開)





import hypermedia.net.*;
UDP udp;
void setup(){
  size(300,200);
  udp=new UDP(this,6000);
  udp.send("hola","120.125.70.53",6100);
  
}
int button=0;
void draw(){

}
void mousePressed(){
  udp.send("mousePressed","120.125.70.53",6100);
}

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);

}





 




喵~搖桿~多人遊戲~

 maker uno+搖桿

  • 組裝:
    • SW  -  pin2
    • GND  -  GND
    • +5V  -  5V
Arduino v.s Processing
  • Arduino
    • void setup() {
    •   // put your setup code here, to run once:
    •   Serial.begin(9600);
    •   pinMode(2,INPUT_PULLUP);
    •   pinMode(8,OUTPUT);
    • }
    • int state=0;
    • void loop() {
    •   // put your main code here, to run repeatedly:
    •   if(digitalRead(2)==LOW && state==0){
    •     state=1;
    •     Serial.write("1");
    •   }
    •   if(digitalRead(2)==HIGH && state==1){
    •     state=0;
    •     Serial.write("0");
    •   }
    •   delay(100);
    • }
  • Processing
    • import processing.serial.*;
    • Serial myPort;
    • void setup() 
    • {
    •   size(200, 200);
    •   myPort = new Serial(this,"COM4",9600);
    • }
    • int btn=0;
    • void draw() {
    •   if(myPort.available()>0){
    •     int now=myPort.read();
    •     if(now=='1') btn=1;
    •     else btn=0;
    •   }
    •   if(btn==1) background(200,100,255);
    •   else background(255);
    • }
連線到老師的電腦~(多人連線)
  • 載入外掛
  • Processing(客戶端)
    • import hypermedia.net.*;//外掛
    • UDP udp;
    • void setup(){
    •   size(500,500);
    •   udp = new UDP(this,6000);
    •   udp.send("hello world","120.125.70.53",6100);
    • }
    • void draw(){}
    • void mousePressed(){
    •   udp.send("mousePressed","120.125.70.53",6100);
    • }

Bibo)Week13_外接器具+使用按鈕

Sep.1 按鈕先與麵包板結合

Sep.2按鈕上的SW連接上到A2的按鈕

         +5V的地方接到5V的地方

         GMD連接到GND的地方

Sep.3撰寫程式,試試看

        在setup下執行

         Serial.begin(9600); //USB連接

         pinMode(2, INPUT_PULLUP); //讓第2的腳位可以接收資料

         在loop下執行

         if(digitalRead(2)==LOW && state==0); //當按下第2的腳位 且state=0時

         if(digitalRead(2)==LOW) tone(8, 572, 100); //當按下第2的腳位 發出聲音


接著將板子.按鈕.Arduino.Processing結合再一起

    首先利用Arduino將程式寫入板子,可以讀入1或0,也就是按鈕的按下與放掉

再來,切換到Processing的程式裡面

myPort = new Serial(this, "COM5", 9600); 
myPort.available()>0; //當我讀到的輸入>0
int now = myPort.read(): //加上now的變數就是讀到的數值
if( now == '1')button=1;//當我讀到now=1 button=1

接下來,我們要來連接網路~



用之前要先載入UDP外掛軟件