Go Down

Topic: counter 100 = 1 (Read 653 times) previous topic - next topic

zago1819

Hi all my Arduino friends.
I not know much about programming. So help of Google and Arduino forum I make a counter with display on Arduino nano it's work ok and counting . And also resetting .
Just I want then it's reached to 100 it give me output 5 high for a sec and then againe on 200 and then on 300..
I mean every 100 pulses he count he give me output pin 5 high for a sec.
Can anybody help me for this ?
Thanks

DannySwarzman

Do you want the counter to stop while the LED is on? How long is a pulse for your counter?


zago1819

Hi. Pulses is connected a flow meter . As fast pass water it's count.
No I not want it stop then led or relay on. Just it give me a pulse out even for half second.
Start and stop not need control. Just he continued counting and each 100 pulses give me 1 pulse out put.
I not know how paste here my code but if anybody like send me mail I can give code there .
zago1819@yahoo.com
Thanks

wildbill

Look at the how to "use the forum" link at the top. It'll tell you how to paste your code.

zago1819

Here is code. Here I used display with 6 wiers so can be use for high output pin 9 or any other which left free.
Thanks



Code: [Select]

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Pines arduino to lcd

     //-------Pins-----//
int Relay = 13;                 //Solenoid valve open/close
int start_stop = 8;             //Start/Stop button
const int sensor_pulse = 12;    //Sensor pulse
int rst_cnt = 10;               // Reset counter button
//---------Storage debounce function-----//
boolean currentstart_stop = LOW;         
boolean laststart_stop =LOW;           
boolean lastsensor_pulse = LOW;
boolean currentsensor_pulse = LOW;
boolean lastrst_cnt = LOW;
boolean currentrst_cnt = LOW;
boolean RelayState = LOW;             
           
int counter = 0;   

void setup() {
  pinMode(Relay, OUTPUT);
  lcd.begin(16, 2);
  lcd.setCursor(5, 0);
  lcd.print("COUNTER");
  lcd.setCursor(0, 1);
  lcd.print("PULSES");

}
   //----Debouncing function----//
boolean debounce(boolean last, int pin)
{
boolean current = digitalRead(pin);
if (last != current)
{
delay(5);
current = digitalRead(pin);
}
return current;
}

void loop() { 
currentstart_stop = debounce(laststart_stop, start_stop);         //Debounce for Start/Stop Button
currentsensor_pulse = debounce(lastsensor_pulse, sensor_pulse);   //Debounce for Sensor pulse Button
currentrst_cnt = debounce(lastrst_cnt, rst_cnt);                 //Debounce for reset counter Button

  //-----Start/Stop toggle function----//
  if (currentstart_stop == HIGH && laststart_stop == LOW){
   
    if (RelayState == HIGH){         //Toggle the state of the Relay
      digitalWrite(Relay, LOW);
      RelayState = LOW;
   

}
    else{
      digitalWrite(Relay, HIGH);
      RelayState = HIGH;
    }
  }
 
  laststart_stop = currentstart_stop;
 
  if (lastsensor_pulse== LOW && currentsensor_pulse == HIGH){
      counter=counter+ 1;
  }
      lastsensor_pulse = currentsensor_pulse;
 
lcd.setCursor(7, 1);
lcd.print(counter);

if(RelayState == LOW){ //Reset counter while sistem is not running
 if (currentrst_cnt == HIGH && lastrst_cnt == LOW){//Reset Counter
    lcd.setCursor(6, 1);         // Clear CNT area
    lcd.print("          ");
    counter= 0;         
   
  }
  lastrst_cnt = currentrst_cnt;
}
 
}// end void loop

david_2018

If I understand correctly you want to output a pulse that is high for 1/2 second when the counter value is a multiple of 100.

Determining when to generate the pulse is fairly easy, just check to see when the counter can be evenly divided by 100.  Using integer math, you can use the following test for that:

if ( (counter - ( (counter / 100) * 100 ) ) == 0 )

but an easier method is to use the modulo operator, which will divide by a number, and return only the remainder from the division:

if ( ( counter % 100 ) == 0 )


