Need guidance merging two working projects.

My first post, so I won’t be offended if you correct how I am asking or tell me what I am leaving out in order to help me figure out my problem.

I have two separate programs running on two different Arduino Uno units.

The first has a serial 16x2 display, a load cell, HX711 and is calibrated for what I am measuring with it. It is to be a sorting machine where I can place a “standard” that is in the middle of the weight range of what I am wanting to sort on the load cell and power up/reset the Uno and tare the unit. At that point subsequent objects placed on the load cell are + or - the standard and that is displayed on the LCD. This is the code for that part.

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

LiquidCrystal_I2C lcd(0x3f,16,2); 
#include "HX711.h"

#define calibration_factor -425.0 

#define DOUT  3
#define CLK  2

int potpin = 2;  
int val;    
HX711 scale(DOUT, CLK);

void setup() {

  Serial.println("HX711 scale");

  scale.tare(); //"center of target" weight on the scale at start up, resets the scale to 0


void loop() {
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 2);  

   val = analogRead("Reading: ");        
 val = map(val, 0, 1023, 5, 175);     
// Pos = map(val, 0, 1023, 0, 180);            
 lcd.print("Weight: ");
 lcd.print(scale.get_units(), 1); 
 lcd.print("gn   ");

The other Uno I have setup with the same display, a 10k POT that is controlling a servo. It is calibrated to swing the servo throughout its range as the POT is rotated throughout its range and display the degrees of servo rotation. This is the code for that part.

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

LiquidCrystal_I2C lcd(0x3f,16,2); 
Servo myservo; 

int potpin = 0; 
int val;   

void setup()


 void loop() 
 val = analogRead(potpin);           
 val = map(val, 0, 1023, 0, 180);    
// Pos = map(val, 0, 1023, 0, 180);          
 lcd.print("   ");
 lcd.print("Degrees Arc");

Where I have seemed to “hit the wall” is joining the two together so the zeroed object on the scale sets the servo at 90 degrees. Objects over the weight on the cell when “zeroed” with the “standard” swing the servo one direction, while objects under the “standard” swing the servo in the opposite direction.

I have read a lot, watched a number of videos, read a lot more, watched some other videos and will continue but I feel I am missing some key words in my searches because I haven’t found what I am looking for.

I also understand that the two programs are not going to be directly compatible as the inputs are not the same. I just made the two examples as “baby steps” in figuring out how to calibrate the load cell to do what I wanted and get the servo to react the way I wanted. Figured I would crawl first then try and walk.

I hope it’s not too drawn out of a post and I can attach photos of the hardware/wiring but they are not a problem because that part works.

Thanks in advance for any help in pointing me in the right direction.

I didn't see it explicitly stated but, if the aim is to have both sketches run in the same Arduino, have a look at CombiningArduinoSketches.

Thank you, I did stumble across that and have it bookmarked as well.

I think my main problem is not knowing exactly what I am doing (isn’t that always the problem) and the last time I messed with programming was Fortran, now decades ago and a bit rusty on learning new things, it would seem.

The load cell is on the digital/PWM input side of the board and my POT is on the analog input side of the board.

So the solution won’t be simply merging the two, rather I have to learn how to control the servo the same way as I am in the 2nd sketch with A0 but with 2 & 3 digital inputs from the load cell in the 1st sketch.

I think, if I can get interaction in the relationship between the servo on 9 and the load cell on 2 & 3, I can figure out the calibration but I haven’t come across that solution thus far.

Look into the map() function.

In that first code you have

val = analogRead("Reading: ");


That's not really combining two sketches. It's more writing a new sketch using some of the techniques learned in the earlier sketches (which is a good way of going about things in my view).

Perhaps you could start by defining more clearly what this new sketch is going to do. I gather you're measuring some weights, something will be displayed on an LCD and a servo will move in some way. What exactly you want to display and how exactly the servo movements relate to the weight inputs isn't all that clear (at least to me).

Essentially your first sketch generates numbers which you display. The second sketch uses numbers from the analogRead() to do something with a servo and display. It seems fairly easy to plug in the numbers from the first sketch in place of the the numbers from the analogRead(), do some adjusting of them and drive the servo...once you've sorted out the details of exactly what it's supposed to do.


That's not really combining two sketches. It's more writing a new sketch using some of the techniques learned in the earlier sketches…

Perhaps you could start by defining more clearly what this new sketch is going to do.

Yes, merging the two concepts together in a single sketch that works on a single Uno is what I am after.

This is what I am after as an end result. I place an object of a known weight onto the load cell and power up the Arduino, once tare has happened the servo runs to its middle position, let’s call that 90 degrees.

If another object, that weighs the same is place on the load cell, the servo stays in that position of 90 degrees or centered.

If is is lighter the servo will move in a direction away from the center of the servos movement in proportion to how much lighter that the “standard” that the tare weight. So if it’s only very slightly lighter the servo will still be very close to the center of its throw, say 89 degrees, if the weight on the load cell is much lighter it would swing all the way to zero. If the weight is somewhere in between the servo would be somewhere between 0 and 89 deg in position. Same thing for weights over the “standard” except servo movement will be between 91 and 180 for those weights.

I understand there will be scaling to deal with once I get the servo to respond with the load cell but how about this for example, I place a 10 lb object on the load cell and power up the device. The servo moves to 90 deg as the tare in effect deducts the 10 lbs on the load cell. Let’s say we have a 21 lb range scaled into the device. So with a given weight on the cell the servo position corresponds.

0 lb the servo is at 0 deg.
2.5 lbs the servo is at 22.5 deg.
5 lbs the servo is at 45 deg.
10 lbs the servo is at 90 deg.
15 lbs the servo is at 135 deg.
20 lbs the servo is at 180 deg.

Is that a better explanation of what I am trying to accomplish?

That's good. Deriving the value to write to the servo is just the sort of thing that the map() function is for. You're already using it in your servo code so effectively you just need to change "val, 0, 1023" to "current weight, low weight in range, high weight in range" and you've cracked that part.

A couple of questions. In your current sketch the tare function is in setup() so you have to reset the Arduino to change the tare weight. Are you happy with that or are you adding a button for that function too? And is the 21 lb range fixed in the program? E.g. if I put 3 lb or 25 lb on as the tare weight is it still a 21 lb range? If not how does the range get in there? Perhaps calculate it as twice the tare weight?

And then there's the LCD display? Does that just display weights as in your original sketch? Or everything from both sketches? Or something else?


Thank you, I’ll will try that when I get a chance.

It’s not a big deal if the tare needs a “reboot”, I suppose it would be nice to have it done with a button but that is down on the list of importance. For that matter the LCD really isn’t important either except I like the visual feed back. The range would be nice to adjust as well as other “features” but I figured I would work on one thing at a time and the most important part is a functional relationship between the load cell and servo, at least until the next step :wink: