Counter that runs a pump every after a set number of clicks

Hi,

I have been at this for a while now as I was trying to figure it out before posting here. I have a gas meter that clicks every 30ml of gas collected. When it clicks it sends out a 5V signal which I want to collect as one count in a counter in arduino (I am using an Arduino Uno board). Then after a set number of clicks (say every 20 clicks) I want it to send a signal to a relay to turn on a dosing pump for a period of time (say for 30 seconds) and then stop and record how many times the dosing pump has turned on. I have been struggling to find a way to use a timer as well as send the signal to dose. My code is below and I need to avoid using delay as this code is a snippet of a larger group of coding. The code might have extra random snippets in as I was messing around with different options.

int gasclickpin = 8;               //set the input of gas meter clicks as pin 8
int stillagedosepin = 6;           //setting digital pin 6 as the signal to dose stillage into the AFBR.
int gasval =0;                     //variable for reading the status of the gas pin
int gasclickcounter=0;             // setting the gas clicks at zero to begin with.
int currentState =0;
int previousState=0;
long OnTime = 30*1000;
unsigned long Timer;

void setup() {
   Serial.begin(9600);          //enable the hardware serial port
      pinMode(gasclickpin, INPUT);  //enable digital pin 8 as an input
      pinMode(stillagedosepin, OUTPUT);//enable digital pin 6 as an output 
}

void loop() {
gasval= digitalRead(gasclickpin);
if(gasval==HIGH){currentState=1;}
else{currentState=0;}
if(currentState != previousState)
{if(currentState==1){gasclickcounter = gasclickcounter + 1;}
Serial.println(gasclickcounter);}
previousState = currentState;
}

void Arduino_Control(){
  if(gasclickcounter=gasclickcounter/2){digitalWrite(stillagedosepin, HIGH);}
  if(stillagedosepin==HIGH){Serial.println("Dosing\r");}

Thanks in advance! meat :)

Does anyone have any suggestions please?

anyone please? The counter works but it is just getting it to dose for a period of time every 20 clicks and then waiting till the next 20 clicks etc. Resetting the counter is also possible.

I' make the running of the pump a an if conditional condition when the click counter reaches a desired value. Reset the click counter at the end of the pump run. You probably need to use millis for the pump run timing. You may need to account for additional clicks while the pump is running.

try this:

const byte gasclickmax = 20;
int gasclickpin = 8;               //set the input of gas meter clicks as pin 8
int stillagedosepin = 6;           //setting digital pin 6 as the signal to dose stillage into the AFBR.
int Gasval =0;                     //variable for reading the status of the gas pin
int gasclickcounter=0;             // setting the gas clicks at zero to begin with.
int previousGasval=0;
long OnTime = 30UL*1000UL;
unsigned long Timer;
unsigned long PumpStartTime;

void setup() 
{
Serial.begin(9600);          //enable the hardware serial port
pinMode(gasclickpin, INPUT);  //enable digital pin 8 as an input
pinMode(stillagedosepin, OUTPUT);//enable digital pin 6 as an output 
}

void loop() 
{
Gasval= digitalRead(gasclickpin);
if(Gasval != previousGasval)
  {
  if(Gasval==HIGH)
    {
    gasclickcounter++;
    if(gasclickcounter >= gasclickmax)
      {
      gasclickcounter=0;
      StartPump();
      }
    }
  Serial.println(gasclickcounter);
  }
if(millis()-PumpStartTime > OnTime)
  {
  StopPump();  
  }
previousGasval = Gasval;
}

void StartPump()
{
PumpStartTime=millis();
digitalWrite(stillagedosepin, HIGH);
}

void StopPump()
{
digitalWrite(stillagedosepin, LOW);
}

Compiled, not tested