Delayed Relay and Instant Relay for CNC support

Hey Guys!

I’m working on some more automation project for my CNC shop and got stumped on this code…that I thought would be really simple!
Background: Using ESP8266 to control two solidstate relays. One will be for an pneumatic solenoid. The part seems to work fine. The other (my problem area) is controlling a ball valve. I want to read a simple high/low level sensor, then turn on a ball valve. This ball valve needs to stay on until high level is reached and then stay on for a little bit longer (FillTime).

Right now, as soon as the FloatState goes HIGH, it closes the valve and does not delay. Also the first 10 seconds of power one is goes to the else statement…Any input to fix these issues and make my code more robust? I’m thinking it’s something simple, but I’ve been staring at this code for days…Thanks!

#include <ezButton.h>

ezButton Currentsen(4);   //D2
const int Airblast(9);    //SD2

const int Floatsen = 0;     //D3
const int Ballvalve = 10;    //SD3

int valvetime = 0;
int FloatsenState = 0;
int FillTime = 10000;

void setup()
{
  Currentsen.setDebounceTime(50); // set debounce time to 50 milliseconds

  Serial.begin(9600); // See the connection status in Serial Monitor

  pinMode(Airblast, OUTPUT);
  pinMode(Ballvalve, OUTPUT);
}

void loop() {

  Currentsen.loop(); // MUST call the loop() functions first

  if (Currentsen.isPressed())
    Serial.println("Conveyor ON");
  digitalWrite(Airblast, HIGH);

  if (Currentsen.isReleased())
    Serial.println("Conveyor OFF");
  digitalWrite(Airblast, LOW);

  unsigned long CoolantTimer = 0;
  int FloatState = digitalRead(Floatsen);

  if ((FloatState == HIGH) && (millis() - CoolantTimer >= FillTime))
  {
    Serial.println("COOLANT GOOD!");
    digitalWrite(Ballvalve, LOW);
  }
  else {
    digitalWrite(Ballvalve, HIGH);
    Serial.println("ADDING EXTRA COOLANT");
  }
  if (FloatState == LOW)
  {
    digitalWrite(Ballvalve, HIGH);
    CoolantTimer = millis();
    Serial.println("COOLANT LOW!");
    Serial.println("VALVE OPEN");

  }

  delay(1000);
}

Okay…after some rearranging of the if/else statements, got it mostly working right. Now it’s just the start-up. It defaults to the else (Adding Extra Coolant) until it gets some time built up (the fill time). Eventually I will want the “fill time” to be a few minutes. Any idea on how to get the initial state happy if the level sensor is high? Thanks!

#include <ezButton.h>

ezButton Currentsen(4);   //D2
const int Airblast(9);    //SD2

const int Floatsen = 0;     //D3
const int Ballvalve = 10;    //SD3

int valvetime = 0;
int FloatsenState = 0;
const long FillTime = 10000;
unsigned long CoolantTimer = 0;

void setup()
{
  Currentsen.setDebounceTime(50); // set debounce time to 50 milliseconds

  Serial.begin(9600); // See the connection status in Serial Monitor

  pinMode(Airblast, OUTPUT);
  pinMode(Ballvalve, OUTPUT);
}

void loop() {

  Currentsen.loop(); // MUST call the loop() functions first

  if (Currentsen.isPressed())
    Serial.println("Conveyor ON");
  digitalWrite(Airblast, HIGH);

  if (Currentsen.isReleased())
    Serial.println("Conveyor OFF");
  digitalWrite(Airblast, LOW);

 
  int FloatState = digitalRead(Floatsen);
  unsigned long OffTimer = millis();
  
  if ((FloatState == HIGH) && ((OffTimer - CoolantTimer) >= FillTime))
  {
    Serial.println("COOLANT GOOD!");
    digitalWrite(Ballvalve, LOW);
  }
  else if (FloatState == LOW)
  {
    digitalWrite(Ballvalve, HIGH);
    CoolantTimer = OffTimer;
    Serial.println("COOLANT LOW!");
    Serial.println("VALVE OPEN");

  }
  else {
    digitalWrite(Ballvalve, HIGH);
    Serial.println("ADDING EXTRA COOLANT");
  }
  
delay(1000);
 }

@bwprecision

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

How is Floatsen wired? Why does it not have a pinMode() statement in setup()?

Hmm…that seemed to work without it, but added that in. Not sure how/why that worked either way. I usually leave it out in most of my scripts when doing basic relay/button stuff.

After hours/days of staring at this…I finally gave up on the millis() stuff. Not sure why I couldn’t make it happy. Went back to simple/basic math and this is what I came up with. Any suggestions for making it more robust or tips for next time? I plan on adding lots more automation to my machine shop. Thanks!

#include <ezButton.h>

ezButton Currentsen(4);   //D2
const int Airblast(9);    //SD2

const int Floatsen = 0;     //D3
const int Ballvalve = 10;    //SD3

int FloatsenState = 0;
const long FillTime = 10000; //On time (milliseconds) after float goes HIGH
unsigned long CoolantTimer = FillTime; //Initialize as equal for startup state

void setup()
{
  Currentsen.setDebounceTime(50); // set debounce time to 50 milliseconds

  Serial.begin(9600); // See the connection status in Serial Monitor

  pinMode(Airblast, OUTPUT);
  pinMode(Ballvalve, OUTPUT);
  pinMode(Floatsen, INPUT_PULLUP);
}

void loop() {
  Currentsen.loop();  // MUST call the loop() functions first

  if (Currentsen.isPressed())
    Serial.println("Conveyor ON");
    digitalWrite(Airblast, HIGH);

  if (Currentsen.isReleased())
    Serial.println("Conveyor OFF");
    digitalWrite(Airblast, LOW);

  int FloatState = digitalRead(Floatsen);
  if ((FloatState == HIGH) && (CoolantTimer >= FillTime))
  {
    Serial.println("COOLANT GOOD!");
    digitalWrite(Ballvalve, LOW);
  }
  else if (FloatState == LOW)
  {
    digitalWrite(Ballvalve, HIGH);
    CoolantTimer = 0;
    Serial.println("COOLANT LOW!");
    Serial.println("VALVE OPEN");
  }
  else {
    digitalWrite(Ballvalve, HIGH);
    Serial.println("ADDING EXTRA COOLANT");
    CoolantTimer = CoolantTimer + 1000;
  }
  delay(1000);
}

This (from the code in Reply #4)

  if (Currentsen.isPressed())
    Serial.println("Conveyor ON");
    digitalWrite(Airblast, HIGH);

does the digitalWrite() regardless of the state of Currentsen. Which means that this

  if (Currentsen.isPressed())
    Serial.println("Conveyor ON");
    digitalWrite(Airblast, HIGH);

  if (Currentsen.isReleased())
    Serial.println("Conveyor OFF");
    digitalWrite(Airblast, LOW);

is effectively the same as

   digitalWrite(Airblast, HIGH);
   digitalWrite(Airblast, LOW);

which is not much use.

Assuming the digitalWrite()s should be controlled by the IF tests the code should be like this - with {}

  if (Currentsen.isPressed()) {
    Serial.println("Conveyor ON");
    digitalWrite(Airblast, HIGH);
  }

...R

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.