Help writing code for actuator control

Hi, @kenpjr
To add code please click this link;

Also if you press [ CTRL ] [ T ] in the IDE you will find your code auto formatted with indents to help read you code.

const int A_PWM = 11;
const int B_PWM = 3;
const int Enable = 8;
void setup()
{
  pinMode(A_PWM, OUTPUT);
  pinMode(B_PWM, OUTPUT);
  pinMode(Enable, OUTPUT);
  digitalWrite (A_PWM, LOW);
  digitalWrite (B_PWM, LOW);
  digitalWrite (Enable, LOW);
}
void TurnRight(uint8_t pwm)
{
  analogWrite(A_PWM, 0);
  delayMicroseconds(200);
  analogWrite(B_PWM, pwm);
}
void TurnLeft(uint8_t pwm)
{
  analogWrite(B_PWM, 0);
  delayMicroseconds(200);
  analogWrite(A_PWM, pwm);
}
void Stop()
{
  analogWrite(B_PWM, LOW);
  analogWrite(A_PWM, LOW);
  analogRead(A0);
}
void loop()
{
  digitalWrite (Enable, HIGH);
  TurnRight(200,);//out
  delay (5000);
  Stop();
  TurnLeft(200);//in
  delay(5000);
}

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

look this over
seems that you only need to cycle the motor once, so no need to do it repeatedly in loop().
this code reads the pot and uses the pwm value from the pot.

const int A_PWM = 11;
const int B_PWM = 3;
const int Enable = 8;

void setup ()
{
    Serial.begin (9600);

    pinMode (A_PWM, OUTPUT);
    pinMode (B_PWM, OUTPUT);
    pinMode (Enable, OUTPUT);

    digitalWrite (A_PWM,LOW);
    digitalWrite (B_PWM,LOW);
    digitalWrite (Enable,HIGH);

    // just do once in setup
    int pot = analogRead (A0);
    int pwm = map (pot, 0, 1023, 0, 255);
    Serial.println (pwm);

    TurnRight (pwm); //out
    delay (5000);

    Stop ();

    TurnLeft (pwm);//in
    delay (5000);

    Serial.println ("Done");
}


void TurnRight (uint8_t pwm){
    Serial.println ("TurnRight");
    analogWrite (A_PWM, 0);
    analogWrite (B_PWM, pwm);
}

void TurnLeft (uint8_t pwm){
    Serial.println ("TurnLeft");
    analogWrite (B_PWM, 0);
    analogWrite (A_PWM, pwm);
}

void Stop (){
    Serial.println ("Stop");
    analogWrite (B_PWM, LOW);
    analogWrite (A_PWM, LOW);
}

void loop ()
{
}

This makes the motor run one speed and even if the speed pot is unplug from A0

what value do you see on the serial monitor?

bear in mind, the pot is only read once.

do you need a calibration mode that allows you to determine the correct pwm value using the pot? would you then hardcode that value or still need the pot?

I also only need it to run as long as a figital input is true

I don't have a monitor on it, i need it to read the pot continuously so the motor speed will change as motor is running in relation to the pot, then change directions when 2nd digital input is true and the 1st digital input is false. And no movement if both inputs are false

so ...
should the motor start when one digital input becomes active and then run for a fixed amount of time and stop and then reverse when a 2nd digital input becomes active and run for a fixed amount of time? if so, which pins and what state is active?

if the pot needs to be continually read, then delay() cannot be used. the code will need to use milliis() to determined when the time is expired to stop running or reverse.

(by any chance, is there an input that indicates when it should stop)?

why does the pot need to be continuously read? is someone going to be adjusting it each time the system runs? then why run for a fixed amount o time?

the arduino IDE has a serial monitor where prints are displayed.

which inputs are you using for fwd and rev? A1 and A2 or D1 and D2 ?
I need this to drive the mega shield, i sent you a copy of a program that runs the motor with feedback (which i don't want feedback)
but I do need to send output with push buttons and check the speed pot all the time.

const int enable = 8;
const int PWMA = 11;
const int PWMB = 3;

Have you tried my suggestion in post #18
Maybe the speed cannot be controlled by PWM

look this over

const byte PinPwmA = 11;
const byte PinPwmB = 3;
const byte PinEn   = 8;
const byte PinFor = A1;
const byte PinRev = A2;

enum { Stop, Forward, Reverse };
int pwmLast;
int dirLast;

unsigned long msecPeriod;
unsigned long msecLast;

// -----------------------------------------------------------------------------
void
setDir (
    int  dir )
{
    if (dirLast == dir)
        return;
    dirLast = dir;

    switch (dir) {
    case Stop:
        Serial.println ("Stop");
        digitalWrite (PinPwmA, LOW);
        digitalWrite (PinPwmB, LOW);
        break;

    case Forward:
        Serial.println ("Forward");
        digitalWrite (PinPwmA, LOW);
        digitalWrite (PinPwmB, HIGH);
        break;

    case Reverse:
        Serial.println ("Reverse");
        digitalWrite (PinPwmA, HIGH);
        digitalWrite (PinPwmB, LOW);
        break;
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    if (msecPeriod && msec - msecLast >= msecPeriod)  {
        msecPeriod = 0;
        setDir (Stop);
    }

    int pot = analogRead (A0);
    int pwm = map (pot, 0, 1023, 0, 255);
    analogWrite (PinEn, pwm);
    if (pwmLast != pwm)
        Serial.println (pwm);
    pwmLast = pwm;

    if (LOW == digitalRead (PinFor)) {
        setDir (Forward);
        msecLast   = msec;
        msecPeriod = 5000;
    }

    else if (LOW == digitalRead (PinRev)) {
        setDir (Reverse);
        msecLast   = msec;
        msecPeriod = 5000;
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    digitalWrite (PinPwmA, LOW);
    digitalWrite (PinPwmB, LOW);
    digitalWrite (PinEn,   LOW);

    pinMode (PinPwmA, OUTPUT);
    pinMode (PinPwmB, OUTPUT);
    pinMode (PinEn,   OUTPUT);

    pinMode (PinFor, INPUT_PULLUP);
    pinMode (PinRev, INPUT_PULLUP);

    Serial.println ("ready");
}

yes an operator will push a start cycle push button on a control panel that will cause an output from my plc to turn a start up input on the arduino unit true
this should cause the motor to run at the input value at A0 on the arduino until my plc turns the up input back to false and so on and so on until the plc teset cycle is done.
At that point the operator will come over check the readings then press plc cycle reset button which will cause an output from the plc to another input start down at the arduino unit
to run the motor back until the operator releases the plc button.
Hope I've explained it clearly enough.
in my plc language I would express it as follows
x1 input up
x2 input down
y0 drive out
Y1 drive in
PwmA drive up
PmwB drive down

x1 = true but not x2 = y0 true

on all the time (read pot input value)

y0 true----write pot value to analog PmwA

x2 = true = y1 but not x1 true = y1 true

y1 true----write pot value to analog PmwB

it controls it with the example with feedback control but I need speed not position, and on with pushbutton not timers

Have you tried my suggestion in post #18
Maybe the speed of your motor cannot be controlled by PWM

Yes I thought i sent you a progane that does that, but it uses a feedback pot to control it direction and speed, i need only need input to control dir and a pot to control speed
Just "if input 5 is on read pot and write it to pmwA"
Or " if input 6 is on read pot and write topmwB"
Thats all I need it to do.

But it needs to continuously read the pot

you never commented on the code in post #30 that reads the pot continuously

You can read the pot all your want but if your motor speed is not controllable via PWM it won't do you any good.
Have you tried my suggestion in post #18 ?
Did the speed change?

Im not sure which one that was, but yes one of them would change motor speed when i changed the write value

Connect one end of your pot to 5V and the other end to GND. Connect the wiper (center pin) to Arduino A0 and run the following program.
Does the speed change when you slowly turn the pot?


const int A_PWM = 11;
const int B_PWM = 3;
const int Enable = 2;

int speed_pot = 0;
int PWM_val;

void setup()
{
  pinMode(A_PWM, OUTPUT);
  pinMode(B_PWM, OUTPUT);
  pinMode(Enable, OUTPUT);
  pinMode(A0, INPUT);
  
  digitalWrite (A_PWM,LOW);
  digitalWrite (B_PWM,LOW);
  digitalWrite (Enable,HIGH);
}

void TurnRight(uint8_t pwm){
     analogWrite(A_PWM, 0);
     delayMicroseconds(200);
     analogWrite(B_PWM, pwm);
}

void TurnLeft(uint8_t pwm){
     analogWrite(B_PWM, 0);
     delayMicroseconds(200);
     analogWrite(A_PWM, pwm);
}

void Stop(){
  analogWrite(B_PWM, LOW);
  analogWrite(A_PWM, LOW);
}


void loop()
{
  speed_pot = analogRead(A0);
  PWM_val = map (speed_pot, 0,1023,0,255);
  
  TurnRight(PWM_val);
  delay (500);
  
}

yes this works but only in one dir, i had to modify it to run the megamoto shield