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
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
}
}