I am an arduino noob. I have been searching for some interesting arduino projects recently and making a game controller using Arduino UNO seemed interesting. So I hooked up two dual-axis joystick modules to the arduino board and wrote a program to print something corresponding to the joystic movements. I collected the serial data through USB and passed it on to a python program. The code works fine, mostly. But the problem is, when I hook up the circuit and start using the joysticks, after few minutes my system is freezing.
Why is my system freezing?
What can I do to avoid the freezing?
My system info:
1.Windows 10 64-bit
2.4GB RAM and 512GB HDD
Here is the code
int SW = 9; // mouse joystick
int xpin = A0;
int ypin = A1;
int SW1 = 6; //movement joystick
int xpin1 = A2;
int ypin1 = A3;
void setup()
{
Serial.begin(9600);
pinMode(SW, INPUT);
digitalWrite(SW, HIGH);
pinMode(SW1, INPUT);
digitalWrite(SW1,HIGH);
}
int prev_state = 0; // previous state of switch
void loop() {
int z = 0, xpos = 0, ypos = 0;
int z1 = 0;char xpos1='0', ypos1='0';
int x = analogRead(xpin);
int x1 = analogRead(xpin1);
int y = analogRead(ypin);
int y1 = analogRead(ypin1);
int sensitivity = 15; //mouse sensitivity
if (x >= 550) // when moved up
xpos = map(x, 550, 1023, 0, sensitivity);
if (x <= 450) // when moved down
xpos = map(x, 450, 0, 0, -sensitivity);
if (y >= 550) // when moved right
ypos = map(y, 550, 1023, 0, sensitivity);
if (y <= 450) // when moved left
ypos = map(y, 450, 0, 0, -sensitivity);
int curr_state = digitalRead(SW);
if (curr_state == 1 && prev_state == 0) // when SW is pressed
z = 1;
else
z = 0;
int prev_state1 = 0;
if (x1 >= 550) // when pressed d
xpos1 ='d';
if (x1 <= 450) // when pressed a
xpos1 ='a';
if (y1 >= 550) // when pressed w
ypos1 ='w';
if (y1 <= 450) // when pressed s
ypos1 ='s';
int curr_state1 = digitalRead(SW1);
if (curr_state1 == 1 && prev_state1 == 0) // when SW1 is pressed
z1 = 1;
else
z1 = 0;
if (xpos != 0 or ypos != 0 or z == 1 or xpos1=='d' or xpos1=='a' or ypos1=='w' or ypos1=='s' or z1 == 1) // prints when the joysticks are moved
{
Serial.print(xpos);
Serial.print(":");
Serial.print(ypos);
Serial.print(":");
Serial.print(z);
Serial.print(":");
Serial.print(xpos1); // prints the data and separating by ":"
Serial.print(":");
Serial.print(ypos1);
Serial.print(":");
Serial.println(z1);
}
prev_state = curr_state;
prev_state1 = curr_state1;
delay(10);
}
I am using a python program, in visual studio code, to read the arduino serial data and perform the required operation.
The python code
import mouse, sys
import time
import serial
import keyboard as kb
mouse.FAILSAFE=False
ArduinoSerial=serial.Serial('com3',9600) #assigning com port
time.sleep(1)
while 1:
data=str(ArduinoSerial.readline().decode('ascii')) #read the data
(x,y,z,x1,y1,z1)=data.split(":") # assigns to x,y,z,x1,y1,z1
(X,Y)=mouse.get_position() #read the current position of mouse cursor
(x,y)=(int(x),int(y))
mouse.move(X+x,Y-y) # move camera/mouse
# press the desired keys corresponding to joystick movement
if y1=='w':
kb.press('w')
kb.release('w')
if y1=='s':
kb.press('s')
kb.release('s')
if x1=='a':
kb.press('a')
kb.release('a')
if x1=='d':
kb.press('d')
kb.release('d')
if '1' in z: # read the Status of SW
mouse.click(button="left") # clicks left button
if '1' in z1: #read SW1
kb.press('space') # press space
-
To be clear, your system is freezing to the point where you need to power cycle the PC?timemage– timemage2021年02月05日 17:57:27 +00:00Commented Feb 5, 2021 at 17:57
-
Even though the communication only runs at 9600, I suspect that's just to many keyboard presses you're generating here. If, for instance, you move left, it will generate around 9000 presses of the "a" button. No human would ever be able to do that.PMF– PMF2021年02月05日 18:26:42 +00:00Commented Feb 5, 2021 at 18:26