5

I am controlling 4 motors using the PWM ports on an Arduino Uno (3,9,10,11). For some reason, the motor hooked up to port 11 doesn't move with my current code. I know it's a code issue because I can create a new basic code that just moves the motor and it works fine, but for some reason it doesn't in this code.

#include <SPI.h>  
#include <Pixy.h>

Pixy pixy;

int personX = 0;
int offCount = 0;

void setup() 
{ 
  pinMode(3, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  Serial.begin(9600);
  while (! Serial);
  pixy.init();
}  

void loop() 
{  
  static int i = 0;
  int j;
  uint16_t blocks;
  char buf[32]; 

  blocks = pixy.getBlocks();

  if (blocks)
  {
    personX = pixy.blocks[0].x;
  }

  if (blocks){
    Serial.println(personX);
    if (personX < 110){
      analogWrite(11, 130);
      analogWrite(3, 130);
      analogWrite(9, 250);
      analogWrite(10, 250);
    } else if (personX > 190){
      analogWrite(11, 250);
      analogWrite(3, 250);
      analogWrite(9, 130);
      analogWrite(10, 130);
    } else {
      analogWrite(11, 190);
      analogWrite(3, 190);
      analogWrite(9, 190);
      analogWrite(10, 190);
    }
    offCount = 0;
  } else {
    if (offCount < 1000){
      offCount = offCount + 1;
    } else {
      analogWrite(11, 190);
      analogWrite(3, 190);
      analogWrite(9, 190);
      analogWrite(10, 190);
    }
  }
} 

Also, when I take the motor and swap it with another, the old one works in the new PWM port, but the new one stops working once I put it in port 11.

9

Pin 11 is shared between both PWM and SPI. By activating SPI you've disabled PWM from working on that pin. Either use another PWM pin instead, or switch to software SPI on different pins.

  • Thanks for the info! I realized that I can just split the PWM from 3 to also control the motor previously hooked up to port 11, but because I will need to use 11 at some point in the future, I will need to know how to use SPI. I am new the Arduino so I had to google what SPI is, but how do I switch to software SPI, and will it still work with the Pixy camera? – Leo Adberg Dec 17 '14 at 2:57
  • I recommend not using software SPI if you can help it. Instead either pick a MCU with more PWM channels (e.g. the ATmega1280 in the Arduino Mega or the ATmega128A found in... no Arduino that I know of), or find an external PWM controller that can be controlled via SPI or I2C. – Ignacio Vazquez-Abrams Dec 17 '14 at 3:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.