###### tags: `Arduino` `Arduino 實作` # YK04-MT CodeModule ```cpp= /* YK04 Module, Multiple Reading Reads a press of the remote control based on the YK04 driver and displays information in the default Serial. Multiple reading of the remote control. If the remote control is clamped, returns a pressed button value. Return value of pressing the remote control: Button::A - A button is pressed; Button::B - B button is pressed; Button::C - C button is pressed; Button::D - D button is pressed; Button::NOT - not pressed. https://github.com/YuriiSalimov/YK04_Module Created by Yurii Salimov, February, 2018. Released into the public domain. */ #include <YK04_Module.h> #define A_PIN 12 // (D0) #define B_PIN 11 // (D1) #define C_PIN 10 // (D2) #define D_PIN 9 // (D3) String msg = ""; const int mt[4] = {4, 5, 6, 7}; YK04_Module* module; // the setup function runs once when you press reset or power the board void setup() { Serial.begin(9600); for (int i = 0; i < 4; i++) { pinMode(mt[i], OUTPUT); } module = new YK04_Module(A_PIN, B_PIN, C_PIN, D_PIN); /* Or if you need to invert a signal of buttons pressing module = new YK04_Module(A_PIN, B_PIN, C_PIN, D_PIN, true); */ } // the loop function runs over and over again forever void loop() { Serial.print("YK04, Multiple Reading: "); Serial.println(buttonTitle(module->multipleRead())); delay(100); // optionally, only to delay the output of information in the example } /** Return title of the input button. */ String buttonTitle(const YK04_Module::Button button) { switch (button) { case YK04_Module::Button::NOT: digitalWrite(mt[0], 0); digitalWrite(mt[1], 0); digitalWrite(mt[2], 0); digitalWrite(mt[3], 0); return "NOT"; break; case YK04_Module::Button::A: digitalWrite(mt[0], 1); digitalWrite(mt[1], 0); digitalWrite(mt[2], 1); digitalWrite(mt[3], 0); return "A"; break; case YK04_Module::Button::B: digitalWrite(mt[0], 0); digitalWrite(mt[1], 1); digitalWrite(mt[2], 1); digitalWrite(mt[3], 0); return "B"; break; case YK04_Module::Button::C: digitalWrite(mt[0], 1); digitalWrite(mt[1], 0); digitalWrite(mt[2], 0); digitalWrite(mt[3], 1); return "C"; break; case YK04_Module::Button::D: digitalWrite(mt[0], 0); digitalWrite(mt[1], 1); digitalWrite(mt[2], 0); digitalWrite(mt[3], 1); return "D"; break; default: return "???"; } } ```