Looking for help with modifying a sketch.

I am running a sketch by James Brunton.

This sketch is for a large diy servo motor.

I will attach the sketch below.

The issue ive run into is that James sketch is coded for an IBT-4 motor driver. this driver has 2 pwm pins.

My motor driver has 1 pwm pin and 2 directional pins labels INA & INB

Full pinout list for my motor driver is such: +5v , GND , EN , CS , INA , INB , PWM

Id like to modify the code to work with this motor driver.

The sketch is to drive a servo motor made from a reduction drive 12v dc wiper motor. I have fixed a 10k POT to the output shaft of the motor. everything is working correctly, if i open the serial monitor, its receiving input from my transceiver, its receiving servo position from the POT, everything is good, accept it needs to be coded for a motor driver that have 2 directional pins and 1 pwm.

I really dont understand code much, but im willing to send someone money over etransfer if they can modify the code and I can see it working

#include <PID_v1.h> //PID loop from http://playground.arduino.cc/Code/PIDLibrary
double Pk1 = 1; //speed it gets there
double Ik1 = 0;
double Dk1 = 0;
double Setpoint1, Input1, Output1, Output1a; // PID variables
PID PID1(&Input1, &Output1, &Setpoint1, Pk1, Ik1 , Dk1, DIRECT); // PID Setup
volatile unsigned long pwm;
volatile boolean done;
unsigned long start;
int pot;
unsigned long currentMillis;
long previousMillis = 0; // set up timers
long interval = 20; // time constant for timers
void setup() {
pinMode(2, INPUT);
pinMode(A0, INPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
attachInterrupt(0, timeit, CHANGE);

Serial.begin(115200);
PID1.SetMode(AUTOMATIC); // PID Setup - trousers SERVO
PID1.SetOutputLimits(-255, 255);
PID1.SetSampleTime(20);
}
void timeit() {
if (digitalRead(2) == HIGH) {
start = micros();
}
else {
pwm = micros() - start;
done = true;
}
}
void loop() {
currentMillis = millis();
if (currentMillis - previousMillis >= interval) { //start timed event
previousMillis = currentMillis;
pot = analogRead(A0);
Serial.print(pot);
Serial.print(" , “);
Serial.print (pwm);
Serial.print(” , ");

Setpoint1 = map(pwm,0,8000,-255,255);
Input1 = map(pot,0,1023,-255,255);
PID1.Compute();

Serial.println(Output1);

if (Output1 >= 0) {
analogWrite(5, Output1);
analogWrite(6, 0);
}
else if (Output1 < 0) {
Output1a = abs(Output1);
analogWrite(5, 0);
analogWrite(6, Output1a);
}

if (!done)
return;
done = false;

} // end of timed event
}

Please read the How to use this forum thread at the top of any of the message boards and pay attention especially to the part about how to post code the right way.

Your changes would be here

 if (Output1 >= 0) {
        analogWrite(5, Output1);
        analogWrite(6, 0);
      }
      else if (Output1 < 0) {
        Output1a = abs(Output1);
        analogWrite(5, 0);
        analogWrite(6, Output1a);
      }

Instead of writing one pin with PWM and the other with 0, you’ll only be writing the same PWM value to the same pin in either case. And the two conditions there, instead of deciding which PWM pin to send to, they’ll decide which direction pin to write high and which one to write low.

my apologies, I did read it last night, completely forgot about how to post the code this morning.

could you write in the changes, Im not sure exactly how to write that in. it makes sence what you said about high and low.. but Im still studying how to code arduinos and im not even close to knowing how id change that.

I can e transfer you 20$ for your time if youd like

thanks!

if (Output1 >= 0) {
        analogWrite(yourPinForPWM, Output1);
        digitalWrite(INA, HIGH);
        digitalWrite(INB, LOW);
      }
      else if (Output1 < 0) {
        analogWrite(yourPinForPWM, -Output1);
        digitalWrite(INA, LOW);
        digitalWrite(INB, HIGH);
      }

I also removed the superfluous call to abs there. If it runs backwards, then swap the LOWs and HIGHs.

I’ve made some assumptions about how you might name your pins. Hopefully you can handle putting your own pin names in there.

If that really is beyond your abilities, then you might want to put this aside for a minute and take some time to learn the basics of Arduino before you dive right into that PID code. Crawl before you walk if you know what I mean.

Im going to try. let me know if im on the right track here.

if (Output1 >= 0) {
        analogWrite(5, high);
        analogWrite(6, low);
      }
      else if (Output1 < 0) {
        Output1a = abs(Output1);
        analogWrite(5, low);
        analogWrite(6, high);
      }

analogWrite(5, high); What is "high"?

scobz:
Im going to try. let me know if im on the right track here.

if (Output1 >= 0) {

analogWrite(5, high);
        analogWrite(6, low);
      }
      else if (Output1 < 0) {
        Output1a = abs(Output1);
        analogWrite(5, low);
        analogWrite(6, high);
      }

Delta_G:
you might want to put this aside for a minute and take some time to learn the basics of Arduino before you dive right into that PID code. Crawl before you walk if you know what I mean.

Unless reply #3 is enough to get you done and you really don’t want to learn anymore, in which case I’ll take that $20.

so this is what ive got, and im still not getting any action.

(yes I know I need to learn more about arduino coding, through this project I have been reading alot about it but definitely not enough. Im interested in learning it all, but at the moment this project is on a time crunch. the original project never called for anything arduino but unfortunately i need a higher power servo so ive had to go and build one.)

Forget $20, Ill send more, if we can get this to work. I dont mind spending the money for someone’s talents.

im going to post everything i have set up, maybe you can see whats missing.

here is the sketch, after ive inputted what you corrected.

#include <PID_v1.h>  //PID loop from http://playground.arduino.cc/Code/PIDLibrary
double Pk1 = 1;  //speed it gets there
double Ik1 = 0;
double Dk1 = 0;
double Setpoint1, Input1, Output1, Output1a;    // PID variables
PID PID1(&Input1, &Output1, &Setpoint1, Pk1, Ik1 , Dk1, DIRECT);    // PID Setup
volatile unsigned long pwm;
volatile boolean done;
unsigned long start;
int pot;
unsigned long currentMillis;
long previousMillis = 0;    // set up timers
long interval = 20;        // time constant for timers
void setup() {
  pinMode(2, INPUT);
  pinMode(A0, INPUT);
  pinMode(7,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  attachInterrupt(0, timeit, CHANGE);
  
  Serial.begin(115200);
  PID1.SetMode(AUTOMATIC);              // PID Setup - trousers SERVO
  PID1.SetOutputLimits(-255, 255);
  PID1.SetSampleTime(20);
}
void timeit() {
    if (digitalRead(2) == HIGH) {
      start = micros();
    }
    else {
      pwm = micros() - start;
      done = true;
    }
  }
void loop() {
  currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {  //start timed event
      previousMillis = currentMillis;
      pot = analogRead(A0);
      Serial.print(pot);
      Serial.print(" , ");
      Serial.print (pwm);
      Serial.print(" , ");

  Setpoint1 = map(pwm,0,8000,-255,255);
      Input1 = map(pot,0,1023,-255,255);
      PID1.Compute();

      Serial.println(Output1);    
      
      if (Output1 >= 0) {
        analogWrite(7, Output1);
        digitalWrite(5, HIGH);
        digitalWrite(6, LOW);
      }
      else if (Output1 < 0) {
        analogWrite(7, -Output1);
        digitalWrite(5, LOW);
        digitalWrite(6, HIGH);
      }
      
      
      if (!done)
            return;               
            done = false;   
     
  } // end of timed event
}

here are some photos.


Motor Driver Board

note: the power supply i have disconnected from my motor driver board to take photos easier.
on the motor driver i have (by wire colours)
Black - PWM
Green - INB
Purple - INA
Gray - GND
(do I need to attach a wire from arduino to +5V pin on motor driver aswell?)

These wires from motor driver connect to arduino as such
Black - PWM 7
Green - PWM 6
Purple - PWM 5
Gray - GND


Receiver

Purple - 5V
Gray - GND
Blue - Signal PIN Channel 1

These wires from receiver connect to arduino as such
Purple - VIN
Gray - GND
Blue- PWM 2


DIY “Servo Motor” (12v worm gear reduction DC motor + 10K POT)

Motor Positive Wire - OUT A (of motor driver)
Motor Negative Wire - OUT B (of motor driver)

10K POT
Orange - Positive pin on pot
Yellow - Signal pin on pot
Brown - Negative on pot

these wires connect to arduino as such

Orange - 5V
Yellow - Analog In (A0)
Brown - GND

No idea if this will work (motor direction may need tweaking…) but give it a try:

I tried to clarify a few things, added some more debug msgs (you may want to remove some of those as they chew time in the timed-event).

I tried a few canned values for the setpoint and feedback and got non-insane values back for output1.

#include <PID_v1.h>  //PID loop from http://playground.arduino.cc/Code/PIDLibrary

double 
    Pk1 = 1,    //speed it gets there
    Ik1 = 0,
    Dk1 = 0;
double
    Setpoint1, 
    Input1, 
    Output1, 
    Output1a;    // PID variables
    
PID PID1(&Input1, &Output1, &Setpoint1, Pk1, Ik1 , Dk1, DIRECT);    // PID Setup

volatile unsigned long pulseWidth;
unsigned long pulseStart;

volatile boolean done;

int feedbackPOT;
unsigned long currentMillis;
unsigned long previousMillis = 0;           // set up timers
volatile unsigned long motorstopTimer;
//
const unsigned long timeoutPWM = 1000ul;    //if we haven't received a pulse in this time, stop the motor
const unsigned long interval = 20ul;        // time constant for timers
const int pinPWM = 7;
const int pinINB = 6;
const int pinINA = 5;

const int pinPotentiometer = A0;

const int pinRCInput = 2;

void setup() 
{
    pinMode( pinRCInput, INPUT );
    pinMode( pinPotentiometer, INPUT );
    pinMode( pinPWM,OUTPUT );
    pinMode( pinINA,OUTPUT );
    pinMode( pinINB,OUTPUT );

    //initialize pins to driver
    digitalWrite( pinINA, LOW );
    digitalWrite( pinINB, LOW );
    analogWrite( pinPWM, 0 );
    
    attachInterrupt( digitalPinToInterrupt(pinRCInput), ISR_TimeRCPulse, CHANGE );
  
    Serial.begin(115200);
    
    PID1.SetMode(AUTOMATIC);              // PID Setup - trousers SERVO
    PID1.SetOutputLimits(-255.0, 255.0);
    PID1.SetSampleTime(20);
    
    motorstopTimer = millis();
}//setup


void ISR_TimeRCPulse() 
{
    if( digitalRead( pinRCInput ) == HIGH ) 
    {
        pulseStart = micros();
        
    }//if
    else 
    {
        pulseWidth = micros() - pulseStart;
        done = true;
        motorstopTimer = millis();
        
    }//else
    
}//ISR_TimeRCPulse

void MotorStop()
{
    analogWrite( pinPWM, 0 );       //zero speed
    digitalWrite( pinINA, HIGH );   //braking condition
    digitalWrite( pinINB, HIGH );
    
}//MotorStop

void loop() 
{
    currentMillis = millis();
    if( (currentMillis - motorstopTimer) >= timeoutPWM )
        MotorStop();
    
    if( (currentMillis - previousMillis) >= interval) 
    {  
        //start timed event
        previousMillis = currentMillis;
            
        if( done )
        {
            feedbackPOT = analogRead(A0);
            Serial.print( "Feedback   : " ); Serial.println(feedbackPOT);
            Serial.print( "RC PW      : " ); Serial.println(pulseWidth);
    
            Setpoint1 = (float)(map( pulseWidth, 0, 8000, 0, 255 ));
            Input1 = (float)(map(feedbackPOT, 0, 1023, 0, 255));
            PID1.Compute();
            Serial.print( "Setpoint1  : " ); Serial.println(Setpoint1,3);
            Serial.print( "Output1    : " ); Serial.println(Output1,3);
            Serial.print( "Out1 (byte): " );
            if( Output1 >= 0)
                Serial.println((int)Output1);
            else
                Serial.println((int)(-Output1));
            
            if( Output1 >= 0) 
            {
                analogWrite( pinPWM, (int)(Output1) );
                digitalWrite( pinINA, HIGH );
                digitalWrite( pinINB, LOW );
                
            }//if
            else 
            {
                analogWrite( pinPWM, (int)(-Output1) );
                digitalWrite( pinINA, LOW );
                digitalWrite( pinINB, HIGH );
                
            }//else
                                         
            done = false;
               
            Serial.println("////////");
         
        }//if done
        
    }// end of timed event    

}//loop

still no luck. that sketch didnt work

scobz:
still no luck. that sketch didnt work

In what way?

Im thinking theres something wrong with this motor driver. I uploaded a basic sketch from a site that sells these drivers... the sketch was super simple. send "1" in serial monitor and the motor should go reverse, send "2" forward send "3" stop send "+" increase speed send "-" decrease speed. nothing happened. Im about to give up on this. hours on forums, weeks trying to get this to atleast move the motor and nothing yet.

scobz:
Im thinking theres something wrong with this motor driver. I uploaded a basic sketch from a site that sells these drivers... the sketch was super simple. send "1" in serial monitor and the motor should go reverse, send "2" forward send "3" stop send "+" increase speed send "-" decrease speed. nothing happened. Im about to give up on this. hours on forums, weeks trying to get this to atleast move the motor and nothing yet.

scobz:
Im thinking theres something wrong with this motor driver. I uploaded a basic sketch from a site that sells these drivers... the sketch was super simple. send "1" in serial monitor and the motor should go reverse, send "2" forward send "3" stop send "+" increase speed send "-" decrease speed. nothing happened. Im about to give up on this. hours on forums, weeks trying to get this to atleast move the motor and nothing yet.

Usually, it's not a good idea to go into such detail as to why you're giving up - it discourages people from offering their help in the future.

scobz:
Im thinking theres something wrong with this motor driver. I uploaded a basic sketch from a site that sells these drivers… the sketch was super simple. send “1” in serial monitor and the motor should go reverse, send “2” forward send “3” stop send “+” increase speed send “-” decrease speed. nothing happened. Im about to give up on this. hours on forums, weeks trying to get this to atleast move the motor and nothing yet.

Well, this sounds like you didn’t connect the motor or driver the right way…