I made a physical controller for my maze game. There are four momentary switches and one push button connected to the breadboard. The four momentary switches are used to control the direction of the dot moves, and the push button controls the fight mode. If I had a large breadboard, I would connect more buttons so that two players can play at the same time.
Here’s my code for Arduino.
void setup() { // put your setup code here, to run once: pinMode(2, INPUT); pinMode(4, INPUT); pinMode(8,INPUT); pinMode(9,INPUT); pinMode(12, INPUT); Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: int inByte = Serial.read(); Serial.print(digitalRead(12)); Serial.print(","); delay(1); Serial.print(digitalRead(8)); Serial.print(","); delay(1); Serial.print(digitalRead(4)); Serial.print(","); delay(1); Serial.print(digitalRead(2)); Serial.print(","); delay(1); Serial.println(digitalRead(9)); //fight }
Code for Processing
void serialEvent(Serial myPort) { String input = myPort.readString(); String[] numbers = split(input, ','); float[] values = float(numbers); if (values[0] == 0) { moveL1 = false; } if (values[0] == 1) { moveL1 = true; } if (values[1] == 0) { moveD1 = false; } if (values[1] == 1) { moveD1 = true; } if (values[2] == 0) { moveU1 = false; } if (values[2] == 1) { moveU1 = true; } if (values[3] == 0) { moveR1 = false; } if (values[3] == 1) { moveR1 = true; } if (values[4] == 0) { fight = false; } if (values[4] == 1) { fight = true; } }