High Speed Checkweigher ARDUINO using analog input

hello , im fixing an old check weigher i installed a load cell on the machine and hooked it to an indicator and programmed it to give an analog output bettween 0 v and 3v and used this output to indicate the mesure to the arduino (because the due operate at 3.3v). i get 0.1v(100mV) per 1 gram becasue my products dont surpass 20 grams and the tolernace is 1 gram that means product betwwen 19 grams and 21 grams are allowed to pass only.
this is my code but what ever weight i put its ejected.
the indicator gives 0v if the weight is higher than 30 grams.

if (sensor = HIGH) {
  
  
   for (sample_count=0 ; sample_count < NUM_SAMPLES ;sample_count++) {
        sum += analogRead(A6);
        sample_count++;
        delay(10);
    }
   
    voltage = ((float)sum / (float)NUM_SAMPLES * 3.3) / 4095;
    voltag = voltage;
  sample_count = 0;
    sum = 0;
    }
   
 
 if (voltag > 21|| (voltag <19 ) && (voltag >= 0.5) ) 
    {
       NRP++;
  Serial.print("Nombre of rejected pieces= ");
  Serial.print(NRP);
      delay(400); 
       
      digitalWrite(ejact,HIGH);
      delay( 900);
       digitalWrite(eject,LOW);

     }
if (sensor = HIGH) {

should be

if (sensor == HIGH) {

Post all your code. The fault might not be where you think it is.

Paul

thanks for the fast reply .Ok here is the full code . The sensor is used to know the presence of a piece on the scale .I want also to count the total number of pieces (each time the sensor is trigerd that means a piece have passed and the nombre should be incremented. the nombre of the ejected peices semms to work fine.(NRP)

const int  sensor = 2;    
const int ejact = 13; 

int WEIGHT = 0;

int TOLERANCE = 0;      
int NAP = 0; 
int NRP =0; 
 int NUM_SAMPLES =10;
int sum = 0;                    
unsigned char sample_count = 0; 
float voltage = 0.0; 
float voltag = 0.0;


void setup() 
{
 
  Serial.begin(9600);
  analogReadResolution(12);
pinMode(sensor, INPUT);
  pinMode(ejact, OUTPUT);

WEIGHT = 20;
TOLERANCE = 1;


}
 
 void loop ()
{
 buttonState = digitalRead(sensor);

if (sensor == HIGH) {
  
  
   for (sample_count=0 ; sample_count < NUM_SAMPLES ;sample_count++) {
        sum += analogRead(A6);
        sample_count++;
        delay(10);
    }
   
    voltage = ((float)sum / (float)NUM_SAMPLES * 3.3) / 4095;
    voltag = voltage;
  sample_count = 0;
    sum = 0;
    }
   
 
 if (voltag > 2.1|| (voltag <1.9 ) && (voltag >= 0.5) ) 
    {
       NRP++;
  Serial.print("Number of rejected pieces= ");
  Serial.print(NRP);
      delay(400); 
       
      digitalWrite(ejact,HIGH);
      delay( 900);
       digitalWrite(eject,LOW);

}
     }

In this part

for (sample_count=0 ; sample_count < NUM_SAMPLES ;sample_count++) {
        sum += analogRead(A6);
        sample_count++;
        delay(10);
    }

you have “sample_count++;” twice. So when sample_count gets to NUM_SAMPLES you have only taken half the number of readings you think you have.

Apart from that, your code is, well… a mess.

Paul

sorry for that , but when i try to read an analog input using that sampling loop (i replace it with a while loop ) i get the same values as a voltmeter ,but it takes some time to stabilize .so i think the problem is that i need to compare the mesured value after it stabilizes .Can you to clean my code a little bit?

Not without knowing a little more about what's going on. Are the objects moving on a conveyor belt? And the reject mechanism will push them off into a bin?

Your while() loop probably works OK because the "sample_count++" only gets done once. Do you understand what I am getting at yet?

yes the objets are moving on a conveyor belt And the reject mechanism will push them off into a bin . yes i used the for loop to make 10 samples and leave the loop .but still not sure why all piece get rejected when test the code .Could it be that the if statment is wrong

 if (voltag > 2.1|| (voltag <1.9 ) && (voltag >= 0.5) )

(and as i mentioned 2.1 volts = 21 grams , and i want product to pass only if it’s between 19 and 21).

No, I do not think that if statement is the problem. I think the loop that calculates the average is incorrect, as I explained before.I think the average is being calculated at half the true value. Try using Serial.print() to see what value is calculated for the average.

    voltag = voltage;

Why do this?

aarg:     voltag = voltage;

Why do this?

Yes, that's one of the things that make the code look like such a mess.

yeah true that line is unnecessary . Still didint try the ode .im going to test it on the machine tomorrow .This is the main loop part

 void loop ()
{
 
sensorstate = digitalRead(sensor);

if ( sensorstate == HIGH) {
  
  
   for (sample_count=0 ; sample_count < NUM_SAMPLES ;sample_count++) {
        sum += analogRead(A6);
        
        delay(10);
    }
   
    voltage = ((float)sum / (float)NUM_SAMPLES * 3.3) / 4095;
   
 Serial.print("Total number of pieces = ");

  Serial.print(NAP);

  sample_count = 0;
    sum = 0;


    }
   
 
 if (voltag > 2.1|| (voltag <1.9 ) && (voltag >= 0.5) ) 
    {
       NRP++;
  Serial.print("Number of rejected pieces= ");
  Serial.print(NRP);
      delay(400); 
       
      digitalWrite(ejact,HIGH);
      delay( 900);
       digitalWrite(eject,LOW);

}

Thanks to all for the assist i tested the code and it worked fine i just had to modify the location of the if statment

void loop {
 sensorstate = digitalRead(sensor);
 delay(200);
 
 
 if (sensorstate== HIGH) {
      NPE++;
      
  Serial.print("Nombre piece total= ");
  Serial.println(NPE);
  
  
   for (sample_count=0 ; sample_count < NUM_SAMPLES ;sample_count++) 
   {
        sum += analogRead(A6);
        
        delay(10);
    }
   
    voltage = ((float)sum / (float)NUM_SAMPLES * 3.3) / 4095;
    sample_count = 0;
    sum = 0;
    
    if (voltage > 2.1|| (voltage < 1.7) && (voltage >= 0.5) ) 
    {
       NRE++;
  Serial.print("Nombre piece rejeter= ");
  Serial.print(NRE);
      delay(400); 
       
      digitalWrite(rejactor,HIGH);
      delay( 900);
       digitalWrite(rejactor,LOW);

     }
}
}

Well done. You code would look neater if you click Tools->Auto Format.