setSpeed with brushed motor

total rookie here. Ive been trying to find a way to have 3 different speeds for my bot project that can be changed while operating. Can I use setSpeed and will this code work? What am I missing?

int motorSpeedMax = 250;
int motorSpeedMed = 150;
int motorSpeedMin = 100;
int LRPWM=5
int LLPWM=6
int RPWM=10
int LPWM=11
// timer 0
int LREN=7
int LLEN=8
int REN=12
int LEN=13
char incomingByte; // variable to receive data from the serial port
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps

turn on motor
LLPWM.setSpeed(motorSpeedMed);
LRPWM.setSpeed(motorSpeedMed);
LPWM.setSpeed(motorSpeedMed);
RPWM.setSpeed(motorSpeedMed);
pinMode(LRPWM,OUTPUT);
pinMode(LLPWM,OUTPUT);
pinMode(LLEN,OUTPUT);
pinMode(LREN,OUTPUT);
digitalWrite(LREN,HIGH);
digitalWrite(LLEN,HIGH);
pinMode(RPWM,OUTPUT);
pinMode(LPWM,OUTPUT);
pinMode(LEN,OUTPUT);
pinMode(REN,OUTPUT);
digitalWrite(REN,HIGH);
digitalWrite(LEN,HIGH);

}

void loop() {

if( Serial.available() > 0 ) // if data is available to read
{
incomingByte = Serial.read(); // read it and store it in 'incomingBye"'

}

if( incomingByte == 'i' )

LRPWM.setSpeed(motorSpeedMax);
LLPWM.setSpeed(motorSpeedMax);
LPWM.setSpeed(motorSpeedMax);
RPWM.setSpeed(motorSpeedMax);
}

if( incomingByte == 'o' )
{
LRPWM.setSpeed(motorSpeedMed);
LLPWM.setSpeed(motorSpeedMed);
LPWM.setSpeed(motorSpeedMed);
RPWM.setSpeed(motorSpeedMed);
}

if( incomingByte == 'p' )
{
LRPWM.setSpeed(motorSpeedMin);
LLPWM.setSpeed(motorSpeedMin);
RPWM.setSpeed(motorSpeedMin);
LPWM.setSpeed(motorSpeedMin);
}

if( incomingByte == '1' )
{
analogWrite(LRPWM,Speed);
analogWrite(LLPWM,0);
analogWrite(RPWM,0);
analogWrite(LPWM,Speed);
}

That's one of the famous questions of computer science.

(It won't compile, so "no")

Please remember to use code tags when posting code

1 Like

thanks Im aware. Im having a problem going from point a to point b code tags sure I tried not sure what that entrails. triing to change to a bts7960 motor driver, previous code below

include <Servo.h>
#include <AFMotor.h>
int motorSpeedMax = 250;
int motorSpeedMed = 150;
int motorSpeedMin = 100;
int pos_t = 90; 
int pos_p = 90; 
int doOnce =0;
Servo pan;
Servo tilt;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
char incomingByte; // variable to receive data from the serial port

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps

  // turn on motor
  motor1.setSpeed(motorSpeedMed);
  motor2.setSpeed(motorSpeedMed);
  motor3.setSpeed(motorSpeedMed);
  motor4.setSpeed(motorSpeedMed);
  pan.attach(9); 
  tilt.attach(10); 
  pan.write(pos_p); 
  tilt.write(pos_t); 
  
}

