PC controll by arduino uno/nano. Something like dial
Joystick to controll pc, using python.
Components and supplies
1
Analog joystick (Generic)
1
Arduino Nano R3
Apps and platforms
1
Python
Project description
Code
My first try in Java NOT WORKING
java
1import processing.serial.*; 2import java.awt.AWTException; 3import java.awt.Robot; 4import java.awt.event.InputEvent; 5import java.awt.event.KeyEvent; 6import javax.swing.KeyStroke; 7 8 9Serial MyPort; 10String KeyString = ""; 11 12void setup() 13{ 14 System.out.println("Hi"); 15 size(300, 300); 16 MyPort = new Serial(this, "COM6", 9600);// My Arduino is on COM3. Enter the COM on which your Arduino is on. 17 MyPort.bufferUntil('\n'); 18} 19 20void draw(){ 21 background(237, 240, 241); 22 fill(20, 160, 133); // Green Color 23 stroke(33); 24 strokeWeight(1); 25 26 27 28 textSize(32); 29 30 textSize(24); 31 fill(33); 32 text("Status:", 100, 150); 33 textSize(30); 34textSize(16); 35 text("Joystick", 100, 50); 36 text(KeyString, 100, 170); 37} 38 39void serialEvent(Serial MyPort)throws Exception { 40 KeyString = MyPort.readStringUntil('\n'); 41 KeyString = KeyString.substring(0, KeyString.indexOf(':')); 42 System.out.println(KeyString); 43 Robot Arduino = new Robot(); 44 int KeyToPress; 45 switch(KeyString){ 46 47 case "1" : 48 Arduino.keyPress(38); 49 Arduino.keyRelease(38); 50 break; 51 case "2" : 52 Arduino.keyPress(39); 53 Arduino.keyRelease(39); 54 break; 55 case "3" : 56 Arduino.keyPress(40); 57 Arduino.keyRelease(40); 58 break; 59 case "4" : 60 Arduino.keyPress(37); 61 Arduino.keyRelease(37); 62 break; 63 64 } 65 66} 67
Python
python
1import serial #Serial imported for Serial communication 2import time #Required to use delay functions 3import pyautogui 4 5ArduinoSerial = serial.Serial('com6',9600) #Create Serial port object called arduinoSerialData 6time.sleep(2) #wait for 2 seconds for the communication to get established 7 8while 1: 9 incoming = str (ArduinoSerial.readline()) #read the serial data and print it as line 10 11 12 if '1' in incoming: 13 pyautogui.hotkey('up') 14 15 if '2' in incoming: 16 pyautogui.hotkey('right') 17 18 if '3' in incoming: 19 pyautogui.hotkey('down') 20 21 if '4' in incoming: 22 pyautogui.hotkey('left') 23 24 25 incoming = ""; 26 27
My first try in Java NOT WORKING
java
1import processing.serial.*; 2import java.awt.AWTException; 3import java.awt.Robot; 4import java.awt.event.InputEvent; 5import java.awt.event.KeyEvent; 6import javax.swing.KeyStroke; 7 8 9Serial MyPort; 10String KeyString = ""; 11 12void setup() 13{ 14 System.out.println("Hi"); 15 size(300, 300); 16 MyPort = new Serial(this, "COM6", 9600);// My Arduino is on COM3. Enter the COM on which your Arduino is on. 17 MyPort.bufferUntil('\ 18'); 19} 20 21void draw(){ 22 background(237, 240, 241); 23 fill(20, 160, 133); // Green Color 24 stroke(33); 25 strokeWeight(1); 26 27 28 29 textSize(32); 30 31 textSize(24); 32 fill(33); 33 text("Status:", 100, 150); 34 textSize(30); 35textSize(16); 36 text("Joystick", 100, 50); 37 text(KeyString, 100, 170); 38} 39 40void serialEvent(Serial MyPort)throws Exception { 41 KeyString = MyPort.readStringUntil('\ 42'); 43 KeyString = KeyString.substring(0, KeyString.indexOf(':')); 44 System.out.println(KeyString); 45 Robot Arduino = new Robot(); 46 int KeyToPress; 47 switch(KeyString){ 48 49 case "1" : 50 Arduino.keyPress(38); 51 Arduino.keyRelease(38); 52 break; 53 case "2" : 54 Arduino.keyPress(39); 55 Arduino.keyRelease(39); 56 break; 57 case "3" : 58 Arduino.keyPress(40); 59 Arduino.keyRelease(40); 60 break; 61 case "4" : 62 Arduino.keyPress(37); 63 Arduino.keyRelease(37); 64 break; 65 66 } 67 68} 69
arduino
arduino
1// the setup routine runs once when you press reset: 2 3 // initialize serial communication at 9600 bits per second: 4 void setup(){ 5 Serial.begin(9600); 6 // Communication started with 9600 baud 7} 8 9 10// the loop routine runs over and over again forever: 11void loop() { 12 // read the input on analog pin 0: 13 int sensorX = analogRead(A0); 14 int sensorY = analogRead(A1); 15 // read the input on analog pin 1: 16 // print out the value you read: 17 if(sensorX >= 700){ 18 19 Serial.println("3"); 20 21 delay(100); 22 } 23 24 else if(sensorX <= 400){ 25Serial.println("1"); 26delay(100); 27 28 } 29else if(sensorY >= 700){ 30 31 Serial.println("4"); 32 delay(100); 33 } 34 35else if(sensorY <= 400){ 36 37 Serial.println("2"); 38 delay(100); 39 } 40 41 // delay in between reads for stability 42} 43
arduino
arduino
1// the setup routine runs once when you press reset: 2 3 // initialize 4 serial communication at 9600 bits per second: 5 void setup(){ 6 Serial.begin(9600); 7 8 // Communication started with 9600 baud 9} 10 11 12// the loop routine 13 runs over and over again forever: 14void loop() { 15 // read the input on analog 16 pin 0: 17 int sensorX = analogRead(A0); 18 int sensorY = analogRead(A1); 19 20 // read the input on analog pin 1: 21 // print out the value you read: 22 23 if(sensorX >= 700){ 24 25 Serial.println("3"); 26 27 delay(100); 28 } 29 30 31 else if(sensorX <= 400){ 32Serial.println("1"); 33delay(100); 34 35 36 } 37else if(sensorY >= 700){ 38 39 Serial.println("4"); 40 delay(100); 41 42 } 43 44else if(sensorY <= 400){ 45 46 Serial.println("2"); 47 delay(100); 48 49 } 50 51 // delay in between reads for stability 52} 53
Downloadable files
Schematic
After some changes led and button are not requied. code is whidout led and button.
Schematic

Schematic
After some changes led and button are not requied. code is whidout led and button.
Schematic

Comments
Only logged in users can leave comments