Once you determine when to generate the pulse, set the output high and start a 1/2 second timer.  To use a timer, save the current time given by the millis() function, then test to see when the current time is 500 milliseconds more than the saved time. When you reach that time set the output low again.

There are a couple of other issues that have to be considered with your code.  The first is what to do if the counter is very slow, or stops at an exact multiple of 100, because that might cause the output to pulse constantly.  To prevent this from happening, you can save the value of the counter, and check to make sure it has changed before setting the output high.  The other issue is when you reset the counter. A counter value of 0 is also evenly divisible by 100, and presumably you don't want to pulse the output when you press the reset switch. To prevent this, check to see if the counter equals 0 before setting the output high. 

Here is your code modified to generate a 1/2 second pulse on pin 9, see if it will work properly:
Code: [Select]


#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Pines arduino to lcd

//-------Pins-----//
int Relay = 13;                 //Solenoid valve open/close
int start_stop = 8;             //Start/Stop button
const int sensor_pulse = 12;    //Sensor pulse
int rst_cnt = 10;               // Reset counter button
//---------Storage debounce function-----//
boolean currentstart_stop = LOW;
boolean laststart_stop = LOW;
boolean lastsensor_pulse = LOW;
boolean currentsensor_pulse = LOW;
boolean lastrst_cnt = LOW;
boolean currentrst_cnt = LOW;
boolean RelayState = LOW;

int counter = 0;

//------------LED and timer------------//
const byte LEDpin = 9;                 //LED indicator output pin
unsigned long starttime;               //time that counter started
unsigned long duration = 500UL;        //duration of timer
int LED_counter = 0;
boolean timer_running = false;

void setup() {
  pinMode(Relay, OUTPUT);
  pinMode(LEDpin, OUTPUT);
  digitalWrite(LEDpin, LOW);
  lcd.begin(16, 2);
  lcd.setCursor(5, 0);
  lcd.print("COUNTER");
  lcd.setCursor(0, 1);
  lcd.print("PULSES");

}
//----Debouncing function----//
boolean debounce(boolean last, int pin)
{
  boolean current = digitalRead(pin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(pin);
  }
  return current;
}

void loop() {
  currentstart_stop = debounce(laststart_stop, start_stop);         //Debounce for Start/Stop Button
  currentsensor_pulse = debounce(lastsensor_pulse, sensor_pulse);   //Debounce for Sensor pulse Button
  currentrst_cnt = debounce(lastrst_cnt, rst_cnt);                 //Debounce for reset counter Button

  //-----Start/Stop toggle function----//
  if (currentstart_stop == HIGH && laststart_stop == LOW) {

    if (RelayState == HIGH) {        //Toggle the state of the Relay
      digitalWrite(Relay, LOW);
      RelayState = LOW;


    }
    else {
      digitalWrite(Relay, HIGH);
      RelayState = HIGH;
    }
  }

  laststart_stop = currentstart_stop;

  if (lastsensor_pulse == LOW && currentsensor_pulse == HIGH) {
    counter = counter + 1;
  }
  lastsensor_pulse = currentsensor_pulse;

  lcd.setCursor(7, 1);
  lcd.print(counter);

  if (RelayState == LOW) { //Reset counter while sistem is not running
    if (currentrst_cnt == HIGH && lastrst_cnt == LOW) { //Reset Counter
      lcd.setCursor(6, 1);         // Clear CNT area
      lcd.print("          ");
      counter = 0;

    }
    lastrst_cnt = currentrst_cnt;
  }

  if ((counter % 100 == 0) && (LED_counter != counter)) {
    //if counter can be evenly divided by 100
    // and LED has not already been turned on for this count
    // note: the % divides by 100 and gives the remainder
    LED_counter = counter;
    if (counter != 0) {              //check for counter reset
      timer_running = true;          //start the LED timer
      digitalWrite(LEDpin, HIGH);    //turn the LED ON
      starttime = millis();          //save the start time for the timer
    }
  }
  //if timer is running and LED has been on for the needed duration
  if ((timer_running) && (millis() - starttime > duration)) {
    timer_running = false;         //stop the LED timer
    digitalWrite(LEDpin, LOW);     //turn the LED OFF
  }


}// end void loop

Go Up