Counters to count no of times the loop entered and to reset it when i give a i/p

Hi all,
i am trying one scenario. in this program i am using while loops to switch on and off the two heaters. so what i need is how many times the heater get on(need to count each time when switch on loop is entered) and after that when i give a digital signal high, total counts need to reset to zero. i have attached the program below:

// constants won't change. They're used here to 
// set pin numbers:
const int waterheaterinput = 2;     // the number of the pushbutton pin
const int waterheateroutput =  13;      
const int milkheaterinput = 3;     // the number of the pushbutton pin
const int milkheateroutput =  12;
const int stopatanymoment = 4; 
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;  
int buttonstate3  = 0;
void setup() {
  
  pinMode(waterheateroutput, OUTPUT); 
  pinMode(milkheateroutput, OUTPUT);
     
  // initialize the pushbutton pin as an input:
  pinMode(waterheaterinput, INPUT);
  pinMode(milkheaterinput, INPUT);
  pinMode(stopatanymoment, INPUT);

 
}

void loop(){
  // read the state of the pushbutton value:

  buttonState = digitalRead(waterheaterinput);
  buttonstate3 = digitalRead(stopatanymoment);

  
  while (buttonState == HIGH and buttonstate3 == LOW ) {     
      
    digitalWrite(waterheateroutput, HIGH);
    break; 
  } 

   while (buttonState == LOW or buttonstate3 == HIGH ) { 
  
    digitalWrite(waterheateroutput, LOW); 
    break;

  }




buttonState2 = digitalRead(milkheaterinput);
buttonstate3 = digitalRead(stopatanymoment);


while (buttonState2 == HIGH and buttonstate3 == LOW) {     
      
    digitalWrite(milkheateroutput, HIGH);
    break; 
  } 

   while (buttonState2 == LOW or buttonstate3 == HIGH) { 
    
    digitalWrite(milkheateroutput, LOW); 
    break;

  }


}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

My first observation would be that none of the while loops are necessary as they accomplish nothing. All of them break out of the loop as soon as the conditions are true but they won't even enter the loop unless the condtions are true, so what's the point of them ?

They could each be replaced by an if test like this

  if (buttonState == HIGH and buttonstate3 == LOW ) 
  {     
    digitalWrite(waterheateroutput, HIGH); 
  }

To count the number of times the heater turns on it is not good enough just to add to a counter each time the conditions are true as this will happen multiple times if the buttons are in the same position for any length of time. To do the count you need to know whether the heater is currently off and if so, turn it on and count it, else do nothing.

Something like this

  if (buttonState == HIGH && buttonstate3 == LOW  && waterHeaterOn == false) 
  {     
    digitalWrite(waterheateroutput, HIGH); 
    waterHeaterCounter++;
    waterHeaterOn = true;
  }

To reset the counter, read another button in the loop() function and, if pressed, set the counter to zero.

OP, can you tell us what the purpose of this program is?

Thank you for your help. I corrected my code and i will attach with this.

For counters i used static variables. One small doubt according the static variable. When we switch off and switch on the board will make those variables to reset(ZERO) or not..??
If it reset to zero means is there any solution to retain the previous values...??

Condition: The counter value sets to zero one and only if i press the reset button. Else it will retain the previous values(Whatever the condition may be).

Code:

// constants won't change. They're used here to
// set pin numbers:
const int waterheaterinput = 0; // the number of the pushbutton pin
const int waterheateroutput = 13; // the number of the LED pin
const int milkheaterinput = 1;
const int milkheateroutput = 12;
const int stopatanymoment = 2;
const int resetcounts = 3;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonstate3 = 0;
int waterheater;
int milkheater;

void setup() {

pinMode(waterheateroutput, OUTPUT);
pinMode(milkheateroutput, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(waterheaterinput, INPUT);
pinMode(milkheaterinput, INPUT);
pinMode(stopatanymoment, INPUT);
pinMode(resetcounts, INPUT);
digitalWrite(waterheateroutput, HIGH);
digitalWrite(milkheateroutput, HIGH);
delay(2000);
}

void loop(){
// read the state of the pushbutton value:
static int watercounter;
static int milkcounter;

buttonState = digitalRead(waterheaterinput);
buttonstate3 = digitalRead(stopatanymoment);
waterheater = digitalRead(waterheateroutput);

if (buttonState == HIGH and buttonstate3 == LOW and waterheater == LOW) {

digitalWrite(waterheateroutput, HIGH);
watercounter = watercounter + 1;
waterheater = HIGH;

}

if (buttonState == LOW or buttonstate3 == HIGH ) {

digitalWrite(waterheateroutput, LOW);

}

buttonState2 = digitalRead(milkheaterinput);
buttonstate3 = digitalRead(stopatanymoment);
milkheater = digitalRead(milkheateroutput);

if (buttonState2 == HIGH and buttonstate3 == LOW and milkheater == LOW) {

digitalWrite(milkheateroutput, HIGH);
milkcounter = milkcounter + 1;
milkheater = HIGH;

}

if(buttonState2 == LOW or buttonstate3 == HIGH) {

digitalWrite(milkheateroutput, LOW);

}

if((digitalRead(resetcounts)) == HIGH) {

static int watercounter = 0;
static int milkcounter = 0;

}

}

Thankyou once again.

Actually i am new to andruino. I am trying with basic codes.

Here i am trying for a coffee machine.

for water heater- i am gonna replace the code for coffee.(by controlling the output valves of milk and water cans).

for milk heater- i am gonna replace the code for tea.(by controlling the output valves of milk and water cans).

counters- used to count the no of tea and coffee i supplied.(which will reset only if i press reset counts buttons else retains those values).

If it reset to zero means is there any solution to retain the previous values...??

EEPROM, perhaps?

Is your coffee machine going to be HTCPCP RFC2324 compliant? ]:smiley:

If the purpose of the counters is to measure the amount of time that a valve or heater is turned on then you could put a 1 second delay at the start of your main loop so that the counters have more meaningful values, and remove all he while loops to keep the code simple.

Also it would help to define a set of states which correspond to the different heating/filling operations. The remaining code in the main loop can then use the counters or input pins the change the current state and also change the output pins when the state changes.

eg:

const int fill_water = 1;
const int preheat = 2;
const int fill_milk = 3;

Sorry dude.. i cant get you... can u please explain with some codes of which i tried...??

State-based coding is a pretty standard way to do this sort of thing. The process is broken up into a number of logical states and you define the conditions which cause the state to change and how a change in state changes your outputs.

This example code probably won't do what you want, it's just an introduction to state-based code.

// First define your states:

const int fill_water = 1;
const int preheat = 2;
const int fill_milk = 3;

// .. and declare your state variable:

int state;

// .. initialize your state

void setup () {
state = fill_water;
digitalWrite (water_valve, HIGH);
}

// and break your loop up to process each state independently

void loop () {
  delay (1000);
  seconds++;
  switch (state) {
  case fill_water:
    if (digitalRead (water_level) == HIGH) {
      digitalWrite (water_valve, LOW)
      digitalWrite (heater, HIGH);
      state = preheat;
    }
    break;

  case preheat:
    break;

  case fill_milk:
    break;
  }
}