Hello guys.It's my first post so be nice with me
I want to control the speed (PWM) of an dc motor with the joystick of PS3 like this video but i can't do it. I have searched in the internet but i couldn't find anything.I have write a code for controling the speed of an dc motor with the joystick but it doesn't work.Can you tell me please what am i doing wrong?Thank you.
https://www.youtube.com/watch?v=ozztMo9F1Qo
#include <PS3BT.h>
USB Usb;
BTD Btd(&Usb);
PS3BT PS3(&Btd);
int dirA = 2;
int dirB = 4;
int PWMA = 3;
int PWMB = 5;
void setup() {
//Serial.begin(115200);
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nPS3 Bluetooth Library Started"));
pinMode(dirA, OUTPUT);
pinMode(dirB, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
}
void loop()
{
Usb.Task();
if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
if (PS3.getAnalogHat(LeftHatY) < 120) {
for (int i = 0; i < 256; i++) // Accelerate from zero to maximum speed
{
digitalWrite(dirA, HIGH);
analogWrite(PWMA, i);
}
}
else if (PS3.getAnalogHat(LeftHatY) > 135)
{
for (int i = 255; i >= 0; --i) // Decelerate from maximum speed to zero
{
analogWrite(dirA, LOW);
analogWrite(PWMA, i);
}
}
else
{
analogWrite(dirA, 0);
analogWrite(PWMA, 0);
}
if (PS3.getButtonClick(PS)) {
PS3.disconnect();
}
}
}
it doesn't work
What does it do ? What should it do ?
Have you tried printing the values returned by the library functions ? Are they what you expect ?
digitalWrite(dirA, HIGH);
analogWrite(PWMA, i);
It seems a little unnecessary to execute both of these commands 255 times particularly as there is no time delay between the Write()s so they will occur within milliseconds.
UKHeliBob:
What does it do ? What should it do ?
Have you tried printing the values returned by the library functions ? Are they what you expect ?
digitalWrite(dirA, HIGH);
analogWrite(PWMA, i);
It seems a little unnecessary to execute both of these commands 255 times particularly as there is no time delay between the Write()s so they will occur within milliseconds.
Thank you for your reply.The code now goes forward and backward with full speed(255).I tried with delay but the motors continue to spin with full speed.Yes the values are fine.
The code now goes forward and backward with full speed(255)
No surprise there as the for loops will be complete in a few microseconds
I tried with delay but the motors continue to spin with full speed.
Post the whole program that you tried.
Νow the code does not work but before it works.Now the motors make a noise.I can't understand why it doesn't work.Anyway the code is this.Thank you for your help.
#include <PS3BT.h>
USB Usb;
BTD Btd(&Usb);
PS3BT PS3(&Btd);
int dirA = 2;
int dirB = 4;
int PWMA = 3;
int PWMB = 5;
void setup() {
//Serial.begin(115200);
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nPS3 Bluetooth Library Started"));
pinMode(dirA, OUTPUT);
pinMode(dirB, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
}
void loop()
{
Usb.Task();
if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
if (PS3.getAnalogHat(LeftHatY) < 120) {
for (int i = 0; i < 256; i++) // Accelerate from zero to maximum speed
{
digitalWrite(dirA, HIGH);
analogWrite(PWMA, i);
Serial.print(F("\tLeftHatY: "));
Serial.print(PS3.getAnalogHat(LeftHatY));
delay(20);
}
}
else if (PS3.getAnalogHat(LeftHatY) > 135)
{
for (int i = 255; i >= 0; --i) // Decelerate from maximum speed to zero
{
digitalWrite(dirA, LOW);
analogWrite(PWMA, i);
Serial.print(F("\tLeftHatY: "));
Serial.print(PS3.getAnalogHat(LeftHatY));
delay(20);
}
}
else
{
digitalWrite(dirA, 0);
analogWrite(PWMA, 0);
}
if (PS3.getButtonClick(PS)) {
PS3.disconnect();
}
}
}
Personally I would try something like this (untested)
int leftHatValue = PS3.getAnalogHat(LeftHatY);
Serial.print(F("\tLeftHatY: "));
Serial.println(leftHatValue);
if (leftHatValue < 120)
{
digitalWrite(dirA, HIGH);
Serial.println(F("Increasing"));
for (int i = 0; i < 256; i++) // Accelerate from zero to maximum speed
{
analogWrite(PWMA, i);
delay(100);
}
Leave the delay() at 100 until you know that it works then adjust it or change to timing with millis()
UKHeliBob:
Personally I would try something like this (untested)
int leftHatValue = PS3.getAnalogHat(LeftHatY);
Serial.print(F("\tLeftHatY: "));
Serial.println(leftHatValue);
if (leftHatValue < 120)
{
digitalWrite(dirA, HIGH);
Serial.println(F("Increasing"));
for (int i = 0; i < 256; i++) // Accelerate from zero to maximum speed
{
analogWrite(PWMA, i);
delay(100);
}
Leave the delay() at 100 until you know that it works then adjust it or change to timing with millis()
No it doesn't work.The motors make a noise and the connection between PS3 controller and Arduino is lost.
What do you see if you print the value returned by PS3.getButtonClick(PS) ?
Actually i want to gradually increase an DC motor speed from 0-255 (with PS3 Controller) like the guy on the video.I want to do exactly the same.Thank you again for your help.
https://www.youtube.com/watch?v=E8DfnvyXc5s
Actually i want to gradually increase an DC motor speed from 0-255
Forget about the controller for now. Write a small program that simply ramps the speed from 0 to 255 and back down again and does no more. Put the code for increasing and decreasing the speed in separate functions and you can use it later in the bigger program.
When that works you can start to involve the controller
If i run this code the motor speed increase and decrease gradually.I have problem when i try to increase and decrease the motor speed with joystick of PS3.Actually the LeftHatY command appear some readings on the serial monitor (numbers) that change all the time.Never is the same readings and that is my problem.if the readings were the same all the time then i would hadn't problem.
int dirA = 2;
int dirB = 4;
int PWMA = 3;
int PWMB = 5;
void setup()
{
pinMode(dirA, OUTPUT);
pinMode(dirB, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
}
void loop()
{
for (int i = 0; i < 256; i++) // Accelerate from zero to maximum speed
{
digitalWrite(dirA, HIGH);
analogWrite(PWMA, i);
delay(20);
}
for (int i = 255; i >= 0; --i) // Decelerate from maximum speed to zero
{
digitalWrite(dirA, LOW);
analogWrite(PWMA, i);
delay(20);
}
}
ArduinoKid1992:
Actually i want to gradually increase an DC motor speed from 0-255 (with PS3 Controller) like the guy on the video.I want to do exactly the same.Thank you again for your help.
https://www.youtube.com/watch?v=E8DfnvyXc5s
I watched the video. I do not see a gradual increase of the motor speed.
I see a mapping of the joystick position to the motor speed.
vinceherman:
I watched the video. I do not see a gradual increase of the motor speed.
I see a mapping of the joystick position to the motor speed.
Thank you for your reply.How works the map command?
vinceherman:
This should help. 
ok thank you for your help sir