銘傳大學 Processing & Arduino 課程部落格
程式碼const int buttonPin1 = 2;const int buttonPin2 = 3;const int buttonPin3 = 4;const int buttonPin4 = 5;const int buttonPin5 = 6;int buttonState1 = 0;int buttonState2 = 0;int buttonState3 = 0;int buttonState4 = 0;int buttonState5 = 0;void setup() { pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(buttonPin4, INPUT); pinMode(buttonPin5, INPUT); }void loop(){ buttonState1 = digitalRead(buttonPin1); buttonState2 = digitalRead(buttonPin2); buttonState3 = digitalRead(buttonPin3); buttonState4 = digitalRead(buttonPin4); buttonState5 = digitalRead(buttonPin5); if (buttonState1 == HIGH) tone(8, 523,8); if (buttonState2 == HIGH) tone(8, 587,8); if (buttonState3 == HIGH) tone(8, 659,8); if (buttonState4 == HIGH) tone(8, 698,8); if (buttonState5 == HIGH) tone(8, 784,8);}
傳輸Serial的程式碼Arduino側int switchPin = 4; //接受第四隻接腳的訊號void setup() { pinMode(switchPin, INPUT); Serial.begin(9600); }void loop() { if (digitalRead(switchPin) == HIGH) { Serial.print(1); } else { Serial.print(0); } delay(100);}
Processing側import processing.serial.*;Serial myPort;int val=48; //1傳過來好像是用ANSI編碼...void setup() { size(200, 200); String portName = "COM4"; //port寫死在COM4 myPort = new Serial(this, portName, 9600);}void draw(){ if ( myPort.available() > 0) { val = myPort.read(); println(val); } background(255); if (val == 48) //因為訊號是ANSI來著,所以1是48 { fill(0); } else { fill(204); } rect(50, 50, 100, 100);}
程式碼
回覆刪除const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
void setup()
{
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}
void loop()
{
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
if (buttonState1 == HIGH)
tone(8, 523,8);
if (buttonState2 == HIGH)
tone(8, 587,8);
if (buttonState3 == HIGH)
tone(8, 659,8);
if (buttonState4 == HIGH)
tone(8, 698,8);
if (buttonState5 == HIGH)
tone(8, 784,8);
}
傳輸Serial的程式碼
回覆刪除Arduino側
int switchPin = 4; //接受第四隻接腳的訊號
void setup()
{
pinMode(switchPin, INPUT);
Serial.begin(9600);
}
void loop()
{
if (digitalRead(switchPin) == HIGH)
{
Serial.print(1);
}
else
{
Serial.print(0);
}
delay(100);
}
Processing側
回覆刪除import processing.serial.*;
Serial myPort;
int val=48; //1傳過來好像是用ANSI編碼...
void setup()
{
size(200, 200);
String portName = "COM4"; //port寫死在COM4
myPort = new Serial(this, portName, 9600);
}
void draw()
{
if ( myPort.available() > 0)
{
val = myPort.read();
println(val);
}
background(255);
if (val == 48) //因為訊號是ANSI來著,所以1是48
{
fill(0);
}
else
{
fill(204);
}
rect(50, 50, 100, 100);
}