#include <Keypad.h>
#define VERT_PIN A0
#define HORZ_PIN A1
#define SEL_PIN 7
#define ENCODER_CLK 8
#define ENCODER_DT 9
#define ENCODER_BTN 7
#define ENCODER_CLK2 10
#define ENCODER_DT2 11
// the loop routine runs over and over again forever:
//编码器电平变量设定
int lastClk = HIGH;
int lastClk2 = HIGH;
//四个独立按钮变量设定
const int buttonPin1 = 3;
int oldValue1 = LOW; // default/idle value for pin 8 is low.
const int buttonPin2 = 4;
int oldValue2 = LOW; // default/idle value for pin 8 is low.
const int buttonPin3 = 5;
int oldValue3 = LOW; // default/idle value for pin 8 is low.
const int buttonPin4 = 6;
int oldValue4 = LOW; // default/idle value for pin 8 is low.
//按钮阵列变量设定
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[COLS][ROWS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 31, 33, 35, 37 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 23, 25, 27, 29 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup(){
//定义针脚,设定频率
//Serial.begin(9600);// Communication started with 9600 baud
Serial.begin(115200);
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
pinMode(SEL_PIN, INPUT_PULLUP);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_CLK2, INPUT);
pinMode(ENCODER_DT2, INPUT);
pinMode(ENCODER_BTN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
// Initialize the pin for reading the button.
//Serial.println("Press the button.");
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
}
void loop() {
//摇杆设定及执行
bool selPressed = digitalRead(SEL_PIN) == LOW;
// read the input on analog pin 0:
int sensorX = analogRead(A0);
int sensorY = analogRead(A1);
// read the input on analog pin 1:
// print out the value you read:
if(sensorX >= 700){
Serial.println("3");
delay(100);
}
else if(sensorX <= 400){
Serial.println("1");
delay(100);
}
else if(sensorY >= 700){
Serial.println("4");
delay(100);
}
else if(sensorY <= 400){
Serial.println("2");
delay(100);
}
// delay in between reads for stability
//编码器 1 设定及执行
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
Serial.println("Rotated clockwise ⏩");
}
if (newClk == LOW && dtValue == LOW) {
Serial.println("Rotated counterclockwise ⏪");
}
}
if (digitalRead(ENCODER_BTN) == LOW) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
//编码器 2 设定及执行
int newClk2 = digitalRead(ENCODER_CLK2);
if (newClk2 != lastClk2) {
// There was a change on the CLK pin
lastClk2 = newClk2;
int dtValue2 = digitalRead(ENCODER_DT2);
if (newClk2 == LOW && dtValue2 == HIGH) {
Serial.println("Rotated clockwise ⏩");
}
if (newClk2 == LOW && dtValue2 == LOW) {
Serial.println("Rotated counterclockwise ⏪");
}
}
if (digitalRead(ENCODER_BTN) == LOW) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
//四个按钮设定执行
// Read the value of pin 3.
int newValue1 = digitalRead(buttonPin1);
// Check if the value was changed,
// by comparing it with the previous value.
if(newValue1 != oldValue1)
{
if(newValue1 == HIGH)
{
Serial.println("The button is pressed.");
}
else
{
Serial.println("The button is released.");
}
// Remember the value for the next time.
oldValue1 = newValue1;
}
// Slow down the sketch.
// Also for debouncing the button.
//delay(100);
// Read the value of pin 4.
int newValue2 = digitalRead(buttonPin2);
// Check if the value was changed,
// by comparing it with the previous value.
if(newValue2 != oldValue2)
{
if(newValue2 == HIGH)
{
Serial.println("The button is pressed.");
}
else
{
Serial.println("The button is released.");
}
// Remember the value for the next time.
oldValue2 = newValue2;
}
// Slow down the sketch.
// Also for debouncing the button.
//delay(100);
// Read the value of pin 5.
int newValue3 = digitalRead(buttonPin3);
// Check if the value was changed,
// by comparing it with the previous value.
if(newValue3 != oldValue3)
{
if(newValue3 == HIGH)
{
Serial.println("The button is pressed.");
}
else
{
Serial.println("The button is released.");
}
// Remember the value for the next time.
oldValue3 = newValue3;
}
// Slow down the sketch.
// Also for debouncing the button.
//delay(100);
// Read the value of pin 6.
int newValue4 = digitalRead(buttonPin4);
// Check if the value was changed,
// by comparing it with the previous value.
if(newValue4 != oldValue4)
{
if(newValue4 == HIGH)
{
Serial.println("The button is pressed.");
}
else
{
Serial.println("The button is released.");
}
// Remember the value for the next time.
oldValue4 = newValue4;
}
// Slow down the sketch.
// Also for debouncing the button.
//delay(100);
char key = keypad.getKey();
if (key != NO_KEY) {
Serial.println(key);
}
}