Motor speed control using Xbox 360 controller

Hello Arduino people, I am trying to control the speed of motor using the pwm pins of arduino. I’ve connected Xbox 360 controller to UNO via USB shield. On experimenting with the XBOXRECV code, I tried to get the values of left analog stick and mapped the values ranging from 7500 to 32767 with 0 to 255 for pwm output. Initially I connected an LED to observe the fading effect on moving the left analog stick of xbox controller. I hope you can imagine what I am trying to do. Please check my code below:

#include <XBOXRECV.h>

#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
int led=12;

USB Usb;
XBOXRECV Xbox(&Usb);

void setup() {
pinMode(led,OUTPUT);

Serial.begin(115200);
#if !defined(MIPSEL)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nXbox Wireless Receiver Library Started"));
}
void loop() {
Usb.Task();
if (Xbox.XboxReceiverConnected) {
for (uint8_t i = 0; i < 4; i++) {
if (Xbox.Xbox360Connected*) {*

  • if (Xbox.getAnalogHat(LeftHatY, i) < 32767 && Xbox.getAnalogHat(LeftHatY, i) > 7500) {*
  • long val=Xbox.getAnalogHat(LeftHatY, i);*
  • Serial.print(val);*
  • val=map(val,7500,32767,0,255);*
  • analogWrite(led,val);*
  • }*
  • }*
  • }*
  • }*
    }
    On the serial monitor I observed that on pushing the analog stick up and down, it displayed the frequently changing values, but the brightness of LED didn’t change even a bit, which meant that the analogWrite() function isn’t working.
    I did some research on the working of pwm pins of arduino and based on the above observations, I think that the input from controller is changing faster than the microcontroller can convert it into its respective pwm signal. Therefore, I decreased the baud rate from 115200 to 1200 but it just reduces the speed at which input is received and the LED brightness isn’t changing.
    I need your help on this!!!
    XBOXRECV.ino (958 Bytes)

Hi,

Pin 12 is not a PWM pin, try pin 11, if you look on the UNO pcb you will see curly ~ next to pins that support PWM.

Tom..... :slight_smile:

Oops, that was a typing mistake! Well, I used pin 11 actually. Still the problem persists.
I was thinking of a way, please try to help me on this:

I tried using baud rate of 300 b/sec, but no use. So I switched to max 250000 bits/sec, therefore it takes 4 microseconds for one bit. The analog stick values range from -32768 to 32767, which are 16 bit values i.e. 64 microseconds to send one position data.
But pwm of Arduino requires 1ms per cycle.
So, according to my observations, by the time one pwm cycle is completed, around 15 analog instructions are waiting to have their own pwm cycle executed. Therefore, they interrupt the ongoing cycle and start a new cycle in between.
Now, what I was thinking is to grab a data value and let it execute its own analog pwm signal for 10ms and then grab another instantaneous value after it

Oh, I found it! the code is working correctly with every other pwm pin, except pin 11 (I guess its faulty). Also does the code work with other pins if they are made to behave like pwm