buttons and pulse reader

Hi everyone! I badly need help on a project that I'm working on. I'm planning to create an automated water filter with the use of a coin acceptor (pulse generator) and arduino. This is what I have so far

unsigned long oldMillis = 0;

int pulsePin = 52;
int ledPin = 13;
unsigned long highCounter = 0;
unsigned long duration = 0;
int pulse = 0;
int lastPulse = LOW;
 
void setup() {
   pinMode(pulsePin, INPUT);
  pinMode(ledPin, OUTPUT);
   digitalWrite(pulsePin, HIGH);   
   Serial.begin(4800);
   Serial.println("Stand Alone Filtered Water");
   Serial.println("Refilling Station");
}

void loop() {
  pulse = digitalRead(pulsePin);  
  if (pulse != lastPulse) {
    lastPulse = pulse;
    if (pulse == HIGH) highCounter++;
  } 

  // print and reset highCounter every seconds
  if ( millis() - oldMillis >= 5000 )
  {
    oldMillis = millis();
    Serial.println(highCounter);
    if (highCounter == 1)
    {
      digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
    }
    
    if (highCounter == 5)
    {
      digitalWrite(ledPin, HIGH);
      delay(5000);
      digitalWrite(ledPin, LOW);
    }
    if (highCounter == 10)
    {
      digitalWrite(ledPin, HIGH);
      delay(15000);
      digitalWrite(ledPin, LOW);
    }
    highCounter = 0;
  }
}

So the program counts the pulses received from the coin acceptor within 1 second and lights a LED depending on the condition met. What I want to do is to add a button on my system and make a condition that if button1 is pressed it would wait for 1 pulse then once met would light the LED then button2 will wait for 5 pulses and button3 will wait for 10 pulses. Help I'm not very good in coding and I don't know where to place that additional bit of code. PLEASE HELP! :slight_smile:

then button2 will wait for 5 pulses and button3 will wait for 10 pulses.

I don't understand this. Switches don't wait for anything. You can make the Arduino wait for events when a switch is pressed.

But, if you are diddling around waiting, you'll miss pulses from the coin acceptor.

Please explain, in terms of the water filter system, what these switches are supposed to do/emulate/test.

I see two major problems after a quick glance...

  1. There is no pin 52

  2. Switches bounce, and you are not taking steps to debounce them.

The coin acceptor receives 3 types of coins that is 1, 5, 10 peso coins. The 1 peso coin sends 1 pulse, then the 5 peso coin sends 5 pulses and the 10 peso coins sends 10 pulses..
I would like to create a system where for instance when button1 is pressed it will display "Insert 5 peso coin/s" and if for example 1 peso coin is inserted (number of pulses will be decremented from 5)it will then display "Insert 4 peso coin/s" once the requirement (0 coins required) is filled it will send a high signal to pin 13 to turn on the water filter.
Btw I'm using Arduino Mega that's why im using digital pin 52 to receive the pulses from the coin acceptor. And how do I debounce the switch? :slight_smile:

I would like to create a system where for instance when button1 is pressed it will display "Insert 5 peso coin/s" and if for example 1 peso coin is inserted (number of pulses will be decremented from 5)it will then display "Insert 4 peso coin/s" once the requirement (0 coins required) is filled it will send a high signal to pin 13 to turn on the water filter.

So, there is no waiting involved. A switch is determined to transition from not pressed to pressed. When that happens, you set a value to the number of pesos that need to be entered and display a message.

Each time through loop, you check for a pulse. If you find one, you count it, decrement the number of pesos still needed, and display a new message. If the number of pesos still needed is 0, open the valve for the required period of time, based on the number of pesos received.

Yes yes exactly!! Just used the wrong word for it =)

Help anyone?

Help anyone?

Help with what? You haven't posted any code, yet. If you want someone to write the code for you, get your wallet out, and head over to Gigs and Collaborations.

Is it something like this you would like to achieve?
The SM librabry and its documentation is here: Arduino Playground - HomePage

Here is some code that you might want to try:

#include <SM.h>

SM Machine(WaitH, WaitB);

const int Button1 = 2;
const int Button2 = 3;
const int Button3 = 4;
const int Pulse = 5;
const int Out1 =10;

int Countvalue;
int CurrentCount;

void setup(){
  Serial.begin(115200);
  pinMode(Out1, OUTPUT);
  Serial.println("Setup done");
}//setup()

void loop(){
  EXEC(Machine);
}//loop()

State WaitH(){
  Serial.println("Waitnig for command");
}//WaitH()

State WaitB(){
  Countvalue = 0;
  CurrentCount=0;
  if(digitalRead(Button1)) Countvalue = 1; 
  if(digitalRead(Button2)) Countvalue = 5;
  if(digitalRead(Button3)) Countvalue = 10;
  if(Countvalue) Machine.Set(CountLH, CountLB);
}//Wait()

State CountLH(){
  Serial.println("waiting for pulse");
}//CountLH()

State CountLB(){
  if(digitalRead(Pulse)) Machine.Set(CountH, CountB);//pulse detected, count
  if(Machine.Timeout(1000)) Machine.Set(WaitH, WaitB);//No coins within 1 s, go back to waiting
}//CountLB()

State CountH(){
  CurrentCount++;
  Serial.println(CurrentCount);
}//CountH()

State CountB(){
  if(CurrentCount == Countvalue) Machine.Set(WorkH, WorkB);//count reached, go to work
  if(!digitalRead(Pulse)) Machine.Set(CountLH, CountLB);//Wait for low pulse
}//CountB()

State WorkH(){
  Serial.println("Working");
  digitalWrite(Out1, HIGH);//start working
}//WorkH()

State WorkB(){
  if(Machine.Timeout(Countvalue*1000)) Machine.Set(WaitH, WaitB);//keep on working for specified time
}//WorkB()