Code doesn't work

I’ve got a differential drive code that doesn’t seem to work.

I’ve sent the prospective outputs to the serial monitor, it is confirming that sometimes the if statements are sending data, but it sends 0’s.

I want to have a joystick control the two motorspeeds based on an angular velocity derived from mapping the x,y of the joystick to velocity y, and distance to center of turn radius x.

Is there a common way to do this sort of simplified test? It would produce an RC type control (granted the joystick is hooked to the car).

int rightmotorpinf = 3;
int rightmotorpinb = 5;
int leftmotorpinf  = 6 ;
int leftmotorpinb =  7 ;
int joyturnangle = A0;
int joyvelocity = A1;


void setup() {
  // put your setup code here, to run once
pinMode (joyturnangle, INPUT);
pinMode (joyvelocity, INPUT);
 pinMode (rightmotorpinf, OUTPUT);
 pinMode (leftmotorpinf, OUTPUT);
 pinMode (rightmotorpinb, OUTPUT);
 pinMode (leftmotorpinb, OUTPUT); 
 Serial.begin (9600);
}

void loop() {
  // put your main code here, to run repeatedly:
   float angularvelocity,velocityright, velocityleft;
   joyturnangle = analogRead(A0);
 joyvelocity = analogRead(A1);
map (joyvelocity, 0, 1023, -200, 200);
map (joyturnangle, 0, 1023, -32000, 32000);
float (angularvelocity = (joyvelocity/joyturnangle)); 
int (velocityright = angularvelocity * (joyturnangle + 5));
int  (velocityleft = angularvelocity * (joyturnangle - 5));
if (velocityright >= 10 && velocityright <= 200 && velocityleft >= 10 && velocityleft <= 200)
{
analogWrite (rightmotorpinf, velocityright);
analogWrite (leftmotorpinf, velocityleft);
analogWrite (rightmotorpinb, 0);
analogWrite (leftmotorpinb, 0);
Serial.print (velocityleft);
Serial.print ("     ");
Serial.println (velocityright);
}
if (velocityright >= -200 && velocityright <= -10&&velocityleft >= -200 && velocityleft <= -10)
{
  analogWrite (rightmotorpinb, -velocityright);
  analogWrite ( leftmotorpinb, -velocityleft);
  analogWrite (rightmotorpinf, 0);
  analogWrite (leftmotorpinf, 0);
  Serial.print (velocityleft);
Serial.print ("     ");
Serial.println (velocityright);
}
 

if (velocityleft >= -10 && velocityleft <= 10 && velocityright >= -10 && velocityright <= 10)
{
  analogWrite (leftmotorpinf, 0);
  analogWrite (rightmotorpinf, 0);
  analogWrite (leftmotorpinb, 0);
  analogWrite (rightmotorpinb, 0);
  Serial.print (velocityleft);
Serial.print ("     ");
Serial.println (velocityright);
}

delay (50);
}
map (joyvelocity, 0, 1023, -200, 200);

map (joyturnangle, 0, 1023, -32000, 32000);

This isn't doing what you think it is, I suspect. Functions return values. The general idea is to do something with those values. For example, you can write:

  2+2;

Sure, that evaluates to 4, but you aren't putting the result anywhere.

I don't want to sound snarky, but you'll find things a lot easier if you do a few C (or C++) tutorials. You can't just make stuff up. There are ways things work, and you don't seem to understand them. For example:

int (velocityright = angularvelocity * (joyturnangle + 5));

What is "int" doing there?

I chucked that in there after it didn't work.,,, It wasn't in the first runs,, it just made it there at the end. , I did a bunch of honkey stuff.

However, my equations should be returning values that fall within my "if" statements,, and on the last code I made with such statements, I got motor action. (a different rig, just one motor through an H bridge,,,, However i'm sure my H bridges will make motor motions, because I'm getting motor motions ,, just the wrong ones. )

As @NickGammon says, this probably doesn't do what you expect it to do. So what do you expect the below to do?

  map (joyvelocity, 0, 1023, -200, 200);

If you expect it to change joyvelocity, you are wrong.

