Files
J1B2-Game-controller/arduino/Serial button test/sketch_nov21a/sketch_nov21a.ino

75 lines
2.0 KiB
C++

#include <ArduinoJson.h>
#include <ArduinoJson.hpp>
const int buttonPinUp = 9; // the number of the pushbutton pin
const int buttonPinRight = 10; // the number of the pushbutton pin
const int buttonPinDown = 11; // the number of the pushbutton pin
const int buttonPinLeft = 12; // the number of the pushbutton pin
const int buttonPinDodge = 14;
const int buttonPinPause = 13;
int buttonStateUp = 0;
int buttonStateRight = 0;
int buttonStateDown = 0;
int buttonStateLeft = 0;
int buttonStateDodge = 0;
int buttonStatePause = 0;
bool boolUp = false;
bool boolRight = false;
bool boolDown = false;
bool boolLeft = false;
bool boolDodge = false;
bool boolPause = false;
// variables will change:
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPinUp, INPUT);
pinMode(buttonPinRight, INPUT);
pinMode(buttonPinDown, INPUT);
pinMode(buttonPinLeft, INPUT);
pinMode(buttonPinDodge, INPUT);
pinMode(buttonPinPause, INPUT);
Serial.begin(9600);
}
void loop() {
boolUp = false;
boolRight = false;
boolDown = false;
boolLeft = false;
boolDodge = false;
boolPause = false;
// read the state of the pushbutton value:
buttonStateUp = digitalRead(buttonPinUp);
buttonStateRight = digitalRead(buttonPinRight);
buttonStateDown = digitalRead(buttonPinDown);
buttonStateLeft = digitalRead(buttonPinLeft);
buttonStateDodge = digitalRead(buttonPinDodge);
buttonStatePause = digitalRead(buttonPinPause);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonStateUp == HIGH) {
boolUp = true;
}
if (buttonStateRight == HIGH) {
boolRight = true;
}
if (buttonStateDown == HIGH) {
boolDown = true;
}
if (buttonStateLeft == HIGH) {
boolLeft = true;
}
if (buttonStateDodge == HIGH) {
boolDodge = true;
}
if (buttonStatePause == HIGH) {
boolPause = true;
}
String Jsarray = String("[") + boolUp + "," + boolRight + "," + boolDown + "," + boolLeft + "," + boolDodge + "," + boolPause + "]\n";
Serial.print(Jsarray);
}