Problem with combining 2 strings of code

Hello. I am having problems combining 2 codes. The first code has to recieve data from 4x 50kg loadcells with the HX711 amplifier module and then activate a waterpump based on the weight value from the loadcells. The second code has to control a linear actuator using an EM288 controlmodule. When using the two codes seperate i dont have any issues but combined it won't send any values to the serial monitor it prints the message "Startup complete".

Kind regards Sigurd :slight_smile:

Code 1: Loadcells

#include <SPI.h>
#include <Controllino.h>
#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif

//pins:
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin

//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);

const int calVal_eepromAdress = 0;
unsigned long t = 0;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(57600); delay(10);
  Serial.println();
  Serial.println("Starting...");

  LoadCell.begin();
 // LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
  float calibrationValue; 
  calibrationValue = -20.30;

  unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
  LoadCell.start(stabilizingtime, _tare);
  if (LoadCell.getTareTimeoutFlag()) {
    Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
    while (1);
  }
  else {
    LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
    Serial.println("Startup is complete");
  }


  pinMode(CONTROLLINO_D4, OUTPUT);
  pinMode(CONTROLLINO_D5, OUTPUT);

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void loop() {
  static boolean newDataReady = 0;
  const int serialPrintInterval = 1000; //increase value to slow down serial print activity

  // check for new data/start next conversion:
  if (LoadCell.update()) newDataReady = true;

  // get smoothed value from the dataset:
  if (newDataReady) {
    if (millis() > t + serialPrintInterval) {
      float i = LoadCell.getData();
      Serial.print("Load_cell output val: ");
      Serial.println(i);
      newDataReady = 0;
      t = millis();
      {
      if (i < 2000) {
      digitalWrite(CONTROLLINO_D7, HIGH);
      Serial.println("Pumpe starter");
      }
  
      else if (i > 10000){
      digitalWrite(CONTROLLINO_D7, LOW);
      Serial.println("Pumpe stopper");
    
    }
  }
{
  // receive command from serial terminal, send 't' to initiate tare operation:
  if (Serial.available() > 0) {
    char inByte = Serial.read();
    if (inByte == 't') LoadCell.tareNoDelay();
  }

  // check if last tare operation is complete:
  if (LoadCell.getTareStatus() == true) {
    Serial.println("Tare complete");
      }
    }
  }
}
}

Code 2: Actuator

#include <SPI.h>
#include <Controllino.h>

int programCount = 0;

void setup() {

  pinMode(CONTROLLINO_D2, OUTPUT);
  pinMode(CONTROLLINO_D3, OUTPUT);
}

void loop() {

     switch(programCount)
   {
    digitalWrite(CONTROLLINO_D2, HIGH); //  Sender aktuator i plus
    delay(5000);                        //  Forsinkelse
    digitalWrite(CONTROLLINO_D3, LOW);  //  Stopper signal, som holder aktuator i +position
    delay(5000);                        //  Forsinkelse
    digitalWrite(CONTROLLINO_D2, HIGH); //  Sender aktuator retur
    delay(5000);                        //  Forsinkelse
    digitalWrite(CONTROLLINO_D3, LOW);  //  Stopper signal, som holder aktuator i -position
    }
}

The 2 codes combined:

.#include <SPI.h>
#include <Controllino.h>
#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif

//pins:
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin

//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);

