IMS-1 Motor Control for Torque Arduino UNO NEED HELP!!!!!

Good Afternoon from South Carolina!
I’m new with the arduino and have only programmed in Basic and Fortran (yes, I’m that old).
I have a project that is basically a torque wrench to tighten a bolt to a known torque and release it by reversing direction. Ihave the motor and planetary gear setup but I’m having difficulty with the code which seems simple but I just can’t get it to work. I’ve bench tested the system, monitored voltages but system will not shut down when it reaches it’s pre-programmed value.
Here is the simple code.
Will someone please identify the erroneous code and/or is there an easier way of doing this. It’s driving me nuts.
I’ll try to list the code and the pdf for the ims-1 motor drive module.
Thanks in advance.
Bill

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Automatic Torque Control usint IMS-1
// Written for Torque Project
// by Bill S.

int enPin = 5; //enable pin for IMS-1 50A motor Drive Module
const int currentPin = A0; // Currint Monitor Pin CT from IMS-1
const int lkPin = 8; //Motor Tighten Input Pin
const int unlkPin = 7; //Motor Release Input Pin
float analogValue = 0; //Variable for Torque Monitor Pin
float voltage = 0; //Varialbe used in Torque Calculation
boolean val=LOW; //Pushbutton Input for Tighten
boolean val1=LOW; //Pushbutton Input for Release
const int mrtPin = 10; // Run motor right
const int mltPin = 11; //Run motor left

//Initialize the LCD

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {

pinMode(enPin, OUTPUT); //initialize en (pin 5) as output
pinMode(currentPin, INPUT); //Set analog pin for current input
pinMode(unlkPin, INPUT); //Unlock Signal from Button
pinMode(lkPin, INPUT); //Lock Signal from Button
pinMode(mrtPin, OUTPUT); //Run Motor Left (pwm)
pinMode(mltPin, OUTPUT); //Run Motor Right (pwm)

/Set initial state for motor control values - Note Motor PWM signal reversed (255 off) (1 Full On)/

digitalWrite (enPin, LOW); //enable Motor Board
analogWrite (mrtPin, 255);
analogWrite (mltPin, 255);

lcd.begin (16, 2); /this section sets up the LCD display/
lcd.backlight();
lcd.setCursor (2,0);
lcd.print(“Initializing”);
delay(1000);
lcd.clear();
}

// the loop routine runs over and over again forever:

void loop()
{
int val = digitalRead(unlkPin);
int val1 = digitalRead(lkPin);

lcd.setCursor (2,0);
lcd.print(“SYSTEM READY”);

if (val != LOW)
{
unlock();
}
else if (val1 != LOW)
{
lock();
}

}

void unlock()
{
/* This section enables the motor drive module, sends message to LCD, reads the current pin and translates the 1024 analog signal to voltage.
to the right until the voltage is less than or equal to .5 volts. After which it sends the voltage to the LCD for torubleshooting
purposes. After .5 volts is reached or exceeded, the loop ends and everything is disarmed. A message is sent to the LCD and the loop ends.
The .5 volts is based on an actual analog measurement*/

{
digitalWrite(enPin, HIGH);
lcd.clear();
lcd.setCursor (0, 0);
lcd.print(“UnLocking”);
delay(1000);
}

{
float analogValue = analogRead(currentPin);
float voltage = analogValue * 5/1024;
{
while(voltage<=.5)
{
analogWrite(mltPin, 50);
float analogValue = analogRead(currentPin);
float voltage = analogValue * 5/1024;
lcd.clear();
lcd.setCursor (3,0);
lcd.print("Voltage = ");
lcd.print(voltage);
lcd.setCursor (3, 1);
lcd.print(“UNLOCKING”);
}
}
{
analogWrite(mltPin, 255);
delay(5000);
digitalWrite(enPin, LOW);
lcd.clear();
lcd.setCursor (3, 0);
lcd.print(“Bolt UnLocked”);
float voltage = 0;
delay(3000);
lcd.clear();
}
}
}
void lock()
{
/* This section enables the motor drive module, sends message to LCD, reads the current pin and translates the 1024 analog signal to voltage.
to the right until the voltage is less than or equal to .5 volts. After which it sends the voltage to the LCD for torubleshooting
purposes. After .5 volts is reached or exceeded, the loop ends and everything is disarmed. A message is sent to the LCD and the loop ends.
The .5 volts is based on an actual analog measurement*/

{
digitalWrite(enPin, HIGH);
lcd.clear();
lcd.setCursor (0, 0);
lcd.print(“LOCKING”);
delay(1000);
}

{
float analogValue = analogRead(currentPin);
float voltage = analogValue * 5/1024;
{
while(voltage<=.5)
{

analogWrite(mrtPin, 50);
analogValue = analogRead(currentPin);
float voltage = analogValue *5/1024;
lcd.clear();
lcd.setCursor (3,0);
lcd.print("Voltage = ");
lcd.print(voltage);
delay(1000);
lcd.setCursor (3, 1);
lcd.print(“LOCKING”);
}
}
{
analogWrite(mrtPin, 255);
delay(5000);
digitalWrite(enPin, LOW);
lcd.clear();
lcd.setCursor (3, 0);
lcd.print(“Bolt Locked”);
float voltage = 0;
delay(3000);
lcd.clear();
}
}
}

