Hi guys,
I have been building my robot using a USB host shield (which uses pins 10-13), which leaves me 4 left over PWM pins (pins 3, 5, 6, and 9). I want to use these four pins to drive an h-bridge so my motors can have varying speeds. Unfortunately, I ran into an issue with pin 9. For some reason, I my code doesn't work when I output through pin 9. When I run the code (which is here:#define motor1Pin1 9#define motor1Pin2 3#define motor2Pin1 6#define motor2 - Pastebin.com), the motor direction controlled by pin 9 doesn't work. This is strange because I just tested pin 9 by changing the intensity of an LED and it worked fine. If i change it to pin 8, it works fine (I want the PWM feature, though). Is there something wrong with my code? The pin seems like it's working fine, and I swapped out the H-bridges to make sure it wasn't that. Furthermore, If I write a program to analogaly write to the motor through pin 9 it works fine, which makes me think that it is a code issue. I greatly appreciate any help.
Thanks again!
#define motor1Pin1 9
#define motor1Pin2 3
#define motor2Pin1 6
#define motor2Pin2 5
//#define redLed 4
//#define greenLed 5
//#define blueLed 6
#include <PS3USB.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
USB Usb;
/* You can create the instance of the class in two ways */
PS3USB PS3(&Usb); // This will just create the instance
//PS3USB PS3(&Usb,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
boolean printAngle;
uint8_t state = 0;
void setup()
{
pinMode(motor1Pin1, OUTPUT); //this is an output because the Arduino is telling the motor to spin
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
//pinMode(redLed, OUTPUT);
//pinMode(greenLed, OUTPUT);
//pinMode(blueLed, OUTPUT);
Serial.begin(115200);
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nPS3 USB Library Started"));
}
void loop()
{
Usb.Task();
if (PS3.PS3Connected || PS3.PS3NavigationConnected)
{
if (PS3.getButtonPress (R1))
{
GoForward();
//Serial.print(F("\r\nR1"));
}
if (PS3.getButtonPress (L1))
{
GoBackward();
//Serial.print(F("\r\nL1"));
}
if (PS3.getAnalogHat(LeftHatX) == 255)
{
GoRight();
//Serial.print(F("\r\nLeftHatX: "));
//Serial.print(PS3.getAnalogHat(LeftHatX));
}
if (PS3.getAnalogHat(LeftHatX) == 0)
{
GoLeft();
// Serial.print(F("\r\nLeftHatX: "));
//Serial.print(PS3.getAnalogHat(LeftHatX));
}
if (PS3.getButtonClick (CIRCLE))
{
Stop();
}
}
}
void GoForward()
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
void GoBackward()
{
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
void Stop()
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
void GoLeft()
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
void GoRight()
{
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
I don't see anything obvious in the code.
Are you sure there are no wiring errors?
CrossRoads:
I don’t see anything obvious in the code.
Are you sure there are no wiring errors?
Yes, I am sure. If I make a new program that just outputs to pin 9, then it works fine. Only when I incorporate it into the final code does it not work. Also for some reason when I am uploading a new sketch pin 9 outputs, which is weird.
Next guess would be something in the libraries then.
Open these up & browse around:
PS3USB.h
spi4teensy3.h
Teensy3 pinout shows pin9 was being one of the SPI CS pins...
jaccoob23:
This is strange because I just tested pin 9 by changing the intensity of an LED and it worked fine. If i change it to pin 8, it works fine (I want the PWM feature, though). Is there something wrong with my code?
Well, if you want to output PWM on a PWM pin, analogWrite() is what you use.
You are doing a digitalWrite().
If you were able to fade your LED in a separate sketch, using PWM, you must have been using analogWrite(), right?
westfw:
Teensy3 pinout shows pin9 was being one of the SPI CS pins...
Can you explain what this means, and what I can do to fix it? Sorry, I'm sort of new here.
Can you explain what this means, and what I can do to fix it?
Do you, in fact, have a teensy? If so, you really should be asking your question on the teensy forum.
PaulS:
Do you, in fact, have a teensy? If so, you really should be asking your question on the teensy forum.
No, I am just using a usb host shield and an Arduino.
No, I am just using a usb host shield and an Arduino.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
What’s this shit in your code, then?