Go Down

Topic: Motor control with TA8428K (Read 1 time) previous topic - next topic

Robin2

Two or three hours spent thinking and reading documentation solves most programming problems.

jonawadl

Ok. I did some reading and work on this on my own. I have the errors down to some unqualified-id before else issues. I think it's got something to do with { missing, but I can't find it. Can somebody help me spot this?

My code looks like this now. I still don't have anything in for the servo, but I do have the servo working on a discrete sketch. I'm hoping I can put it in here later.

Code: [Select]
/* 
  L298N Motor Control Demonstration with Joystick
  L298N-Motor-Control-Demo-Joystick.ino
  Demonstrates use of Joystick control with Arduino and L298N Motor Controller
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
  // Edited and modified by Jonathan Waldner on Nov.2017 for a relay and a TA8428 Motor driver.
 
// Motor A

int in1 = 10;   //Reverse motor
int in2 = 11;   //Forward Motor
int tx=1;       //Bluetooth TX
int rx=0;       //Bluetooth RX



void setup(){




  Serial.begin(9600);   // Start Serial Monitor
 

  // Set all the motor control pins to outputs

  pinMode(in1, OUTPUT);     //Turns revere motor pin into output
  pinMode(in2, OUTPUT);     //Turns forward motor pin into output

 
  pinMode(tx, OUTPUT);     //TX for Bluetooth
  pinMode(rx, INPUT);       //RX for Bluetooth
 
  }

void loop() {
 
      char BluetoothData; // the Bluetooth data received
      char direction;   // This will tell program which direction to turn motor.
   
       // Joystick Input
   
       int joyVert = A0; // Vertical 
   
       // Motor Speed Values - Start at zero
   
       int motorSpeed1 = 0;

       // Joystick Values - Start at 512 (middle position)

       int joyposVert = 50;    //Digital joystick goes from 0-100. 50 is center

   
  if (Serial.available()) {
    while (Serial.available()) {
    joyVert = Serial.read(); // Read Joystick input
 
 
   }
if (joyVert < 49) {

   // This is Backward
   direction = 'R';
 // ........
 }
 else if (joyVert > 51)
 {
   // This is Forward
   direction = 'F';
  }
}
{
    //Determine Motor Speeds

    // As we are going backwards we need to reverse readings

    joyVert = joyVert - 50; // This produces a negative number
    joyVert = joyVert * -1;  // Make the number positive

    motorSpeed1 = map(joyVert, 0, 460, 0, 255);
 
    //Determine Motor Speed

    motorSpeed1 = map(joyposVert, 52, 100, 0, 48);

}
  else
  {
    // This is Stopped

    motorSpeed1 = 0;

  }
 
  if (direction == 'F') {
  analogWrite(motorPinA, 0);
  analogWrite(motorPinB, motorSpeed);
}}
else {
  if (direction == 'R') {
  analogWrite(motorPinB, 0);
  analogWrite(motorPinA, motorSpeed);
}


  // Adjust to prevent "buzzing" at very low speed
  {

  if (motorSpeed1 < 8)motorSpeed1 = 0;

  // Set the motor speeds



}

Robin2

If you use the AutoFormat tool it will lay your code out consistently so that you can see where there are unmatched braces.

On line 101  you have }}  and I think there should only be }. I think there is another error also.

In any case never put more than one } on any line.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

jonawadl

OK, according to Arduino IDE. I created a set of code that makes sense. In real life ... not yet. My code compiled finally without a problem.

however, nothing happens when I try it out.

I'm guessing it has to do with the bluetooth data its receiving and what it is doing with it.

The app I'm using for my android phone is from Keuwl software. I'm using a joystick pad that sends 0-100 from bottom of throw to top.

It is preceding the signal with an X or a Y depending on Horizontal throw or Vertical throw.

Probably I need to add and X somewhere in my code. For now I'm stuck will be doing further reading.

Code: [Select]
/*
  L298N Motor Control Demonstration with Joystick
  L298N-Motor-Control-Demo-Joystick.ino
  Demonstrates use of Joystick control with Arduino and L298N Motor Controller

  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
// Edited and modified by Jonathan Waldner on Nov.2017 for a relay and a TA8428 Motor driver.

// Motor A

int in1 = 10;   //Reverse motor
int in2 = 11;   //Forward Motor
int tx = 1;     //Bluetooth TX
int rx = 0;     //Bluetooth RX



void setup() {




  Serial.begin(9600);   // Start Serial Monitor


  // Set all the motor control pins to outputs

  pinMode(in1, OUTPUT);     //Turns revere motor pin into output
  pinMode(in2, OUTPUT);     //Turns forward motor pin into output


  pinMode(tx, OUTPUT);     //TX for Bluetooth
  pinMode(rx, INPUT);       //RX for Bluetooth

}

void loop() {

  char BluetoothData; // the Bluetooth data received
  char direction;   // This will tell program which direction to turn motor.

  // Joystick Input

  int joyVert = X0; // Vertical

  // Motor Speed Values - Start at zero

  int motorSpeed1 = 0;

  // Joystick Values - Start at 512 (middle position)

  int joyposVert = 50;    //Digital joystick goes from 0-100. 50 is center


  if (Serial.available()) {
    while (Serial.available()) {
      joyVert = Serial.read(); // Read Joystick input


    }
    if (joyVert < 49) {

      // This is Backward
      direction = 'R';
      // ........
    }
    else if (joyVert > 51)
   
      // This is Forward
      direction = 'F';
    }

    {
      //Determine Motor Speeds

      // As we are going backwards we need to reverse readings

      joyVert = joyVert - 50; // This produces a negative number
      joyVert = joyVert * -1;  // Make the number positive

      motorSpeed1 = map(joyVert, 0, 460, 0, 255);

      //Determine Motor Speed

      motorSpeed1 = map(joyposVert, 52, 100, 0, 48);

    }
    {
    if (joyVert = 50)
   
      // This is Stopped

      motorSpeed1 = 0;

    }

    if (direction == 'F') {
      analogWrite(in2, 0);
      analogWrite(in1, motorSpeed1);
    }
    else {
      if (direction == 'R') {
        analogWrite(in1, 0);
        analogWrite(in2, motorSpeed1);
      }


      // Adjust to prevent "buzzing" at very low speed
      {

        if (in1 < 8)motorSpeed1 = 0;

        // Set the motor speeds

      }
    }
      }

Robin2

The code in Reply #18 mentions BluetoothData on line 41 but that is all. There does not seem to be any code to receive data from a bluetooth device.

It is a good idea (i.e. essential IMHO) to include Serial.print() statements in your code so you can see that the actual values are in the variables. Maybe they are not what you think.

It is also a good idea to break your code into short single purpose functions as that allows you to test the parts separately. Have a look at Planning and Implementing a Program


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

jonawadl

I've changed both things mentioned above already before even reading this. I added a line to serial.print Serial joypos and motor output. I also added a line for serial read. I'll play with it some and get back here.

jonawadl

Ok I've exhausted all the instructions I can find. I tried including SoftwareSerial because I saw somebody had done that. I tried without and with it there. I opened the serial monitor to see what I was getting. I see  joyvert and motorspeed = but no input beside it.

Here is my current code. Somebody please point out where I went wrong.

Code: [Select]
// RC Car control using HC-06 Servo and TA8428K.


#include <SoftwareSerial.h>    //Creates a "virtual" Serial port/UART
SoftwareSerial BT(1, 2);       //Connects BT moduel TX & RX to pins 1&2 respectively

// Motor A

int in1 = 10;   //Reverse motor
int in2 = 11;   //Forward Motor
int tx = 1;     //Bluetooth TX
int rx = 0;     //Bluetooth RX



void setup() {



  Serial.begin(19200); // For Serial Monitor
  BT.begin(9600);   // Start Bluetooth Monitor
  BT.println("a* Hello from Arduino");
 

  // Set all the motor control pins to outputs

  pinMode(in1, OUTPUT);     //Turns reverse motor pin into output
  pinMode(in2, OUTPUT);     //Turns forward motor pin into output


  pinMode(tx, OUTPUT);     //TX for Bluetooth
  pinMode(rx, INPUT);       //RX for Bluetooth

}
  char BluetoothData;   // the Bluetooth data received
  char direction;       // This will tell program which direction to turn motor. 
  int motorSpeed1=0;    // Motor Speed Values - Start at zero
  int joyVert=50;    //Digital joystick goes from 0-100. 50 is center

 
void loop() {




  if (BT.available()) {       //If input arrives from BT serial...
    while (BT.available()) {  //Wait for input
      joyVert=BT.read();      // Read Joystick input

    }
    if (joyVert < 50) {

      // This is Backward
      direction = 'R';
      // ........
    }
    else if (joyVert > 50)
   
      // This is Forward
      direction = 'F';
    }

    {
      //Determine Motor Speeds

      // As we are going backwards we need to reverse readings

      joyVert = joyVert - 50; // This produces a negative number
      joyVert = joyVert * -1;  // Make the number positive

      motorSpeed1 = map(joyVert, 0, 100, 0, 255);
      Serial.println("joyVert =  "); //Send Joystick position to serial monitor
      Serial.println("motorSpeed1 =  "); // Send Motor Speed to serial monitor
     
      //Determine Motor Speed

    }
    {
    if (joyVert = 50)
   
      // This is Stopped

      motorSpeed1 = 0;

    }

    if (direction == 'F') {
      analogWrite(in2, 0);
      analogWrite(in1, motorSpeed1);
    }
    else {
      if (direction == 'R') {
        analogWrite(in1, 0);
        analogWrite(in2, motorSpeed1);
      }


      // Adjust to prevent "buzzing" at very low speed
      {

        if (in1 < 8)motorSpeed1 = 0;

        // Set the motor speeds

      }
    }
      }

Robin2

Your {} are still all over the place which makes it very hard to see what parts belong with what. Also you have huge swathes of unnecessary white-space which also makes it hard for the eye to follow the flow. Have a look at this tidied-up version. PLEASE read through it VERY carefully to make sure I have not introduced any error or have misinterpreted how things should be grouped.
Code: [Select]
// tidied up version
// RC Car control using HC-06 Servo and TA8428K.


#include <SoftwareSerial.h>    //Creates a "virtual" Serial port/UART
SoftwareSerial BT(1, 2);       //Connects BT moduel TX & RX to pins 1&2 respectively

// Motor A

int in1 = 10;   //Reverse motor
int in2 = 11;   //Forward Motor
int tx = 1;     //Bluetooth TX
int rx = 0;     //Bluetooth RX

char BluetoothData;   // the Bluetooth data received
char direction;       // This will tell program which direction to turn motor.
int motorSpeed1=0;    // Motor Speed Values - Start at zero
int joyVert=50;    //Digital joystick goes from 0-100. 50 is center

void setup() {

    Serial.begin(19200); // For Serial Monitor
    BT.begin(9600);   // Start Bluetooth Monitor
    BT.println("a* Hello from Arduino");
 
    // Set all the motor control pins to outputs

    pinMode(in1, OUTPUT);     //Turns reverse motor pin into output
    pinMode(in2, OUTPUT);     //Turns forward motor pin into output
    pinMode(tx, OUTPUT);     //TX for Bluetooth
    pinMode(rx, INPUT);       //RX for Bluetooth

}
 
void loop() {

    if (BT.available()) {       //If input arrives from BT serial...
        while (BT.available()) {  //Wait for input
            joyVert=BT.read();      // Read Joystick input
        }
       
        if (joyVert < 50) {
                // This is Backward
            direction = 'R';
        }
        else if (joyVert > 50) {
                // This is Forward
            direction = 'F';
        }

            //Determine Motor Speeds
            // As we are going backwards we need to reverse readings

        joyVert = joyVert - 50; // This produces a negative number
        joyVert = joyVert * -1;  // Make the number positive

        motorSpeed1 = map(joyVert, 0, 100, 0, 255);
        Serial.println("joyVert =  "); //Send Joystick position to serial monitor
        Serial.println("motorSpeed1 =  "); // Send Motor Speed to serial monitor
     
        //Determine Motor Speed

        if (joyVert = 50) {
            // This is Stopped
            motorSpeed1 = 0;
        }

        if (direction == 'F') {
            analogWrite(in2, 0);
            analogWrite(in1, motorSpeed1);
        }
        else {
            if (direction == 'R') {
                analogWrite(in1, 0);
                analogWrite(in2, motorSpeed1);
            }
                // Adjust to prevent "buzzing" at very low speed
            if (in1 < 8) {
                motorSpeed1 = 0;
                // Set the motor speeds
            }
        }
    }
}


What is the purpose of the code in lines 51 to 59 in the tidied up version? Can you provide a few examples to illustrate what you are trying to do. I don't understand how it relates to the use of the variable direction

Also, and separately, can you provide an example of the data you are SENDING using Bluetooth, as i suspect your code for receiving the data is not suitable. Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

TomGeorge

Hi,
I'm just trying to trace your changes as this thread continues.

Have you written code that just controls the driver and motor, with NO BLURTOOTH or any other code?
Does it work?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

jonawadl

Hi,
I'm just trying to trace your changes as this thread continues.

Have you written code that just controls the driver and motor, with NO BLURTOOTH or any other code?
Does it work?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom... :)
That turned out to be pretty good advice. I tried the circuit "ofline" by putting everything on a breadboard without an arduino manually made the forward pin hi by connecting it to 5V nothing. Same with the reverse pin. I know the power supply is good. I know the 5V is right and I know I did the wiring for the for the TA8428K right. I took it off a board from an old printer. It was nice and conveniently in a corner with obvious traces. I snipped that corner off the board leaving lots of room around the circuit. I kept the Cap in place as it shows in the datasheet. Then I carefully scraped the varnish off the traces to solder the wires in place. I even remembered to have two grounds so that I can tie everything together.

I checked and double checked my soldering for continuity to the pins that I intended it for and I checked and double checked for shorts. Two of the traces are pretty close together. I was intending to use epoxy or Hot glue to reinforce all my connections after I had everything working so that there was a bit of mechanical holding power.

Is there a reason the driver should not turn the motor with a DC motor plugged into Pins 3 and 5, Ground to 4 Battery + to 7. If I take a lead from the common ground and either of 1 or 2 to 5V I should have the motor turn at full speed right? I tried a 0-2kOhm pot first then tried connecting directly to 5V nothing. Now, unless somebody can tell me where there is an error in my thinking, I'm guessing my driver is kaput somehow.

bummer. I'll order a L298N expansion board for this project. There are tones of examples of people using that driver I can learn from.

Jonathan

TomGeorge

Hi,
Forget the L298N, pay a bit extra for boards that have MOSFET switching.

Quote
I know the 5V is right and I know I did the wiring for the for the TA8428K right. I took it off a board from an old printer. It was nice and conveniently in a corner with obvious traces.
So the TA8428K is not brand new, how did you take it off the PCB it was on?

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

jonawadl

I didn't take it off. As I said it was conveniently on a corner. So I snipped around it leaving a lot of the PCB in place including the cap. I ended up with a square about 5cmx5cm. The traces for the motor and the battery were nice and thick so I was easily able to scrape away the varnish and solder leads directly to the PCB.

jonawadl

#27
Dec 11, 2017, 01:27 am Last Edit: Dec 11, 2017, 05:37 am by jonawadl




Risky post of the day. Now many people will say I didn't do a good job? I know the connections are secure. I have done quite a bit of soldering. I also know there are no shorts on the board and I know where each pin goes.

EDIT:
Somehow those two pictures I linked to don't show up in the forum. This post would make way more sense if they were there.

jonawadl

Hi,
Forget the L298N, pay a bit extra for boards that have MOSFET switching.
So the TA8428K is not brand new, how did you take it off the PCB it was on?

Tom... :)
Can you give me an example of such a board?

Robin2

#29
Dec 11, 2017, 10:04 am Last Edit: Dec 11, 2017, 10:04 am by Robin2
I'll order a L298N expansion board for this project.
Why would you buy an L298 if you can get a new TA8428K?

And I am using Infineon TLE 5206 devices as an alternative to the Toshiba device as RS Components did not have any more of the TA8428Ks. The TLE5206 is similar but is not a drop-in replacement. It also has a pin that signals a fault which my "client" friend wanted.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up