See what the below code displays on the serial monitor and draw your conclusions :wink:

void setup()
{
  Serial.begin(115200);
  int joyvelocity = analogRead(A0);
  Serial.print("measured joyvelocity   = "); Serial.println(joyvelocity);
  map (joyvelocity, 0, 1023, -200, 200);
  Serial.print("mapped (?) joyvelocity = "); Serial.println(joyvelocity);
  Serial.print("mapped (!) joyvelocity = "); Serial.println(map(joyvelocity, 0, 1023, -200, 200));
}

Also see map() reference. I quote the relevant part for this

Returns

The mapped value.

Next get rid of the none necessary 'float', 'int' that Nick pointed out.

sterretje:
As @NickGammon says, this probably doesn't do what you expect it to do. So what do you expect the below to do?

  map (joyvelocity, 0, 1023, -200, 200);

If you expect it to change joyvelocity, you are wrong.

See what the below code displays on the serial monitor and draw your conclusions :wink:

Ok, I'm using a rendition of your code,, I put some delay and some extra lines so I can see it.

It IS doing what I wanted. I want the two joystick inputs to be converted to the following values, -255 (reverse) to 255 (forward),,, (I will then negative the reverse analogWrite so that the value is positive once it is a PWM command),,,, and the turn circle radius,, where I chose to use a number close to the maximum possible int,, 32000 (cm,,, I am going to assume cm for the other distance measurements as well). ( I hope that 32000 centimeters is a large enough radius to provide a nice shallow turn when desired).


I will go ahead and try some different things at the moment. I've been having to realize that my Hbridge was actually not good,, it was SO broken that the motors WOULD spin for some activity, but not CORRECTLY,, just some random erroneous behavior. ,,, Now they are good, and using other codes to simply write a speed to a motor output, I can drive the motors about above 100 PWM. (I would like it to be more sensitive, but that's not for now to worry about).

I think my problem is in the equation by now. I'm reading the serial and seeing values I would have expected. I am sure I have problems just everywhere, but that particular equation code must not be working.

I've been having to realize that my Hbridge was actually not good

Once you have fixed the code, you may need to brush up about motors, motor power supplies and H-bridges. All have certain characteristics and limitations, and must be chosen appropriately, or nothing will work.

I made the H bridge out of transistors. It’s all fine. I took apart a “thunder Tumbler” and left the motors in place, and then hooked that up to my breadboard H bridge. the point of the project is to get any functionality, so I’m not terribly concerned with performance, and I have verified that the Arduino PWM signal will in fact drive the H bridge I podged together.

actually it looks nice! You can see I screwed a board to the body of the tumbler, and maybe can’t see that I then put a caster on a post on the other side of the board. I’m using the battery pack from the tumbler to power the motors, and WAS going to use the transistors but I burned one of them.

I did try to hook my arduino up to the controller, such that the PWM would control a transistor that connected to the button input, in hopes that I could just use the tumbler hardware and have the arduino only handheld. It didn’t work as I planned, and the transistor started smoking pretty good before I got too thorough. , It might still work but when I hook things up to the transmitter, it does funny business, probably charging capacitors in weird places.

so should I instead say "mapped joyvelocity"???
like this:

(angularvelocity = (mapped joyvelocity/mapped joyturnangle));

sterretje:
As @NickGammon says, this probably doesn't do what you expect it to do. So what do you expect the below to do?

  map (joyvelocity, 0, 1023, -200, 200);

If you expect it to change joyvelocity, you are wrong.

See what the below code displays on the serial monitor and draw your conclusions :wink:

void setup()

{
  Serial.begin(115200);
  int joyvelocity = analogRead(A0);
  Serial.print("measured joyvelocity  = "); Serial.println(joyvelocity);
  map (joyvelocity, 0, 1023, -200, 200);
  Serial.print("mapped (?) joyvelocity = "); Serial.println(joyvelocity);
  Serial.print("mapped (!) joyvelocity = "); Serial.println(map(joyvelocity, 0, 1023, -200, 200));
}




