Devices & Components
Arduino Nano
USB A to Mini-B Cable
Jumper wires (generic)
Resistor 10k ohm
Breadboard (generic)
Pushbutton switch 12mm
Software & Tools
Arduino IDE
Python IDLE
Project description
Code
switches.ino
c_cpp
The Arduino Code
1int pulsante1 = 2; 2int pulsante2 = 9; 3int button1stato = 0; 4int button2stato = 0; 5int valorepotenziometro = 0; 6 7void setup() { 8 // put your setup code here, to run once: 9 Serial.begin(9600); 10 pinMode(pulsante1, INPUT); 11 pinMode(pulsante2, INPUT); 12 pinMode(LED_BUILTIN, OUTPUT); 13 14} 15 16void loop() { 17 button1stato = digitalRead(pulsante1); 18 button2stato = digitalRead(pulsante2); 19 valorepotenziometro = analogRead(A7); 20 21 if(button1stato == HIGH){ 22 Serial.println("sx"); 23 digitalWrite(LED_BUILTIN, HIGH); 24 delay(200); 25 } 26 if(button2stato == HIGH){ 27 Serial.println("dx"); 28 digitalWrite(LED_BUILTIN, LOW); 29 delay(200); 30 } 31 32} 33
commands.py
python
The Python Code
1import serial 2import pyautogui 3import time 4 5arduino = "dx" 6arduino2 = "sx" 7 8ser = serial.Serial('COM45', 9600) 9time.sleep(1) 10 11while True: 12 val = ser.readline().strip() 13 if(val == arduino): 14 pyautogui.click() 15 if(val == arduino2): 16 pyautogui.rightClick() 17 18if __name__ == "__main__": 19 main() 20 21 22 23 24 25
switches.ino
c_cpp
The Arduino Code
1int pulsante1 = 2; 2int pulsante2 = 9; 3int button1stato = 0; 4int button2stato = 0; 5int valorepotenziometro = 0; 6 7void setup() { 8 // put your setup code here, to run once: 9 Serial.begin(9600); 10 pinMode(pulsante1, INPUT); 11 pinMode(pulsante2, INPUT); 12 pinMode(LED_BUILTIN, OUTPUT); 13 14} 15 16void loop() { 17 button1stato = digitalRead(pulsante1); 18 button2stato = digitalRead(pulsante2); 19 valorepotenziometro = analogRead(A7); 20 21 if(button1stato == HIGH){ 22 Serial.println("sx"); 23 digitalWrite(LED_BUILTIN, HIGH); 24 delay(200); 25 } 26 if(button2stato == HIGH){ 27 Serial.println("dx"); 28 digitalWrite(LED_BUILTIN, LOW); 29 delay(200); 30 } 31 32} 33
Comments
Only logged in users can leave comments