const int calVal_eepromAdress = 0;
unsigned long t = 0;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(57600); delay(10);
  Serial.println();
  Serial.println("Starting...");

  LoadCell.begin();
  LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
  float calibrationValue; 
  calibrationValue = -21.13; 

  unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
  LoadCell.start(stabilizingtime, _tare);
  if (LoadCell.getTareTimeoutFlag()) {
    Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
    while (1);
  }
  else {
    LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
    Serial.println("Startup is complete");
  }
  //Sensor D7

  pinMode(CONTROLLINO_D5, OUTPUT); //Aktuator 
  pinMode(CONTROLLINO_D4, OUTPUT); //Aktuator 
  pinMode(CONTROLLINO_D7, OUTPUT); //Pumpe +
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void loop() {
  static boolean newDataReady = 0;
  const int serialPrintInterval = 0; //increase value to slow down serial print activity

  // check for new data/start next conversion:
  if (LoadCell.update()) newDataReady = true;

  // get smoothed value from the dataset:
  if (newDataReady) {
    if (millis() > t + serialPrintInterval) {
      float i = LoadCell.getData();
      Serial.print("Load_cell output val: ");
      Serial.println(i);
      newDataReady = 0;
      t = millis();
    
  {
   //PUMPE
      if (i < 2000) {
      digitalWrite(CONTROLLINO_D7, HIGH);
      Serial.println("Pumpe starter");
      }
      else if (i > 10000)
      digitalWrite(CONTROLLINO_D7, LOW);
      Serial.println("Pumpe stopper");
    }
  }
{
  // receive command from serial terminal, send 't' to initiate tare operation:
  if (Serial.available() > 0) {
    char inByte = Serial.read();
    if (inByte == 't') LoadCell.tareNoDelay();
  }

  // check if last tare operation is complete:
  if (LoadCell.getTareStatus() == true) {
    Serial.println("Tare complete");
      }
    }
  }

//AKTUATOR
{
  digitalWrite(CONTROLLINO_D5, HIGH); // sender aktuator i plus
  delay(5000); //5 sekunders forsinkelse
  digitalWrite(CONTROLLINO_D5, LOW); //stopper signal som holder aktuor i +position
  delay(5000); //5 sekunders forsinkelse
  digitalWrite(CONTROLLINO_D4, HIGH); //sender aktuator retur
  delay(5000); //5 sekundersforsinkelse
  digitalWrite(CONTROLLINO_D4,LOW); //stopper signal, som holder aktutor i -position
  }
}

Welcome to the forum

I am not familiar with the Controllino, but is there some conflict in the use of pins ?

const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin
  pinMode(CONTROLLINO_D5, OUTPUT); //Aktuator
  pinMode(CONTROLLINO_D4, OUTPUT); //Aktuator

We have tried switching them and it doesnt seem to fix the problem. To my understanding the Controllino pins can be defined as the picture below shows.
The pins used for HX711 is connected to Digital 0 (4) and Digital 1 (5). The actuator is connected to Relay 4 (8) and Relay 5 (9). On the Controllino itself the relays is marked as D4 and D5. The actuators works without problem in the combined code.
The only issue we seem to meet is that we dont get any values posted in the serial monitor.
Controllino mini pinouts

This is a problem if you are using an Atmega328 based Arduino because digital pins 0 and 1 are used by the Serial interface to communicate with the PC

Why do the pins have 2 different numbers ?
Digital 0 (4) and Digital 1 (5) ?

The pins used for the HX711 amplifier has to run on 5V and the actuator need 12V. We have used the X1 for HX711 and the digital ports for the actuator. We are using the Controllino Mini for this project.

Sorry, but I cannot make any sense of what you are saying

Please post a schematic of your project with the pin numbers of all devices clearly labelled. A picture of a clear pencil and paper drawing of your project is good enough

loadcell code alone

Actuator code alone:

Combined:

  pinMode(CONTROLLINO_D5, OUTPUT); //Aktuator 
  pinMode(CONTROLLINO_D4, OUTPUT); //Aktuator 
  pinMode(CONTROLLINO_D7, OUTPUT); //Pumpe +

You standalone actuator is using different pins than your combined code. Which way to you have it wired up?

Also,

is not the correct way to measure elapsed time. It will bite you when millis() rolls over. It must be

if (millis() - t > serialPrintInterval) {

1 Like

We found a solution, thank you for the help :slight_smile:

It seem to be working fine. This string of code is from the examples page of the HX711 amblifiermodule.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.