2012年12月4日 星期二

HW11,Week13

[程式碼]



[影片]

3 則留言:

  1. 程式碼

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

    回覆刪除
  2. 傳輸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);
    }

    回覆刪除
  3. 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);
    }

    回覆刪除