Changing function parameter value from initial by set value with button push

Hello,

I am trying to make an inteigent doser for keeping the water level in a tank constant. Currently the sketch I am using will dose (pump water in) the tank for a set period of time such as 20 seconds every 15 minutes. I determine the dosing time but cacluteing the average evaporation rate. However, the rate of evaparotaiotn changes from day to day due to indoor temp, humidity, etc.

With this in mind I wanted to make an intelligent system that would adjust the dosing time based on the actual eval rate. How I wanted to do this was:

  1. Initially set dosing time based on the average evaporation rate
  2. Place a high water level and low water level float switch in the tank
  3. If the high float switch was triggered the dosing time would be decreased by a set increment such as 5 seconds
  4. If the low float switch was triggered the dosing time would be increased by the same set increment.

I was going to accomplish this by two if/then statements for each float switch which in the sketch is represented by button states. As proof of principle I have it set so that the dosing time when the float switch is pushed to have a new dosing time, i.e:

if (buttonState1 == HIGH)
dose = 15000;
if (buttonState2 == HIGH)
dose = 25000;
else {
dose = x;Preset dosing time of
20000 milliseconds

When what I want is to have

if (buttonState1 == HIGH)
dose = prior loop dosing time - 5000 milliseconds
if (buttonState2 == HIGH)
dose = prior loop dosing time + 5000 milliseconds
else {
dose = prior loop dosing time

Any input would be much appreciated cause this more dynamic coding is over my head.

Full sketch follows

//This sketch  triggers daily alarms at 8:30 am and 17:45 pm.
//A Timer is triggered every 15 seconds, another timer triggers once only after 10 seconds.

#include <Time.h>
#include <TimeAlarms.h>

int duration = 15; //Duration of doser loop in minutes
int dose; //How long the doser is on in milliseconds
int x = 20000; // THe intial amount of time that doser is running on each cycle intilly 
const int buttonPin1 = 10;  
int buttonState1 = 0;   // Button that is pushed when too much is dosed
int buttonState2 = 0;    //Button that is pushed when too little is dosed  
void setup()
{
 pinMode(13, OUTPUT);  //doser output
 setTime(0,0,0,1,1,10); // set time to 8:29:00am Jan 1 2010
 // create the alarms 
 Alarm.timerRepeat(0,duration,0, Repeat);                    
 Serial.begin(57600);
 if(timeStatus()!= timeSet) 
    Serial.println("Unable to sync with the RTC");
 else
    Serial.println("RTC has set the system time mother fucker");  
      Serial.print(hour());
 printDigits(minute());
 printDigits(second());
 Serial.println(); 
}

void  loop(){  
 Alarm.delay(1000); // wait one second between clock display
 
 if (buttonState1 == HIGH) {     
   dose = 15000; //decrease the dosing time when button 1 is pushed
 } 

if (buttonState2 == HIGH) {     
   dose = 25000; // increase the dosing time when buttom 2 is pushed
 } 

 else {
   dose = x; //Preset dosing time of
   20000 milliseconds
 }
 
}

void Repeat(){
 digitalWrite(13, HIGH);   // set the pump on
 Serial.print("On ");   
  Serial.print(hour());
 printDigits(minute());
 printDigits(second());
 Serial.println(); 
 delay(dose);
 digitalWrite(13, LOW);   // set the pump off
 Serial.print("Off ");  
 Serial.print(hour());
 printDigits(minute());
 printDigits(second());
 Serial.println();  
}


void digitalClockDisplay()
{
 // digital clock display of the time
 Serial.print(hour());
 printDigits(minute());
 printDigits(second());
 Serial.println(); 
}

void printDigits(int digits)
{
 Serial.print(":");
 if(digits < 10)
   Serial.print('0');
 Serial.print(digits);
}
void  loop(){ 
 Alarm.delay(1000); // wait one second between clock display

 if (buttonState1 == HIGH) {     
   dose = 15000; //decrease the dosing time when button 1 is pushed
 }

Don't you think that reading the state of the pin and assigning it to buttonState1 would be a good idea, before using the value stored in buttonState1?

PaulS: ``` void  loop(){ Alarm.delay(1000); // wait one second between clock display

if (buttonState1 == HIGH) {   
  dose = 15000; //decrease the dosing time when button 1 is pushed }



Don't you think that reading the state of the pin and assigning it to buttonState1 would be a good idea, before using the value stored in buttonState1?

In this case when the device is set up both float switches i.e. the buttons in the sketch will be off.

In this case when the device is set up both float switches i.e. the buttons in the sketch will be off.

If you are not going to read the switches, be they push-button switches or float switches, what is the purpose of having the switches?

Aka, you never ever read the button :wink:

What you want is to detect when a button becomes pressed, not when it is pressed. Aka, state change. A lot to read about it if you Google it, or just grab a library like Bounce2 to do the work for you. Set it up and use

if(buttonUp.fell()){
  dose += 5000;
}
if(buttonDown.fell()){
  dose -= 5000;
}

Also, do you use a pull down resistor on the switch? Will not work without.

Or easier, just connect the switch between GND and a pin (instead of between Vcc an the pin) and use the internal pull up resistor (pinMode(pin, PULL_UP) ). Only thing to remember, the pin now reads HIGH when NOT pressed and LOW when pressed.