Also see [map()](http://file:///C:/Program%20Files%20(x86)/Arduino/reference/www.arduino.cc/en/Reference/Map.html) reference. I quote the relevant part for this
Next get rid of the none necessary 'float', 'int' that Nick pointed out.

tsmspace:
Ok, I’m using a rendition of your code, I put some delay and some extra lines so I can see it.

It IS doing what I wanted. I want the two joystick inputs to be converted to the following values, -255 (reverse) to 255 (forward),

Without seeing newer code, there is no way for us to see if you understood what the problem was :wink:

tsmspace:
so should I instead say “mapped joyvelocity”???
like this:

(angularvelocity = (mapped joyvelocity/mapped joyturnangle));

Something like that, yeah. And get rid of that extra ( at the begin and that extra ) at the end.

sterretje:
Without seeing newer code, there is no way for us to see if you understood what the problem was :wink:

So, I haven’t yet tried to work out my math error, but I found it. My velocities are NOT falling within my if statements, they are in the 10s of thousands, for a centered stick.

I put a bunch of serial.prints, and came up with this return cascade.
551 (joyturnangle) 521 (joyvelocity)
2471 (jt = mapped joyturnangle) 4 (jv = mapped joyvelocity)
617.00 (angularvelocity)
1521522.00------1527692.00\\\ (velocityright — velocityleft)
etc.

537 541
1595 14
113.00
179670.00------180800.00\\\

these two excerpts are two prints. I have added info in parenthesis (the id’s of each number)
of course I will re-evaluate my math, I did do calculator math before entering the equation and result in acceptable values, so I don’t know what I was up to before vs. now… seems so small, but anyway I’m just replying to post the code and its status.

you’ll notice none of the if statements returned a serial print. none of them were triggered.

int rightmotorpinf = 3;
int rightmotorpinb = 5;
int leftmotorpinf  = 6 ;
int leftmotorpinb =  7 ;
int joyturnangle = A0;
int joyvelocity = A1;


void setup() {
  // put your setup code here, to run once
pinMode (joyturnangle, INPUT);
pinMode (joyvelocity, INPUT);
 pinMode (rightmotorpinf, OUTPUT);
 pinMode (leftmotorpinf, OUTPUT);
 pinMode (rightmotorpinb, OUTPUT);
 pinMode (leftmotorpinb, OUTPUT); 
 Serial.begin (9600);
}

void loop() {
  // put your main code here, to run repeatedly:
   float angularvelocity,velocityright, velocityleft;
   joyturnangle = analogRead(A0);
 joyvelocity = analogRead(A1);
 Serial.print (joyturnangle);
 Serial.print ("      ");
 Serial.println (joyvelocity);
int jv = map (joyvelocity, 0, 1023, -255, 255);
int jt = map (joyturnangle, 0, 1023, -32000, 32000);
Serial.print (jt);
Serial.print ("     ");
Serial.println (jv);
(angularvelocity = (jt/jv)); 
Serial.println (angularvelocity);
float (velocityright = angularvelocity * (jt + 5));
float (velocityleft = angularvelocity * (jt - 5));
Serial.print (velocityleft);
Serial.print ("------");
Serial.print (velocityright);
Serial.println("\\\\\\\\\\");
if (velocityright >= 10 && velocityright <= 200 && velocityleft >= 10 && velocityleft <= 200)
{
analogWrite (rightmotorpinf, velocityright);
analogWrite (leftmotorpinf, velocityleft);
analogWrite (rightmotorpinb, 0);
analogWrite (leftmotorpinb, 0);
Serial.print (velocityleft);
Serial.print ("     ");
Serial.println (velocityright);
}
if (velocityright >= -200 && velocityright <= -10&&velocityleft >= -200 && velocityleft <= -10)
{
  analogWrite (rightmotorpinb, -velocityright);
  analogWrite ( leftmotorpinb, -velocityleft);
  analogWrite (rightmotorpinf, 0);
  analogWrite (leftmotorpinf, 0);
  Serial.print (velocityleft);
Serial.print ("     ");
Serial.println (velocityright);
}
 

if (velocityleft >= -10 && velocityleft <= 10 && velocityright >= -10 && velocityright <= 10)
{
  analogWrite (leftmotorpinf, 0);
  analogWrite (rightmotorpinf, 0);
  analogWrite (leftmotorpinb, 0);
  analogWrite (rightmotorpinb, 0);
  Serial.print (velocityleft);
Serial.print ("     ");
Serial.println (velocityright);
}

delay (1000);
}

Get rid if some of the crap :wink:

Change

(angularvelocity = (jt/jv));
Serial.println (angularvelocity);
float (velocityright = angularvelocity * (jt + 5));
float (velocityleft = angularvelocity * (jt - 5));

to

angularvelocity = (jt/jv);
Serial.println (angularvelocity);
velocityright = angularvelocity * (jt + 5))
velocityleft = angularvelocity * (jt - 5))