IMS-1_EN.pdf (975 KB)

First, you need to post within code tags. Use the # icon in the middle row of icons.

Now, about your program… while it passes a syntax check, it will never work. You seem to think that curly braces do something when they are only used to create a block of code that is executed when a conditional is true. For example:

if (x == 5)
{  // begin a block that executes when x = 5
do operation 1;
do operation 2;
}  // end of block

You can perform a single operation and there you do not need the braces (although it is perfectly fine to use them)

if (x == 5)
do operation 1;

which can be written this way too:

if (x == 5) do operation 1;

since white space means nothing from a syntax point of view.

I took the liberty of reformatting your program, putting the curly braces where the would normally occur, given the syntax you used. I have no idea if it will work as intended, it just follows the normal coding flow that would used in this environment. Also, do not overlook the reformat command built into the IDE. Tools->Auto Format (ctrll+T)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Automatic Torque Control usint IMS-1
// Written for Torque Project
// by  Bill S.

int enPin = 5;                //enable pin for IMS-1 50A motor Drive Module
const int currentPin = A0;    // Currint Monitor Pin CT from IMS-1
const int lkPin = 8;          //Motor Tighten Input Pin
const int unlkPin = 7;        //Motor Release Input Pin
float analogValue = 0;        //Variable for Torque Monitor Pin
float voltage = 0;             //Varialbe used in Torque Calculation
boolean val = LOW;             //Pushbutton Input for Tighten
boolean val1 = LOW;            //Pushbutton Input for Release
const int mrtPin = 10;         // Run motor right
const int mltPin = 11;         //Run motor left

//Initialize the LCD

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {

  pinMode(enPin, OUTPUT);             //initialize en (pin 5) as output
  pinMode(currentPin, INPUT);        //Set analog pin for current input
  pinMode(unlkPin, INPUT);           //Unlock Signal from Button
  pinMode(lkPin, INPUT);              //Lock Signal from Button
  pinMode(mrtPin, OUTPUT);            //Run Motor Left (pwm)
  pinMode(mltPin, OUTPUT);            //Run Motor Right (pwm)

  /*Set initial state for motor control values - Note Motor PWM signal reversed (255 off) (1 Full On)*/

  digitalWrite (enPin, LOW);      //enable Motor Board
  analogWrite (mrtPin, 255);
  analogWrite (mltPin, 255);

  lcd.begin (16, 2);            /*this section sets up the LCD display*/
  lcd.backlight();
  lcd.setCursor (2, 0);
  lcd.print("Initializing");
  delay(1000);
  lcd.clear();
}

// the loop routine runs over and over again forever:

void loop()
{
  int val = digitalRead(unlkPin);
  int val1 = digitalRead(lkPin);

  lcd.setCursor (2, 0);
  lcd.print("SYSTEM READY");

  if (val != LOW)
  {
    unlock();
  }
  else if (val1 != LOW)
  {
    lock();
  }

}

