Hi,
I have a working code for short and long press for a proximity switch, and using delay i can easily have a pump (currently an LED while testing) turn on for different lengths of time depending on the long or short press.
I am going to have a display and will be adding a rotary encoder(i have working code for) to change these length of times. - therefore I want to convert these times to using millis rather than delays.
This is where I am currently stuck, trying to work out how to implement this.
The pump small function is what I am trying to convert to millis with my attempt being done on the pump large version
Any help would be appreciated
///////////////////////////////////////////// PROXIMITY SWITCH /////////////////////////////////////////////
static const int proximity_pin = 13; // proximity switch pin
int proxy_state_previous = LOW; // previous state of the proximity switch
unsigned long min_long_proxy_duration = 1200; // minimum proximity switch for long press
unsigned long proxylongMillis ; // time in ms when the proximity was pressed
bool proxy_state_long = false; // true if long press
const int interval_proxy = 200; // debouncing the proximity switch
unsigned long previousproxyMillis; // time of latest proximity sensor reading
unsigned long currentproxyMillis; // current millis value for proximity
unsigned long proxy_active_duration; // length of time the proximity sensor is activated
/////////////////////////////////////////////////// PUMP ///////////////////////////////////////////////////
const int pump_pin = 12; // proximity switch pin
int pumpONdelay = 1000; // pump delay at 1 second value
unsigned long pumpDUR_sml = 2000; // milk amount in ms for small
unsigned long pumpDUR_lrg = 4000; // milk amount in ms for large
unsigned long currentpumpMillis; // current millis value for pump
unsigned long pumponMillis; // current millis value for proximity
unsigned long proxytopumpMillis = 0; // time proximity sensor was released
bool pump_state = false; // flag when pump is on or not
bool pump_ready = false; // flag for when proximity sensor has been let go
///////////////////////////////////////////////// GENERAL /////////////////////////////////////////////////
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX SETUP XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//
void setup() {
Serial.begin(9600);
Serial.println("To milk this machine, place jug next to sensor");
pinMode(proximity_pin, INPUT); // initialising proximity pin as an input with a pullup resistor
pinMode(pump_pin, OUTPUT); // set the digital pin for the pump as an output
digitalWrite(pump_pin, LOW);
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX LOOP XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//
void loop() {
currentproxyMillis = millis(); // store current proxy millis value
currentpumpMillis = millis(); // get current millis value when the long state has been activated
read_proximity_state(); // run read proximity switch function
//Serial.println(proxy_state); // testing proximity state either high or low
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX FUNCTIONS XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//
///////////////////////////////////////////// PROXIMITY STATE /////////////////////////////////////////////
void read_proximity_state(){
if (currentproxyMillis - previousproxyMillis > interval_proxy){ // if there is a time different between current millis value and previous read greater than the proximity interval value
int proxy_state = digitalRead(proximity_pin); // read digital vlaue of the proximity sensor (HIGH or LOW)
if (proxy_state == HIGH && proxy_state_previous == LOW && !proxy_state_long){ // if the button has been pushed AND the button hadn't previously been pushed AND if there wasnt a measurement running to determine how long the button had been pressed for
proxylongMillis = currentproxyMillis;
proxy_state_previous = HIGH; // set proximity state to be high
// Serial.println("Proximity Switch Activated"); // print line of text
}
proxy_active_duration = currentproxyMillis - proxylongMillis; // calculate how long the button has been pressed
if (proxy_state == HIGH && !proxy_state_long && proxy_active_duration >= min_long_proxy_duration){ // if the proximity sensore was activated
proxy_state_long = true;
proxytopumpMillis = currentpumpMillis; // update time proximity was released
pump_ready = true;
Serial.println("Long Proximity Switch Activated");
pumplarge();
}
if (proxy_state == LOW && proxy_state_previous == HIGH){ // if the proximity was deactivated and proximity was activated before
proxy_state_previous = LOW; // set proximity state to LOW
proxy_state_long = false; // set long press state to false
// Serial.println("Proximity Switch Deactivated"); // print line of text
if(proxy_active_duration < min_long_proxy_duration){ // if the proximity active duration is smaller then the minimal time needed for a long press
Serial.println("Short Proximity Switch Activated"); // print line of text
pumpsmall();
}
}
previousproxyMillis = currentproxyMillis; // store current millis value
}
}
/////////////////////////////////////////////// PUMP SMALL ///////////////////////////////////////////////
void pumpsmall(){
delay(pumpONdelay);
digitalWrite(pump_pin,HIGH);
delay(pumpDUR_sml);
digitalWrite(pump_pin,LOW);
}
/////////////////////////////////////////////// PUMP LARGE ///////////////////////////////////////////////
void pumplarge(){
// currentpumpMillis = millis(); // get current millis value when the long state has been activated
proxytopumpMillis = currentpumpMillis; // update time proximity was released
pump_ready = true;
if(pump_ready){
if((unsigned long)(currentpumpMillis - proxytopumpMillis) >= pumpONdelay){
digitalWrite(pump_pin, HIGH);
pump_state = true;
pumponMillis = currentpumpMillis;
pump_ready = false;
}
}
if (pump_state){
if((unsigned long)(currentpumpMillis - pumponMillis) >= pumpDUR_sml){
pump_state = false;
digitalWrite(pump_pin, LOW);
}
}
}