Problem about Arduino serial pot commuication (Loadcell and Servo)

Hello, I connected Arduino to Matlab. I want to control a motor (Servo or DC) by measured value from a load cell. The value of the load cell was measured on Matlab. However, I was wondering why the servo doesn't work when the measured values are over 50 or less -50.

#include <Servo.h>
#include "HX711.h"

#define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch

#define DOUT  3
#define CLK  2


HX711 scale;
Servo myservo;
int pos = 0;

void setup() {
  Serial.begin(9600);
  scale.begin(DOUT, CLK);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
 
  myservo.attach(6); 
  delay(500);
}

void loop() {
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.println();
  
  if(scale.get_units()>50){
    for(pos = 0; pos <=100; pos += 1){                                   
      myservo.write(pos);

      
      delay(100);
    }
  
  if(scale.get_units() < -50)                            
    for(pos = 100; pos >=1; pos -= 1){
      myservo.write(pos);


      delay(100);
    }
  }

}

the servo doesn't work

What does this mean? What does the servo do? What do you expect the servo to do ?

Originally, I want to control a DC motor by measured value from the load cell. So, if the higher values (compression or tension) which is more than what I want measured, the DC motor adjust a cable that attached the load cell.

I thought the "if" command was right. For example,

"scale.get_units()>50)" & "for(pos = 0; pos <=100; pos += 1)<- if the measured value is over 50, the servo goes 0 to 100.

I don't know what I missed for this step. Right now, I am trying to use this code below. If you have better ways to control a DC motor by measured values, please let me know. Thank you a lot:)

// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;

void setup() {
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  
  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}

void loop() {
  directionControl();
  delay(1000);
  speedControl();
  delay(1000);
}

// This function lets you control spinning direction of motors
void directionControl() {
  // Set motors to maximum speed
  // For PWM maximum possible values are 0 to 255
  analogWrite(enA, 255);

  // Turn on motor A & B
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  delay(2000);
  
  // Now change motor directions
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  delay(2000);
  
  // Turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}

// This function lets you control speed of the motors
void speedControl() {
  // Turn on motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);

    // Accelerate from zero to maximum speed
  for (int i = 0; i < 256; i++) {
    analogWrite(enA, i);
    delay(20);
  }
  
  // Decelerate from maximum speed to zero
  for (int i = 255; i >= 0; --i) {
    analogWrite(enA, i);
    delay(20);
  }
  
  // Now turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);

}

gsung2:
Right now, I am trying to use this code below.

You need to tell us in detail what happens when you run that program and what you want it to do that is different.

It would also be very helpful if you can provide an example of the message that Matlab sends to the Arduino.

What Arduino are you using?

...R
Serial Input Basics - simple reliable non-blocking ways to receive data.

Hi,
In your first code can I suggest the first thing you do in the loop is read ALL your inputs and store them in variables.
Then for the rest of your code you use those variables.

This way you use a snapshot of your project which will contain ALL input data at that time.

This will help the stability of your code performance.

Also your code was missing some { and }.

#include <Servo.h>
#include "HX711.h"

#define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define DOUT  3
#define CLK  2

HX711 scale;
Servo myservo;
int pos = 0;
float scaleValue = 0;

void setup()
{
  Serial.begin(9600);
  scale.begin(DOUT, CLK);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  myservo.attach(6);
  delay(500);
}

void loop()
{
  scaleValue = scale.get_units(); // first read the scale and store it in a variable
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.println();

  if (scaleValue > 50.0)
  {
    for (pos = 0; pos <= 100; pos += 1)
    {
      myservo.write(pos);
      delay(100);
    }
  }
 if (scaleValue < -50.0)
  {
    for (pos = 100; pos >= 1; pos -= 1)
    {
      myservo.write(pos);
      delay(100);
    }
  }
}

Tom... :slight_smile:

Threads merged, images fixed, thread moved.

To Mr. Coding Badly,

I am sorry to be maladroit. I edited everything correct. Thank you and have a nice day :slight_smile:

To Mr. Tom,

Thank you so much for your correction! It worked. I could learn how to use 'float' function. The interesting result is while the servo is working, Matlab stops the data collection. So, I changed the delay and the pos angle that how much it moves at one time. It seems that there is no big change.

I was wondering that the reason why the result was not really changed was that I used a servo, which is 25 cents?

I attached the code that I changed some values and have a nice day :slight_smile:

#include <Servo.h>
#include "HX711.h"

#define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define DOUT  3
#define CLK  2

HX711 scale;
Servo myservo;
int pos = 0;
float scaleValue = 0;

void setup()
{
  Serial.begin(9600);
  scale.begin(DOUT, CLK);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  myservo.attach(6);
  delay(100);
}

void loop()
{
  scaleValue = scale.get_units(); // first read the scale and store it in a variable
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.println();

  if (scaleValue > 50.0)
  {
    for (pos = 0; pos <= 100; pos += 100)
    {
      myservo.write(pos);
      delay(100);
    }
  }
 if (scaleValue < -50.0)
  {
    for (pos = 100; pos >= 1; pos -= 100)
    {
      myservo.write(pos);
      delay(100);
    }
  }
}

You are missing a connection from the battery ground to the rest of the circuit. You need 2 wires coming from your battery. Do you have both?

Have you tried to get the motor to work with a simple demo program?

@gsung2, please stop cross-posting. Threads merged.

Why is that continually merged even after I corrected? :o :o I am so sorry about that.