2018年12月6日 星期四

Week13_陳示珮

Week13

按鈕















(1)成功連接開發板
















(2)至processing中開啟範例程序並選擇SimpleRead
















(3)將程式碼下方的程式複製到Arduino中並修改

Processing程式碼 :

import processing.serial.*;

Serial myPort;
int val;   
void setup()
{
  size(200, 200);
  String portName = Serial.list()[0];
  myPort = new Serial(this,"COM7", 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  background(255);             // Set background to white
  if (val == 0) {              // If the serial value is 0,
    fill(0);                   // set fill to black
  }
  else {                       // If the serial value is not 0,
    fill(204);                 // set fill to light gray
  }
  rect(50, 50, 100, 100);
}

Arduino程式碼 :

//int switchPin = 2;                       // Switch connected to pin 4

void setup() {
 // pinMode(switchPin, INPUT);             // Set pin 0 as an input
 pinMode(2,INPUT_PULLUP);
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(2) == HIGH) {  // If switch is ON,
    Serial.write(1);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.write(0);               // send 0 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}
















(4)成功執行畫面
















(5)按下按鈕灰色方塊會變成黑色方塊

沒有留言:

張貼留言