1.processing
import processing.serial.*;
Serial myPort;
int val;
void setup() {
size(400, 200);
background(255);
noStroke();
// frameRate(30);
fill(102);
myPort = new Serial(this, "COM4", 9600);
}
int d=0,r=0,m=0,f=0,s=0;
void draw() {
if(myPort.available()>0){
val = myPort.read();//第三個重點:如果讀到數字就拿出來用
//rect(20, 20, 72, 160);
}
fill(255, 255, 255);
stroke(0, 0, 0);
strokeWeight(2);
if(val=='G'){
d=1;
}
else if(val=='B'){
d=0;
}
if(val=='F'){
r=1;
}
else if(val=='V'){
r=0;
}
if(val=='D'){
m=1;
}
else if(val=='C'){
m=0;
}
if(val=='S'){
f=1;
}
else if(val=='X'){
f=0;
}
if(val=='A'){
s=1;
}
else if(val=='Z'){
s=0;
}
if(val=='H'){
d=0;
r=0;
m=0;
f=0;
s=0;
}
//println(doo);
if(s==1){
fill(255,0,0);
rect(20, 20, 72, 160);
}
else if(s==0){
fill(255);
rect(20, 20, 72, 160);
}
if(f==1){
fill(255,0,0);
rect(92, 20, 72, 160);
}
else if(f==0){
fill(255);
rect(92, 20, 72, 160);
}
if(m==1){
fill(255,0,0);
rect(164, 20, 72, 160);
}
else if(m==0){
fill(255);
rect(164, 20, 72, 160);
}
if(r==1){
fill(255,0,0);
rect(236, 20, 72, 160);
}
else if(r==0){
fill(255);
rect(236, 20, 72, 160);
}
if(d==1){
fill(255,0,0);
rect(308, 20, 72, 160);
}
else if(d==0){
fill(255);
rect(308, 20, 72, 160);
}
fill(0);
rect(74, 20, 36, 80);
rect(146, 20, 36, 80);
rect(290, 20, 36, 80);
rect(362, 20, 18, 80);
}
2.arduinno
int buttonPin = 2;
int buttonPin3 = 3;
int buttonPin4 = 4;
int buttonPin5 = 5;
int buttonPin6 = 6;
int buttonState = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
buttonState6 = digitalRead(buttonPin6);
if (buttonState == HIGH) {
tone(8, 131, 300);
Serial.write('A');
}
else if (buttonState == LOW) {
Serial.write('Z');
}
if (buttonState3== HIGH) {
tone(8, 147 , 300);
Serial.print('S');
}
else if (buttonState3 == LOW) {
Serial.write('X');
}
if (buttonState4 == HIGH) {
tone(8, 165 , 300);
Serial.print('D');
}
else if (buttonState4 == LOW) {
Serial.write('C');
}
if (buttonState5 == HIGH) {
tone(8, 175 , 300);
Serial.print('F');
}
else if (buttonState5 == LOW) {
Serial.write('V');
}
if (buttonState6 == HIGH) {
tone(8, 196, 300);
Serial.print('G');
}
else if (buttonState6 == LOW) {
Serial.write('B');
}
else {
Serial.print('H');
}
delay(100);
}
3.展示歌曲 火車快飛
5531 5531 2344 3455
5353 231
4222 3111
2342111
沒有留言:
張貼留言