Want to print some sensor value using > and < and print sum total

I have an infrared emitter and receiver. I want to detect some object and calculate electrical probe changes on serial monitor while passing in front of IR sensor. Each object has a different value when passed, Object doesn't pass continuously but one by one.

I am trying to write a code using following condition.

// If analogRead < 35, Serial Print 0.
// if analogRead > 35 start sum, and < 35 stop sum.
I want to sum the values ​​between "start sum" and "stop sum" and print the total value.
//Else print 0.

Please show us your efforts.
Discuss about the code without the code itself is pointless.

@mustkimdhukka

Your duplicate post has been deleted

I am trying the below code but not getting the total sum

int sum = 0; 

void setup() {
  Serial.begin(9600); 
}

void loop() {
  int ifRead = analogRead(A0);  
  if (ifRead < 35) { 
    Serial.println("0"); 
    sum = 0; // reset the sum
  }
  else { 
    sum += ifRead; 
    if (sum >= 35) { 
      Serial.println(sum); 
      sum = 0; // reset the sum
    }
  }
  delay(100); 
}

If you only have 2 conditions you only have to test for one of them. :nerd_face:
Twice in this case. :thinking:

// If analogRead < 35,   
     // if sum, print sum, sum=0,
     // else, print sum  
// else, sum += analogRead

Your sum is not increasing because you zero sum in every condition (<35 and >=35). Here is a simulation that has removed the sum = 0; from >=35:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.