Can't say if it will solve you problem but it just looks so unnatural.

I got rid of a different line, and left the word float, it didn’t work for me to double declare, but then it didn’t seem to work at all, then it worked again if I just put float at the first use of the variable. s, so thats where it is.
I’m running the code, and everything is working until the angularvelocity calculation, where it returns 0 no matter what joyvalues there are., resulting in a zero motion of the wheels.

(at some point I had jt/jv, instead of jv/jt, but that’s all better now.)

here’s the code again, I don’t understand why the angularvelocity is always zero, I feel , so…

int rightmotorpinf = 3;
int rightmotorpinb = 5;
int leftmotorpinf  = 6 ;
int leftmotorpinb =  7 ;
int joyturnangle = A0;
int joyvelocity = A1;


void setup() {
  // put your setup code here, to run once
pinMode (joyturnangle, INPUT);
pinMode (joyvelocity, INPUT);
 pinMode (rightmotorpinf, OUTPUT);
 pinMode (leftmotorpinf, OUTPUT);
 pinMode (rightmotorpinb, OUTPUT);
 pinMode (leftmotorpinb, OUTPUT); 
 Serial.begin (9600);
}

void loop() {
  // put your main code here, to run repeatedly:
   
   joyturnangle = analogRead(A0);
 joyvelocity = analogRead(A1);
 Serial.print (joyturnangle);
 Serial.print ("      ");
 Serial.println (joyvelocity);
int jv = map (joyvelocity, 0, 1023, -255, 255);
int jt = map (joyturnangle, 0, 1023, -32000, 32000);
Serial.print (jt);
Serial.print ("     ");
Serial.println (jv);
float angularvelocity = jv/jt; 
Serial.println (angularvelocity);
float velocityright = angularvelocity * (jt + 5);
float velocityleft = angularvelocity * (jt - 5);
Serial.print (velocityleft);
Serial.print ("------");
Serial.print (velocityright);
Serial.println("\\\\\\\\\\");
if (velocityright >= 10 && velocityright <= 200 && velocityleft >= 10 && velocityleft <= 200)
{
analogWrite (rightmotorpinf, velocityright);
analogWrite (leftmotorpinf, velocityleft);
analogWrite (rightmotorpinb, 0);
analogWrite (leftmotorpinb, 0);
Serial.print (velocityleft);
Serial.print ("     ");
Serial.println (velocityright);
}
if (velocityright >= -200 && velocityright <= -10&&velocityleft >= -200 && velocityleft <= -10)
{
  analogWrite (rightmotorpinb, -velocityright);
  analogWrite ( leftmotorpinb, -velocityleft);
  analogWrite (rightmotorpinf, 0);
  analogWrite (leftmotorpinf, 0);
  Serial.print (velocityleft);
Serial.print ("     ");
Serial.println (velocityright);
}
 

if (velocityleft >= -10 && velocityleft <= 10 && velocityright >= -10 && velocityright <= 10)
{
  analogWrite (leftmotorpinf, 0);
  analogWrite (rightmotorpinf, 0);
  analogWrite (leftmotorpinb, 0);
  analogWrite (rightmotorpinb, 0);
  Serial.print (velocityleft);
Serial.print ("     ");
Serial.println (velocityright);
}

delay (1000);
}