I am new to Arduino programming, after days and nights trying to use array and for loop, and googling a lot of articles, I am still unable to find how to store each weighing output value from int Sampling()
to be executed by the respective servo in sequence order.
If you have any clue to share and help me, I'll be very happy.
Below is my base code:
#include <HX711.h>
///////////////////////////////////////////////////////////////////////
#define calibration_factor -251
#define LOADCELL_DOUT_PIN 7 // data
#define LOADCELL_SCK_PIN 8 // sck
////////////////////////////////////////////////////////////////////////
HX711 scale;
////////////////////////////////////////////////////////////////////////
int servo1 = 12;
int servo2 = 13;
int proxiPin1 = 2;
int proxiPin2 = 3;
////////////////////////////////////////////////////////////////////////
int toggleState = 0;
int proxiState1 = 1;
int proxiState2 = 1;
////////////////////////////////////////////////////////////////////////
int weight;
int weightFinal;
////////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(9600);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor);
scale.tare();
pinMode(servo1, OUTPUT); // simulating servo1
pinMode(servo2, OUTPUT); // simulating servo2
pinMode(proxiPin1, INPUT_PULLUP); // proximity
pinMode(proxiPin2, INPUT_PULLUP); // proximity
}
/////////////////////////////////////////////////////////////////////////
void loop()
{
Serial.print(Sampling());
Serial.print(" gram");
Serial.println();
///////////////////////////////////////////////////////////////////////
if ( weightFinal == 10 )
{
toggleState = true;
servo10gr();
}
if ( weightFinal == 20 )
{
toggleState = true;
servo20gr();
}
}
////////////////////////////////////////////////////////////////////////
int Sampling()
{
int weight = scale.get_units();
if (weight >= 10 && weight< 20)
{
weightFinal = 10;
}
else if (weight >= 20 && weight < 30)
{
weightFinal = 20;
}
return weight;
}
/////////////////////////////////////////////////////////////////////////
int servo10gr()
{
proxiState1 = digitalRead(proxiPin1);
if ( proxiState1 == 0 && toggleState == true )
{
digitalWrite(servo1, HIGH); //Simulating servo signal Pin//
delay(500);
digitalWrite(servo1, LOW);
toggleState = false;
weightFinal=0;
}
}
/////////////////////////////////////////////////////////////////////////
int servo20gr()
{
proxiState2 = digitalRead(proxiPin2);
if ( proxiState2 == 0 && toggleState == true )
{
digitalWrite(servo2, HIGH); //Simulating servo signal Pin//
delay(500);
digitalWrite(servo2, LOW);
toggleState = false;
weightFinal=0;
}
}
weight
for? The servo functions don't use it. And what do you mean byeach weighing output [...] in sequence order
? You mean to store it in an array? Please describe what you want to achieve.loop()
is executed thousands of times per second. There are some basic concepts (like BlinkWithoutDelay and finite state machines), that make your life easier.