Hi, I am trying to integrate solenoid lock, reed switch and load cell sensor in my arduino program. The issue I am facing is when I am using void loop the weight cell show the weight readings initially as 0 (no issues here), however, when I am trying to avoid using void loop and integrate lock and reed switch in my arduino program (my project requires not to get stuck in the void loop), the load cells initially show a different value (~34). I am unable to know here the issue is. Does integrating lock and reed switch affect the load cell readings? Here is my program when I am using the void loop in my program -
#include<HX711_ADC.h>
HX711_ADC LoadCell(4,5);
//Arduino state flag
bool arduino_state_flag = true;
int number = 0;
int lastWeightRecorded= 0;
int totalWeight = 0;
float i;
int led_pin= 9;
bool led_flag = false;
void setup()
{
Serial.begin(38400);
LoadCell.begin();
LoadCell.start(2000);/
LoadCell.setCalFactor(130040);
pinMode(led_pin, OUTPUT);
}
void loop()
{
const unsigned long time_delay = 2000UL;
static unsigned long lastSampleTime = 0 - time_delay;
unsigned long now = millis();
LoadCell.update();
if(now - lastSampleTime >= time_delay)
{
lastSampleTime += time_delay;
i = LoadCell.getData();
int i_gram = i * 1000;
if (i_gram > 5)
{
led_flag = true;
ledBlink();
if(i_gram != lastWeightRecorded)
{
lastWeightRecorded = i_gram;
totalWeight = i_gram;
//data[0] = totalWeight;
//sendDataSerial();
Serial.println("Testing");
}
Serial.println("External object detected");
Serial.print("Its weight is :");
Serial.println(totalWeight);
}
else if ((i_gram <= -5) && (i_gram != lastWeightRecorded ))
{
//It means some object has been removed from the basket so send the data to raspberry Pi
led_flag = false;
ledBlink();
lastWeightRecorded = i_gram;
totalWeight = i_gram;
Serial.print("Weight[grams]: ");
Serial.println(totalWeight);
}
else if ((i_gram == 0) || (i_gram == -0) || (i_gram == lastWeightRecorded))
{
led_flag = false;
ledBlink();
totalWeight = i_gram;
Serial.println("No weight change is detected");
Serial.print("Tol Weight: ");
Serial.println(totalWeight);
}
}
}
bool ledBlink()
{
if(led_flag == true){
digitalWrite(led_pin, HIGH);
delay(500);
digitalWrite(led_pin, LOW);
delay(500);
}
else if(led_flag == false){
digitalWrite(led_pin, LOW);
}
}
The serial output is in Attachment 1. Here is my program without using void loop-
#include<HX711_ADC.h>
HX711_ADC LoadCell(4,5);
//Arduino state flag
bool arduino_state_flag = true;
int number = 0;
int lastWeightRecorded= 0;
int totalWeight = 0;
float i;
int i_gram;
int led_pin= 9;
bool led_flag = false;
//pins for lock
int solenoidPin = 7;
//pins for reed switch
int reedSwitch = 2;
bool reedSwitchStatus;
bool receivedValue = true;
int x;
void setup()
{
Serial.begin(38400);
pinMode(solenoidPin, OUTPUT);
pinMode(reedSwitch, INPUT);
digitalWrite(solenoidPin, HIGH);
LoadCell.begin();//start connection to HX711
LoadCell.start(2000);//load cells get 2 seconds to stabilize
LoadCell.setCalFactor(130040);
//For LED pin
pinMode(led_pin, OUTPUT);
doorUnlock();
}
void doorUnlock()
{
Serial.println("Door will be unlocked now");
digitalWrite(solenoidPin, LOW);
delay(5000);
checkReedSwitchStatus();
}
void checkReedSwitchStatus()
{
reedSwitchStatus = digitalRead(reedSwitch);
if (reedSwitchStatus == 1)
{
x = 1;
weightTransaction();
}
else
{
x = 0;
weightTransaction();
}
}
void weightTransaction()
{
while(x==1)
{
beginTransaction();
delay(1000);
checkReedSwitchStatus();
}
while(x==0)
{
endTransaction();
}
}
void beginTransaction()
{
i = LoadCell.getData();
i_gram = i * 1000;
Serial.print("Check i_gram ");
Serial.println(i_gram);
if (i_gram > 5)
{
led_flag = true;
ledBlink();
if(i_gram != lastWeightRecorded)
{
lastWeightRecorded = i_gram;
totalWeight = i_gram;
Serial.println("Loop 1");
}
Serial.println("External object detected");
Serial.print("Its weight is :");
Serial.println(totalWeight);
data[0] = totalWeight;
}
else if ((i_gram <= -5) && (i_gram != lastWeightRecorded ))
{
Serial.println("Loop 2");
led_flag = false;
ledBlink();
lastWeightRecorded = i_gram;
totalWeight = i_gram;
Serial.print("Weight[grams]: ");
Serial.println(totalWeight);
data[0] = totalWeight;
}
else if ((i_gram == 0) || (i_gram == -0) || (i_gram == lastWeightRecorded))
{
Serial.println("Loop 3");
led_flag = false;
ledBlink();
totalWeight = i_gram;
Serial.println("No weight change is detected");
Serial.print("Tol Weight: ");
Serial.println(totalWeight);
}
}
void endTransaction()
{
Serial.println("ReedSwitch Detected. The system will close now");
delay(3000);
digitalWrite(solenoidPin, HIGH);
data[0] = 1000;
LoadCell.powerDown();
Wire.endTransmission();
}
void loop()
{
delay(50);
}
bool ledBlink()
{
if(led_flag == true){
digitalWrite(led_pin, HIGH);
delay(500);
digitalWrite(led_pin, LOW);
delay(500);
}
else if(led_flag == false){
digitalWrite(led_pin, LOW);
}
}
The serial output for the above program is shown in Attachment 2. I am unable to understand why does the load cell show ~-34 in the second program. Kindly let me know where the issue is. Thanks in advance.