maker uno+搖桿
- 組裝:
- SW - pin2
- GND - GND
- +5V - 5V
- 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);
- }


沒有留言:
張貼留言