Mikiklopsiki
Published © GPL3+

PC controll by arduino uno/nano. Something like dial

Joystick to controll pc, using python.

IntermediateFull instructions provided504
PC controll by arduino uno/nano. Something like dial

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Analog joystick (Generic)
×1

Software apps and online services

Python

Story

Read more

Schematics

Schematic

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

Code

arduino

Arduino
// the setup routine runs once when you press reset:

  // initialize serial communication at 9600 bits per second:
  void setup(){
  Serial.begin(9600);
    // Communication started with 9600 baud
}


// the loop routine runs over and over again forever:
void loop() {
  // 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
}

Python

Python
import serial #Serial imported for Serial communication
import time #Required to use delay functions
import pyautogui

ArduinoSerial = serial.Serial('com6',9600) #Create Serial port object called arduinoSerialData
time.sleep(2) #wait for 2 seconds for the communication to get established

while 1:
    incoming = str (ArduinoSerial.readline()) #read the serial data and print it as line
    
    
    if '1' in incoming:
        pyautogui.hotkey('up')

    if '2' in incoming:
        pyautogui.hotkey('right')  

    if '3' in incoming:
        pyautogui.hotkey('down') 

    if '4' in incoming:
        pyautogui.hotkey('left')

        
        incoming = "";
    

My first try in Java NOT WORKING

Java
import processing.serial.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;


Serial MyPort;                                
String KeyString = "";

void setup()
{
  System.out.println("Hi");
  size(300, 300);
  MyPort = new Serial(this, "COM6", 9600);// My Arduino is on COM3. Enter the COM on which your Arduino is on.
  MyPort.bufferUntil('\n');
}

void draw(){
   background(237, 240, 241);
  fill(20, 160, 133); // Green Color
  stroke(33);
  strokeWeight(1);
 
  
  
  textSize(32);
 
  textSize(24);
  fill(33);
  text("Status:", 100, 150);
  textSize(30);
textSize(16);
  text("Joystick", 100, 50);
  text(KeyString, 100, 170);
}

void serialEvent(Serial MyPort)throws Exception {
   KeyString = MyPort.readStringUntil('\n');
   KeyString = KeyString.substring(0, KeyString.indexOf(':'));
   System.out.println(KeyString);
   Robot Arduino = new Robot();
   int KeyToPress;
   switch(KeyString){
    
     case "1" :
       Arduino.keyPress(38);
       Arduino.keyRelease(38); 
       break;
     case "2" :
      Arduino.keyPress(39);
       Arduino.keyRelease(39); 
       break;
     case "3" :
       Arduino.keyPress(40);
       Arduino.keyRelease(40);      
       break;
     case "4" :                                                                                          
       Arduino.keyPress(37);
       Arduino.keyRelease(37);
       break; 
     
   }
   
}

Credits

Mikiklopsiki
0 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.