void unlock()
{
  float analogValue;
  float voltage;

  /*  This section enables the motor drive module, sends message to LCD, reads the current pin and translates the 1024 analog signal to voltage.
  to the right until the voltage is less than or equal to .5 volts.  After which it sends the voltage to the LCD for torubleshooting
  purposes.  After .5 volts is reached or exceeded, the loop ends and everything is disarmed.  A message is sent to the LCD and the loop ends.
  The .5 volts is based on an actual analog measurement*/

  digitalWrite(enPin, HIGH);
  lcd.clear();
  lcd.setCursor (0, 0);
  lcd.print("UnLocking");
  delay(1000);
  while (voltage <= .5)
  {
    analogWrite(mltPin, 50);
    analogValue = analogRead(currentPin);
    voltage = analogValue * 5 / 1024;
    lcd.clear();
    lcd.setCursor (3, 0);
    lcd.print("Voltage = ");
    lcd.print(voltage);
    lcd.setCursor (3, 1);
    lcd.print("UNLOCKING");
  }
  analogWrite(mltPin, 255);
  delay(5000);
  digitalWrite(enPin, LOW);
  lcd.clear();
  lcd.setCursor (3, 0);
  lcd.print("Bolt UnLocked");
  float voltage = 0;
  delay(3000);
  lcd.clear();
}
void lock()
{
  float analogValue;
  float voltage;

  /*  This section enables the motor drive module, sends message to LCD, reads the current pin and translates the 1024 analog signal to voltage.
  to the right until the voltage is less than or equal to .5 volts.  After which it sends the voltage to the LCD for torubleshooting
  purposes.  After .5 volts is reached or exceeded, the loop ends and everything is disarmed.  A message is sent to the LCD and the loop ends.
  The .5 volts is based on an actual analog measurement*/

  digitalWrite(enPin, HIGH);
  lcd.clear();
  lcd.setCursor (0, 0);
  lcd.print("LOCKING");
  delay(1000);
  while (voltage <= .5)
  {
    analogWrite(mrtPin, 50);
    analogValue = analogRead(currentPin);
    float voltage = analogValue * 5 / 1024;
    lcd.clear();
    lcd.setCursor (3, 0);
    lcd.print("Voltage = ");
    lcd.print(voltage);
    delay(1000);
    lcd.setCursor (3, 1);
    lcd.print("LOCKING");
  }
  analogWrite(mrtPin, 255);
  delay(5000);
  digitalWrite(enPin, LOW);
  lcd.clear();
  lcd.setCursor (3, 0);
  lcd.print("Bolt Locked");
  float voltage = 0;
  delay(3000);
  lcd.clear();
}

avr_fred: it will never work. You seem to think that curly braces do something

I don't know whether the code could work but I don't think the unnecessary extra {} are the problem.

@bsouthnow, can you explain what information is available to tell you when the correct torque is reached?

...R

Many Thanks avr_fred and Robin!
Fred, I missed the “using code tags” when I read the “read this first” post. I"ll have to figure out what they do and use them when required. Thanks for pointing that out!
I’ll try the reformatted code tonight and see if it makes any difference. The use of curley braces just seemed to make sense to me for structure. Again, remember I’m a throw back from Basic where we created our loops and used subroutines with line numbers. Antiquated but it’s all I have to work with and I"m trying to learn this without any guidance other than you generous folks in this forum! If I stumble, help me up and I"ll be grateful!
Robin, I selected this motor drive module since it is designed to work with the arduino an it has a current output pin and a voltage output pin. I have decided to use current for torque since the voltage is more related to speed. The documentation is in chenglish and is kind of difficult to interpret. The current output signal is in the form of voltage and I’m using a voltage divider to keep the voltage below 5V for the arduino. I’m setting my voltage target for a low voltage until I get the code to sense when the voltage target is reached proceed to the next phase of the code and shut the motor down. My attempts at code are noted in the lock and unlock loops. I have actually monitored this pin in the shop and the voltage does increase when putting a load on the motor. So, the signal is there but my code is just not seeing it.
Sorry to be so long in the tooth but I’d rather post to much information rather than not enough!
I’ll attache a pix of the current related chart from the documentation. Any help will be appreciated.

One other question if I may. Is there a place on the forum one can solicit help. I’m getting to the point that I will gladly pay for assistance. This phase of my little project is bogging me down.

Many thanks again and have a great day.
Bill

Thanks.

This is just a quick follow-up.

    float analogValue = analogRead(currentPin);
    float voltage = analogValue * 5/1024;
    {
      while(voltage<=.5)
    {       
      analogWrite(mltPin, 50);
      float analogValue = analogRead(currentPin);
      float voltage = analogValue * 5/1024;

There are a couple of things here that may be causing a problem.

The function analogRead() returns an integer in the range 0 - 1023. I don’t know if you need floating point values at all.

The syntax float analogValue = analogRead(currentPin); tells the compiler to create a new local variable with the name analogValue. You created a variable with that name at the top of your code so you don’t need the word float anymore. Just use
analogValue = analogRead(currentPin); everywhere except the very first time. Note also that the second time (in the above snippet) that float analogValue = analogRead(currentPin); is used it creates a 3rd varible with the same name and the 3rd one will only be visible within the WHILE loop where it was created.

If you want someone to write code for you post a request in the Gigs and Collaborations section of the forum.

…R

Thank you Robin so very much. Again, I'm just learning this and all help is appreciated. What you said about creating new variables make sense after giving it some thought. I'm going to change my code (correct) and give it a try tonight. Have a great day. Bill :)