Home        Store        Docs        Blog

Sending command through Joystick in I2c

(rahim rasool) #1

I am controlling the BlueESC through I2c and sending the throttle input by entering value in the serial monitor of Arduino IDE. All this works perfectly.

However, I wanted to send the throttle value through a joystick which is connected to PC and using a Python code it sends the joystick commands to Arduino serially. I presume this should be simple but I’m facing a lot issue with it. I am attaching the python code and the slightly modified i2c arduino code.

I’ll be sending 1,2 or 3 through the joystick.

//#include <Wire.h>
//#include “Arduino_I2C_ESC.h”
//#define ESC_ADDRESS 0x29
//Arduino_I2C_ESC motor(ESC_ADDRESS);
//int signal;
//void setup() {
//void loop() {
//if ( Serial.available() > 0 ) {
//signal = Serial.read(); //I’ve changed it to Serial.read() to
//} // to read commands from joystick
//motor.set((signal)*10000); //Received value 1,2 or 3 is multiplied by 10000
//motor.update(); //to set throttle value
//Serial.print(“ESC: “);
//if(motor.isAlive()) Serial.print(“OK\t\t”);
//else Serial.print(“NA\t\t”);
//Serial.print(signal);Serial.print(” \t\t”);
//Serial.print(motor.rpm());Serial.print(" RPM\t\t");

import pygame
import time
import serial
arduino = serial.Serial(‘COM4’, 57600, timeout=.1)

j = pygame.joystick.Joystick(0)
print ‘Initialized Joystick : %s’ % j.get_name()

def get():
out = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

setTempCar1 = j.get_button(4) * 1
setTempCar2 = j.get_button(5) * 2
setTempCar3 = j.get_button(6) * 3

if setTempCar1 == 1:
setTemp1 = str(setTempCar1)
print setTemp1
elif setTempCar2 == 2:
setTemp2 = str(setTempCar2)
print setTemp2

elif setTempCar3 == 3:
setTemp3 = str(setTempCar3)
print setTemp3


def test():
while True:
print get()

<hr />

I’m not good with coding but it seems as if the serial value from python to arduino is being overwritten by input from blueEsc through i2c. Please guide me with the changes i need to make in my code. Thanks

(Rusty) #2

Hi Rahim,

The first thing I notice in this is that you are sending the values as ASCII characters, like ‘1’ and ‘2’, instead of as numbers, like 1 and 2. That’s an issue because the characters 1 and 2 actually correspond to an ASCII code that’s much higher (49 and 50). If you remove the “str()” and just send as a number, you should have better luck.