void loop() {
  
  if( Serial.available() > 0 ) // if data is available to read
{
incomingByte = Serial.read(); // read it and store it in 'incomingByte'
doOnce = 0;
}

if( incomingByte == 'i' )
{
 motor1.setSpeed(motorSpeedMax);
 motor2.setSpeed(motorSpeedMax);
 motor3.setSpeed(motorSpeedMax);
 motor4.setSpeed(motorSpeedMax);
}>>>>

Again,code that cannot compile, cannot "work" (whatever "work" means)

that isnt the complete code and 2nd code does compile with a afmotor driver im trying to change the code to work with a different driver. I want to change speeds like the 2nd.

Should I post a picture of my puzzled face?

1 Like

Are you asking if

int LRPWM=5
... 
LRPWM.setSpeed(motorSpeedMax);
...

will work?

No, int does not have members.

AF_DCMotor Class | Adafruit Motor Shield | Adafruit Learning System might have info on setSpeed()

Im trying to go from adafruit driver to a bts7960 driver for higher amps. The adafruit code in second code posting does what i want. I want to be able to choose 3 different speeds but i cant find examples of this for the 7960 or similar l298n. Ive fumbled through code for a few years. now maybe its time to pick up a book. Im finding it hard to find code examples for my project online

If it is a BTS7960 you want, there's a library with an example: https://github.com/luisllamasbinaburo/Arduino-BTS7960/blob/master/examples/BTS7960_PWM/BTS7960_PWM.ino

Im trying to take this code and make it work with 2 bts7960 on a much larger powerful
platform. I have it working with a set speed my problem is how do I setup multiple speeds that can be changed like code below. I dont understand the example code in library mainly this part

for(int speed = 0 ; speed < 255; speed+=10)
  {
	motorController.TurnLeft(speed);
	delay(100);

how can (speed) be set to different variables
thanks for the help

#include <Servo.h>
#include <AFMotor.h>
int motorSpeedMax = 250;
int motorSpeedMed = 150;
int motorSpeedMin = 100;
int pos_t = 90; 
int pos_p = 90; 
int doOnce =0;
Servo pan;
Servo tilt;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
char incomingByte; // variable to receive data from the serial port

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps

  // turn on motor
  motor1.setSpeed(motorSpeedMed);
  motor2.setSpeed(motorSpeedMed);
  motor3.setSpeed(motorSpeedMed);
  motor4.setSpeed(motorSpeedMed);
  pan.attach(9); 
  tilt.attach(10); 
  pan.write(pos_p); 
  tilt.write(pos_t); 
  
}

void loop() {
  
  if( Serial.available() > 0 ) // if data is available to read
{
incomingByte = Serial.read(); // read it and store it in 'incomingByte'
doOnce = 0;
}

if( incomingByte == 'i' )
{
 motor1.setSpeed(motorSpeedMax);
 motor2.setSpeed(motorSpeedMax);
 motor3.setSpeed(motorSpeedMax);
 motor4.setSpeed(motorSpeedMax);
}

if( incomingByte == 'o' )
{
 motor1.setSpeed(motorSpeedMed);
 motor2.setSpeed(motorSpeedMed);
 motor3.setSpeed(motorSpeedMed);
 motor4.setSpeed(motorSpeedMed);
}

if( incomingByte == 'p' )
{
 motor1.setSpeed(motorSpeedMin);
 motor2.setSpeed(motorSpeedMin);
 motor3.setSpeed(motorSpeedMin);
 motor4.setSpeed(motorSpeedMin);
}

  if( incomingByte == '1' )
{
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

  if( incomingByte == '2' )
{
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

  if( incomingByte == '3' )
{
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}


 if( incomingByte == '4' )
{
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}
 
 if( incomingByte == 's' )
{
 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
}

type or paste code here

How can I take this code and make the change from fixed speed to 3 different setting max med min

> //int motorSpeedMax = 250;
> //int motorSpeedMed = 150;
> //int motorSpeedMin = 100;
> #define LRPWM 5
> #define LLPWM 6
> #define LREN 8
> #define LLEN 9
> #define RPWM 10
> #define LPWM 11
> #define REN 12
> #define LEN 13
> 
> char incomingByte; // variable to receive data from the serial port
> 
> void setup() {
>   Serial.begin(9600);           // set up Serial library at 9600 bps
> 
>   pinMode(LRPWM,OUTPUT);
>   pinMode(LLPWM,OUTPUT);
>   pinMode(LLEN,OUTPUT);
>   pinMode(LREN,OUTPUT);
>   digitalWrite(LREN,HIGH);
>   digitalWrite(LLEN,HIGH);
>   pinMode(RPWM,OUTPUT);
>   pinMode(LPWM,OUTPUT);
>   pinMode(LEN,OUTPUT);
>   pinMode(REN,OUTPUT);
>   digitalWrite(REN,HIGH);
>   digitalWrite(LEN,HIGH);
>  
>   
> }
> 
> void loop() {
>   
>   if( Serial.available() > 0 ) // if data is available to read
> {
> incomingByte = Serial.read(); // read it and store it in 'incomingByte'
> 
> }
> 
> if( incomingByte == 'i' )
> {
>  //motor1.setSpeed(motorSpeedMax);
>  //motor2.setSpeed(motorSpeedMax);
>  //motor3.setSpeed(motorSpeedMax);
>  //motor4.setSpeed(motorSpeedMax);
> }
> 
> if( incomingByte == 'o' )
> {
>  //motor1.setSpeed(motorSpeedMed);
>  //motor2.setSpeed(motorSpeedMed);
>  //motor3.setSpeed(motorSpeedMed);
>  //motor4.setSpeed(motorSpeedMed);
> }
> 
> if( incomingByte == 'p' )
> {
>  //motor1.setSpeed(motorSpeedMin);
>  //motor2.setSpeed(motorSpeedMin);
>  //motor3.setSpeed(motorSpeedMin);
>  //motor4.setSpeed(motorSpeedMin);
> }
> 
>   if( incomingByte == '1' )
> {
>  
>   analogWrite(LRPWM,255);
>   analogWrite(LLPWM,0);
>   analogWrite(RPWM,0);
>   analogWrite(LPWM,255);
> }
> 
>   if( incomingByte == '2' )
> {
>   
>   analogWrite(LRPWM,255);
>   analogWrite(LLPWM,0);
>   analogWrite(RPWM,255);
>   analogWrite(LPWM,0);
> }
> 
>   if( incomingByte == '3' )
> {
>   
>   analogWrite(LRPWM,0);
>   analogWrite(LLPWM,255);
>   analogWrite(RPWM,0);
>   analogWrite(LPWM,255);
> }
> 
> 
>  if( incomingByte == '4' )
> {
>   
>   analogWrite(LRPWM,0);
>   analogWrite(LLPWM,255);
>   analogWrite(RPWM,255);
>   analogWrite(LPWM,0);
> }
>  
>  if( incomingByte == 's' )
> {
>  
>  analogWrite(LRPWM,0);
>  analogWrite(LLPWM,0);
>  analogWrite(RPWM,0);
>  analogWrite(LPWM,0);
> }
> }

It is difficult to understant what you have working. Is it that the code with this chunk works for you to produce a single speed?

And you'd like it to work with the 100,150,250 speeds?

If so, I'd choose different letters for incomingByte == '1' and replace the 255 with 100 and 150.

the leters relate to code from a python program with control panel having multiple leters would work but control panel would be a mess. control panel has 3 speed buttons and direction buttons. but thats a small part of the project

basically the code with AFMotor driver allows you to choose a speed fast med and slow then 1 would have either fast med or slow not 255. the 255 is how I have it set up now with the bts7960 driver not the af driver(h293d) so when i o or p is activated thats the speed that 1 2 3 or 4 goes by with the h293d. I want speed control with the bts7960 like I have with the h293d but I cant wrap my head around how to do it. the example code for the bts7950 that ive found all use a pot as input for adjustable speed/direction control. I need it to be adjustable like the h293d code. the below code is with a pot.

#define RPWM 5
#define LPWM 6
#define REN 8
#define LEN 9


int pot;
int out1;
int out2;

void setup() {
  Serial.begin(9600);
  pinMode(RPWM,OUTPUT);
  pinMode(LPWM,OUTPUT);
  pinMode(LEN,OUTPUT);
  pinMode(REN,OUTPUT);
  digitalWrite(REN,HIGH);
  digitalWrite(LEN,HIGH);

}
 
 
void loop() {
  
  pot=analogRead(A0);
  
  if(pot>512){
    out1=map(pot,512,1023,0,255);
    analogWrite(RPWM,out1);
    analogWrite(LPWM,0);
    
  }
  
  if(pot<512){
    out2=map(pot,512,0,0,255);
    analogWrite(LPWM,out2);
    analogWrite(RPWM,0);
    
  }
}

It is still unclear what code you have that works.

Do you have code that works with your panel's buttons?

If the letter code works with your motor, you could add buttons to the letter code conditionals with a logical or operators || like:

  if( incomingByte == '1' || digitalRead(oneButton) == LOW)
  {
  
    analogWrite(LRPWM,255);
    analogWrite(LLPWM,0);
    analogWrite(RPWM,0);
    analogWrite(LPWM,255);
  }

...but looking at the code looks like you should sure that incomingByte is cleared somewhere so you don't have old conflicting commands still in operation. I'd either move char incomingByte; inside of loop(){...} so it won't persist, or add an else clause to the Serial.available() conditional to clear it.

i think maybe I should have started this differently. I want to send a serial command to set pwm of my motors to move fast med or slow. this would change the code globally so forward reverse left right all change at the same time. Im trying to do this because I cant find examples of a web based virtual joystick im stuck with buttons right now. Ive been reading alot on web control but only found buttons. this started as a rpi went to jetson nano but now I need to add arduino because I dont have enough pwm pins to do what i need. The python side is pretty good, facial recognition with custom voice greetings, object following, all web based controll and streaming from a web server on any device, two way audio opencv tensorflow machine learning training modules my heads going to explode. Its a telepresence experiment thats gone out of control. the issue that i didnt think to much about is motion needs when moving to a high amp motor driver. Im on a tight budget so the bts7960 fit but needs more pwm then I have available with just the rpi/jetson. its a large rover that will get more complicated

HI,
How about firstly you post a schematic of your project?
An image of a drawn circuit will be fine, include power supplies, component names and pin labels.

If you are using the BTS7960 motor driver, have you Googled

arduino bts7960

There you will find tutorials and sample code on how to drive them.
Using <AFMotor.h> will probably not work on anything but the AF motor driver shield.

Thanks... Tom... :grinning: :+1: :coffee: :australia:

Well, if you already have code that might respond to serial commands, then adding physical buttons to it doesn't look difficult. And for the web-based virtual joystick, you could use the same serial commands.

If you've got a pot-based BTS7960 script working with one of your motors, moving that motor functionality into your otehr code should be easy too.

If the hold up is the three levels, maybe something like this could work:

int activeMotorSpeed = 0;
...
if( incomingByte == 'p' ) 
{
   activeMotorSpeed = 100;
}
if( incomingByte == 'o' ) 
{
   activeMotorSpeed = 150;
}
...

if( incomingByte == '1' )
{
   analogWrite(LRPWM,activeMotorSpeed);
   analogWrite(LLPWM,0);
   analogWrite(RPWM,0);
   analogWrite(LPWM,activeMotorSpeed);
}
...

Davex exactly what i was looking for thankyou
Im at a loss for writing code but im learning. the mechanical side Im good its what i do for a living. Any good books i should read that would help? It seems everything i find online is a little different then i need and my ability to adapt that to what i need doesnt always turnnout right. Ill test out the code and let you know how it goes. Are there virtual build enviroments to test projects before implementarion?
Thanks davex

Got my code finished up and everything works good of course now id like to move to a joystick x y control but for now im working on self driving automation so i can click a button on control panel and it will map and drive autonomously using lidar.