2013年12月23日 星期一

第十六週 課堂作業及回家作業

第16週12/23寫Arduino的部份
用兩個按鈕控制左右移動


int switchStateRed = 0; int switchStateBlue = 0; int switchStateW = 0; int switchStateA = 0; const int ledPin = 13; // led connected to digital pin 13 const int knockSensor = A0; // the piezo is connected to analog pin 0 const int threshold = 100; int sensorReading = 0; // variable to store the value read from the sensor pin int ledState = LOW;
void setup(){ pinMode(ledPin, OUTPUT); pinMode(4,INPUT); pinMode(5,INPUT); Serial.begin(9600); }
//int a=0,b=0;
void loop(){ sensorReading = analogRead(knockSensor); switchStateW = digitalRead(4); switchStateA = digitalRead(5);
if (sensorReading >= threshold) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
} if(switchStateW == HIGH) {Serial.write('3');} if(switchStateA == HIGH) {Serial.write('4');} delay(33); }

沒有留言:

張貼留言