1

I am new to Arduino programming, after days and nights trying to use array,for loop and googling a lot of article, I still unable to find the way 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. Thank You very much.

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;
  }
}
6
  • 1
    What do you need weight for? The servo functions don't use it. And what do you mean by each weighing output [...] in sequence order? You mean to store it in an array? Please describe what you want to achieve.
    – Sim Son
    Dec 7 '20 at 8:39
  • I am simulating conveyor. So first the scale weigh each object value and then the object send through a conveyor until the object separated by the servo depend on the object weight. I am using proximity switch to trigger the servo. In the code above,I am using LED to simulating servos to make the code shorter. If I am only using the code above, i cant weigh the new object before the previous object executed by the servo. If I do so, only the latest weight will be execute, so the previous object that is already on the conveyor will be executed by the wrong servo. Thank you
    – FlipFlop
    Dec 7 '20 at 11:02
  • The speed of your loop depends on whether delay(500) is executed or not, inside your servo functions. I guess that's hard to control properly. Dec 7 '20 at 11:21
  • I am using LED to simulating servo() function on the above code that's why i am using delay(). In reality, I am using servo. My main problem is how to store value for each object weight to be executed later by the designated servo. Some people suggest me to use array. But after a week, I cant figure it out how to integrate the button/ proximity switch with "for loop" .
    – FlipFlop
    Dec 7 '20 at 11:57
  • It seems like you need to learn what arrays are and how to use them (explaining them on SE would go a bit too far). It's important to keep in mind that a typical loop() is executed thousands of times per second. There are some basic concepts (like BlinkWithoutDelay and finite state machines), that make your life easier.
    – Sim Son
    Dec 7 '20 at 19:40
1

I'm still not entirely sure what you want to achieve, but maybe this will help:

You have a global variable int weight, and in int Sampling() you create another int weight = scale.get_units() which you give a value.

That second variable is only accessible inside of Sampling(). If you want to use it elsewhere for storing or in other functions, change:

int weight = scale.get_units();

to

weight = scale.get_units();

and you can then use weight wherever you like in your code, also in servo10gr() and servo20gr().

You don't need to store a sequence of weights to drive the servos; you can just use the current weight immediately to drive a servo and then get a new weight.

If, however, you want or need to keep a list of weights for some reason, there's a bit more work to be done.

There are better ways of going about this, but let's see first if this solves your problem.

3
  • First of all, i would like to say thank you for your kindness. I'll try to describe in another way. What i want to do is if my output value of each object (from the scale) was let say: 10gr>10gr>20gr>20gr>20gr>20gr>10gr> and so on, I want those each object(depend on it weight) is assign to the servo based on it designation servo, in this case servo10gr() or servo20gr(). Should I use array or another method to store those value so that those servo assignment will be executed sequencially by the servos?. Ok I'll try different method by using weight inside servo10gr() and servo20gr.
    – FlipFlop
    Dec 7 '20 at 14:44
  • @FlipFlop as I understand your requirement, this answer will not solve your problem. What you need are arrays, because you want to store "a bunch" of values. What ocrdu explained is relevant, though. As arrays can't be passed as function arguments, you'll need to make them global.
    – Sim Son
    Dec 7 '20 at 19:45
  • Thank you Sim son for your willingness to give me suggestions.
    – FlipFlop
    Dec 8 '20 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.