liquid filler help needed

Hi All

hope you well.

Posting my code as i got it to work i am not a programmer and might have lots of things done the wrong way around so please help.

i am using a pro mini with a 16x2 LCD and a YF-S401 flow sensor, also interfaced an IR sensor to detect if their is a bucket or not.

now my question all work fine counter works count fairly accurate, nut what i would like to do is when the IR detect there is no container all activity must stop and the fill counter must reset to zero without the bottle or bucket counter reset whole process must wait untill IR show there is a container in place again. Also have a selector switch to select different values to fill

any assistance will be much appreciated

[code]

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);//initialise the library with the numbers of the interface pins

 volatile int IRQcount;
 int last_count;
 int pin = 2;
 int pin_irq = 0; //IRQ that matches to pin 2
 
 void IRQcounter() 
 {
  IRQcount++;
 }  
 
 #define relayPin 13 // 5v relay module attached to Arduino digital pin 13

   int manualFill = 4; // Bleed or manual fill
   int resetButtonB = 5; // reset Total Bottles.
   
//Selector SW
   int analogPin = A1;
   int threshold = 0;
   const int divider=93;
   int myVolume[5] = {200,440,500,750,1000};

   int analogState = 0;         // current state of the button
   int lastanalogState = 0;     // previous state of the button
   
   //pin 6 = dual color LED
   
// IR Sensor
  int pd=3;                    //Photodiode to digital pin 3
  int senRead=2;               //Readings from sensor to analog pin A2 
  int limit = 900;             //play with system to find this limit
  int val;
  
   unsigned int  flowMilliLitres;
   unsigned long totalBottels;
   //  unsigned long totalMilliLitresB;
   
 void setup() //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  {
//===== LCD Setup =====
  lcd.begin(16, 2);
  lcd.setCursor(0, 0); //sets the cursor to 1 row 1 column
  lcd.print("Filler");
  lcd.setCursor(0, 1); //sets the cursor to 2 row 1 column
  lcd.print("valv control");
  delay (5000); //sets delay for 5 seconds
  lcd.clear();  //clears the screen
  
//======RELAY======
  pinMode(relayPin, OUTPUT);  
  digitalWrite(relayPin, LOW);
  
//=====Bottle detect IR====== 
  pinMode(pd,OUTPUT);  
  digitalWrite(pd,HIGH); //supply 5 volts to photodiode  
  
//=====Manual Fill=====  
  pinMode(manualFill, INPUT);
  digitalWrite(manualFill, HIGH);
  
//======Reset Ml amount=====
  pinMode(resetButtonB, INPUT);
  digitalWrite(resetButtonB, HIGH);
  
  {
   Serial.begin (9600);
   attachInterrupt(pin_irq, IRQcounter, FALLING); 
   last_count = 0 ;
  
   }
    flowMilliLitres = 0;
    totalBottels = 0;
  }

int read_count ()  // safely read count
{
  noInterrupts () ;
  int result = IRQcount ;
  interrupts () ;
  return result ;
}

void loop() 
{

 {
  int count = read_count () ;  // read the current count without ever losing counts

  int new_counts = count - last_count ;   // get difference from last time

  last_count = count ;   // update for next time round
  
  delay (25);
  Serial.println (IRQcount);
 }
 
  int totalBottels = (int((flowMilliLitres) / myVolume[threshold]));
 
//=====selector SW=====

   threshold = analogRead(analogPin)/90;  
    
//obstacle detector IR
    
   int val=analogRead(senRead);  //photodiode reading 
      
//=====Manual Fill=====  
   
   if ((digitalRead(manualFill) == LOW) && (int(val < limit)))
      {
      digitalWrite(relayPin, HIGH);
      }
     else 
     {
      digitalWrite(relayPin, LOW);
     }
   
//======Reset Ml amount=====  
   if(digitalRead(resetButtonB) == LOW)
   {
   lcd.clear();//clears the screen // work around to start rele
   IRQcount = 0;
   totalBottels = 0;
    }

   flowMilliLitres = (IRQcount /5);

   lcd.setCursor(7, 0);
   lcd.print("Sel:");
   lcd.print(myVolume[threshold]);
   lcd.print("ML");
  
   lcd.setCursor(0, 0);
   lcd.print(flowMilliLitres);
   lcd.print("ML");
  
   lcd.setCursor(0, 1);
   lcd.print("Bottles:");
   lcd.setCursor(9, 1);
   lcd.print (totalBottels);
   lcd.print("");
   
    if (flowMilliLitres >= myVolume[threshold] )
    //|| (val > limit)) //water limit set and obstacle detect
      {
       digitalWrite(relayPin, LOW);
      }
       
//selector switch Status Change

   analogState = analogRead(analogPin);
   if (not((analogState <= lastanalogState + 10) and (analogState >= lastanalogState - 10)))
    {
   digitalWrite(relayPin, LOW);
   IRQcount = 0;
   totalBottels = 0;
   
    }
   //else
   
   //digitalWrite(relayPin, HIGH)
   
  // lcd.clear();//clears the screen
   
    lastanalogState = analogState; // save the current state as the last state,for next time through the loop
  
        }

[/code]

britsgi:
now my question all work fine counter works count fairly accurate, nut what i would like to do is when the IR detect there is no container all activity must stop and the fill counter must reset to zero without the bottle or bucket counter reset whole process must wait untill IR show there is a container in place again.

This is all a bit of a jumble and it will make the programming much easier if you tidy it up. I seems to me that there are a few separate things that you want to happen

check the state of the IR sensor
if nothing is detected
    stop any action that may be in progress
    set the fill counter to 0

But after that I get confused. You seem to be repeating yourself when you say "without the bottle or bucket counter reset whole process must wait untill IR show there is a container in place again". However you may have something else in mind.

This seems like a classic case for a State Machine approach (read up about it). "State Machine" is just a fancy name for one or more variables that keep track of what is happening - for example whether there is a container in place. With a variable to keep track of that you can have code like these snippets of pseudo code.

if (IR detects container) 
   containerInPlace = true;
else
   containerInPlace = false;
if (somethingIsBeingFilled) 
   if (containerInPlace == false)
       stop the filling
if (user presses start button)
      if (containerInPlace == true)
         start the filling

...R

thank you for response

Basically i want the IR to reset fill counter every time it detect a new container.

eg

i fill container to 500ml then pump will stop i remove container add new container then the fill counter must reset to zero and the pump must start filling again to 500ml.

and repeat this untill i select 200 then the bottle/ container total qty will reset.

currently if i reset the fill counter it reset the total qty as well. also i cant get the IR to reset the fill counter and switch rele to high

britsgi:
currently if i reset the fill counter it reset the total qty as well. also i cant get the IR to reset the fill counter and switch rele to high

Do you think the suggestions I made may help?

...R

I did try your idea as well as (while IR is activated ) but either i do it wrong or not working

britsgi:
I did try your idea as well as (while IR is activated ) but either i do it wrong or not working

Without seeing the code where you tried to implement it I cannot help.

...R

Basically i want the IR to reset fill counter every time it detect a new container.

The IR sensor does NOT detect a container, new or old. It detects that there is, or is not, something there.

If there is something there, it is you that assumes that what is there is a container.

It is up to YOU to determine that it is, or is not, the same container every time you look.

It seems unlikely that one could replace containers in such a way that there was always a container in front of the sensor. So, the Arduino should be able to see that there is a container in sight. Then, at some point it should see that there is no container. Then, at some point, is should see that there is again a container, which it could safely assume is a new container. When it sees that there is a container this time, when there wasn't one last time, reset whatever needs resetting.

This technique is illustrated in the state change detection example.