因為主遊戲程式是用C#寫的,所以有點複雜,不知道怎麼解釋,
不過Arduino部分就很簡單,
一號按鈕有按,就送出1,反之則0
二號按鈕有按,就送出3,反之則2
以此類推,遊戲側的程式會判定傳來的值代表哪個按鈕。
另外補上幾張製作控制器時的圖:
Arduino的程式碼:
int switchPin1 = 2;
int switchPin2 = 3;
int switchPin3 = 4;
int switchPin4 = 5;
int switchPin5 = 6;
boolean oldStage1 = false;
boolean oldStage2 = false;
boolean oldStage3 = false;
boolean oldStage4 = false;
boolean oldStage5 = false;
void setup()
{
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
pinMode(switchPin3, INPUT);
pinMode(switchPin4, INPUT);
pinMode(switchPin5, INPUT);
Serial.begin(9600);
}
void loop()
{
if (digitalRead(switchPin1) == LOW)
{
if(oldStage1)
{
Serial.print(0,DEC);
}
oldStage1 = false;
}
else
{
if(!oldStage1)
{
Serial.print(1,DEC);
}
oldStage1 = true;
}
if (digitalRead(switchPin2) == LOW)
{
if(oldStage2)
{
Serial.print(2,DEC);
}
oldStage2 = false;
}
else
{
if(!oldStage2)
{
Serial.print(3,DEC);
}
oldStage2 = true;
}
if (digitalRead(switchPin3) == LOW)
{
if(oldStage3)
{
Serial.print(4,DEC);
}
oldStage3 = false;
}
else
{
if(!oldStage3)
{
Serial.print(5,DEC);
}
oldStage3 = true;
}
if (digitalRead(switchPin4) == LOW)
{
if(oldStage4)
{
Serial.print(6,DEC);
}
oldStage4 = false;
}
else
{
if(!oldStage4)
{
Serial.print(7,DEC);
}
oldStage4 = true;
}
if (digitalRead(switchPin5) == LOW)
{
if(oldStage5)
{
Serial.print(8,DEC);
}
oldStage5 = false;
}
else
{
if(!oldStage5)
{
Serial.print(9,DEC);
}
oldStage5 = true;
}
}
沒有留言:
張貼留言