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

}





 




沒有留言:

張貼留言