A little help with motor control for L298n motor driver

So i've been trying to make this table tennis robot that shoots balls automatically which i can control through an app in my smart phone. This is a school project of mine which is due 1 week from now. I ran into a bit of a problem with controlling the two motors for my launcher. Here's the section of the code that i'm having trouble with:

if (val >= 2000 && val < 2255)
{
int ballMapped = val;
ballMapped = map(ballMapped, 2000, 2255, 0, 255);
if (spinType == "topspin") {
motorSpeed1 = 255;
motorSpeed2 = 0;
motorSpeed2 = motorSpeed2 + ballMapped;
if (motorSpeed2 > 120 ) {
motorSpeed2 = 120;
}
}
if (spinType == "underspin") {

motorSpeed2 = 255;
motorSpeed1 = 0;
motorSpeed1 = motorSpeed1 + ballMapped;
if (motorSpeed1 > 120) {
motorSpeed1 = 120;
}
}

What this does is it takes the variable "val" which is from my smartphone app, it is a slider with values ranging from 2000 - 2255. Then I mapped those values to 0 - 255 which i put into ballMapped. There's also a checkbox in my smartphone that sends the word "topspin" or "underspin". Now if for example i checked topspin on the smartphone app, motor1 would spin at full speed because of motorspeed1 = 255 while motorspeed2 will be controlled by the slider. Now here's the problem whenever i check underspin, motor2 will now spin at full speed but now motor1 can't be controlled by the slider, it won't even turn on. Does anyone know how to solve this or at least have another idea on how to control these two motors? Help would be very much appreciated.

UPDATE:
I found the culprit. Servo.attach was actually interfering with the motors, I did a little research and turns out that you can't use pin 9,10 for pwm if you're using the servo library. After that, I just rewired enA,in1,and in2 to pins 11,12,and 13, and thank God, it worked. Thank you so much for everyone who helped, I really appreciate it.

Put a few Serial prints in to see if a) you ever get into that code and b) what actual values you are getting.

Steve

slipstick:
Put a few Serial prints in to see if a) you ever get into that code and b) what actual values you are getting.

Steve

I did that and got the right values but the motor still doesn't spin. I can control the speed of the other motor just fine though and I also tested the motor that doesn't spin with another sketch that i just put bare values for the speed and works fine, I just can't control it.

So you're saying that the underspin bit works fine when setting motorSpeed1 but topspin doesn't work setting motorSpeed2.

In that case my best guess is that motorSpeed2 is NOT the variable that is actually controlling the speed of that motor. In other words the problem is somewhere else in the rest of the program you didn't bother posting.

Steve

@OP

To receive meaningful help, please post the complete program with code tags (>/>).

slipstick:
So you're saying that the underspin bit works fine when setting motorSpeed1 but topspin doesn't work setting motorSpeed2.

In that case my best guess is that motorSpeed2 is NOT the variable that is actually controlling the speed of that motor. In other words the problem is somewhere else in the rest of the program you didn't bother posting.

Steve

Actually, you had it backwards, I can still control motorSpeed2 on topspin but can't control motorSpeed1 on underspin but anyways it's still the same problem. Sorry about not posting the whole code. Here it is, Maybe you can help me find the problem, i'd really appreciate it.

#include <Servo.h>
//for shooter head position
Servo servo1;
//for ball feeder
Servo servo2;

//servo motor initial positions
int servo1Pos = 0;
int servo2Pos = 0;
//Dc motor 1
int enA = 8;
int in1 = 10;
int in2 = 11;
//Dc motor 2
int enB = 5 ;
int in3 = 6;
int in4 = 7;
//motor speeds
int motorSpeed1;
int motorSpeed2;
//functions for changing spins and changing to manual mode or drill mode
String spinType = "topspin";
//int topspin(int x);
//int underspin(int w);
int drills(int w);

unsigned int val;

void setup() {
Serial.begin(9600);
servo1.attach(4);
servo2.attach(3);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

}

void loop() {
if (Serial.available() >= 2)
{
unsigned dataIn = Serial.read();
unsigned dataIn2 = Serial.read();
val = (dataIn2 * 256) + dataIn;
Serial.println(val);
}

//shooter head position
if (val >= 1000 && val < 1180) {
int servo1pos = val;

servo1pos = map(servo1pos, 1000, 1105, 0, 105 );
servo1.write(servo1pos);
delay(10);
}
//ball feeder
if (val >= 3000 && val < 3180)
{
int servo2pos = val;
servo2pos = map(servo2pos, 3000, 3180, 0, 180);
servo2.write(servo2pos);
}

//
if (val >= 2000 && val < 2255)
{
int ballMapped = val;
ballMapped = map(ballMapped, 2000, 2255, 0, 255);
if (spinType == "topspin") {
motorSpeed1 = 255;
motorSpeed2 = 0;
motorSpeed2 = motorSpeed2 + ballMapped;
if (motorSpeed2 > 120 ) {
motorSpeed2 = 120;
}
}
if (spinType == "underspin") {

motorSpeed2 = 255;
motorSpeed1 = 0;
motorSpeed1 = motorSpeed1 + ballMapped;
if (motorSpeed1 > 120) {
motorSpeed1 = 120;
}
}
// Serial.println(motorSpeed1);
// Serial.println(motorSpeed2);
// Serial.println(ballMapped);

}

if (val == 5000) {
spinType = "topspin";
}
if (val == 6000) {
spinType = "underspin";
}

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, motorSpeed1);

digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enB, motorSpeed2);

}

int drills(int y) {
switch (y) {
//Topspin Drill
case 7000:
// motorSpeed1 = 200;
// motorSpeed2 = 90;
//kulang pa
break;
//Underspin Drill
case 8000:
// motorSpeed1 = 90;
// motorSpeed2 = 200;
//kulang pa
break;
//No spin Drill
case 9000:
// motorSpeed1 = 120;
// motorSpeed2 = 120;
//kulang pa
break;
}
}

GolamMostafa:
@OP

To receive meaningful help, please post the complete program with code tags (>/>).

oh sorry, I will keep that in mind.

//Dc motor 1
int enA = 8;
..
..
 analogWrite(enA, motorSpeed1);

If you have a Uno, pin 8 isn't PWM.... only pins marked ~ are.

neiklot:

//Dc motor 1

int enA = 8;
..
..
analogWrite(enA, motorSpeed1);




If you have a Uno, pin 8 isn't PWM.... only pins marked ~ are.

Yea, That was originally at pin 9, Im just trying out anything. Im desperate for an answer :frowning:

jardy:
Yea, That was originally at pin 9, Im just trying out anything. Im desperate for an answer :frowning:

Well at least change it back to a PWM pin and re-post. While you're at it, make it easier to read by putting each { or } on a line of its own, so eg change lines like this:

if (spinType == "topspin") {

to this:

if (spinType == "topspin")
 {

(And do a ctrl-T to get the indents all evened out)

And also someone else suggested:

.... post .... with code tags (>/>).

(He or she meant the </> icon not >/>, or you can hardcode [code] put the program here [/code] )

neiklot:
Well at least change it back to a PWM pin and re-post. While you're at it, make it easier to read by putting each { or } on a line of its own, so eg change lines like this:

if (spinType == "topspin") {

to this:

if (spinType == "topspin")

{




(And do a ctrl-T to get the indents all evened out)


And also someone else suggested:

(He or she meant the </> icon not >/>, or you can hardcode `[code] put the program here [/code]` )

Thanks for the advice, appreciate it. :slight_smile:

@OP

If you are really seeking help, please answer to my questions with respect to the following codes taken from your loop() function:

if (Serial.available() >= 2)
  {
    unsigned dataIn = Serial.read();
    unsigned dataIn2 = Serial.read();
    val = (dataIn2 * 256) + dataIn;
    Serial.println(val);
  }

1. Are yo testing your codes using Serial Monitor -- Yes/No?

2. Are you testing your codes using Android Smart Phone -- Yes or No?
If yes, connect your Bluetooth Module using a Software UART Port (SUART Port) and leave the Hardware UART Port with the Serial Monitor for debugging and uploading.
(1) Make connection as per following diagram.
hc5-3x.png
Figure-1: Connection between BT and UNO using SUART Port

(2) Include the following lines in your sketch to create SUART Port.

#include<SoftwareSerial.h>
SoftwareSerial SUART(12, 13); //SRX = DPin-12, STX = DPin-13
SUART.begin(9600);

3. You are creating val (unsigned 16-bit) from dataIn and dataIn2 which are coming from your Smart Phone. Are they coming as charcaters (ASCII code) or binary value? Is it A (01000001 ASCII code for example) for dataIn and B (01000010 ASCII code for example) for datIn2? Or, is it A (00001010 binary value for example) for dataIn and B (00001011 binary value for exmple) for dataIn2? What is the BT Terminal setting of your Smart Phone -- is it in ASCII or HEX? The answers are need to adjust the codes that I have quoted above at the beginning of this post.

4. Assume that the BT Terminal of your Smart Phone has the HEX setting, then you are receiving two data items from the Smart Phone to create the 16-bit value of val. If so, the following codes are logical considering SUART Port.

byte n = SUART.available();
if(n >= 2)
{
   byte dataIn = SUART.read();   //what is unsigned dataIn?
   byte dataIn2 = SUART.read();
   val = (unsigned int)dataIn2*256 + (unsigned int)dataIn;
   Serial.print(val);     //shows as decimal value
}

5. You have declared and defined this function drills(); but, you have never used/called it in you program. What is the need of this sub-program?

6. To get a value of 1100 for your variable val, what are these two bytes data you wish send from your Smart Phone to UNO? Remember the formula for val --
val = dataIn2 *256 + dataIn

Awaiting for the answer and then I will continue with my nest queries/questions in this post?

hc5-3x.png