Arduino 方面的程式碼為
#include "IRremote.h"
#define RECV_PIN 6 //define input pin on Arduino
/*
* 右 CD0
* 左 2D0
* 中 A70
* 下 AF0
* 上 2F0
*/
#define right_button 3280
#define left_button 720
#define middle_button 2672
#define down_button 2800
#define up_button 752
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
//Serial.println("Start");
}
void loop() {
if ( irrecv.decode(&results) ) {
int getValue = results.value;
Serial.println(getValue, HEX);
switch(getValue){
case right_button:
{
Serial.println("right");
delay(100);
break;
}
case left_button:
{
Serial.println("left");
delay(100);
break;
}
case middle_button:
{
Serial.println("middle");
delay(300);
break;
}
case down_button:
{
Serial.println("down");
delay(200);
break;
}
case up_button:
{
Serial.println("up");
delay(200);
break;
}
default:
{
Serial.println("default");
break;
}
} // end of switch-case
irrecv.resume(); // Receive the next value
}
delay(100);
}
Python 方面的程式碼為
import pyautogui
import serial #用 pip 安裝 Pyserial
ser = serial.Serial('COM12',9600) #setup serial port&&baudrate
while 1 :
#data = ser.read(5) # get serial data from arduino dht11(5Bytes)
data = ser.readline()
data1 = data.decode('utf-8') #decode data
#print(data1)
if data1 =="right\r\n":
pyautogui.press('right')
#print("right12456")
elif data1 =="left\r\n":
pyautogui.press('left')
#print("left12456")
elif data1 =="middle\r\n":
pyautogui.typewrite('s')
#print("middle12456")
elif data1 =="down\r\n":
pyautogui.press('down')
#print("down12456")
elif data1 =="up\r\n":
pyautogui.press('up')
#print("up12456")
Python 方面,可控制鍵盤也可以用來控制滑鼠
詳細的對照表請見此連結
我將定期推出程式語言的新手教學影片