LED is not "hard" off

hi, I am having an issue with my code where I can get the led to power on after a delay, but after a determined time when I want it off, it is lit, but dim. almost like the pullup is enabled but I didn’t call for that so I’m not sure how that can be the case. after the program reaches the end of its cycle, however, the led does go off completely. I have tried many variations of what I pasted and I just cant figure it out. I don’t need a code rewrite; areas to check or missing variables should be enough. Also, may be off topic and I have to make a new post, but would this be better suited to be a state machine? This is the basic outline of what I plan to make this. eventually I would like a similar sketch, but with 4 size options, having varying times for different loads that I would like to make selectable through a menu.

/* PERHIPERALS- VACUUM, TRANSDUCER, PUMP, SOLENOID, DISPLAY, BUTTONS OR ENCODER,(x2) solenoid
   MENU- RUN, CLEAN, COUNTS, DIAGNOSTICS(INPUTS, OUTPUTS), SETTINGS(SCREEN BRIGHTNESS/CONTRAST, BREW TIME, NITRO PRESSURIZATION LEVEL)
*/

int button = 2;
static int vacuum = 3;
int pump = 4;
int nitroSolenoid = 5;
int vacuumSolenoid = 6;
int buttonState = 0;
int newButtonState = 0;
int oldButtonState = 0;
unsigned long startMillis;
unsigned long previousMillis;
unsigned long pumpStartMillis;
long pumpDelay = 300;
long vacuumDelay = 200;
long pumpDuration = 500;
long vacuumDuration = 400;
unsigned long vacuumStartMillis;
//unsigned long buttonPushMillis = millis();
unsigned long currentMillis;
bool buttonPushed = false;
unsigned long duration;
unsigned long runTime = 0;
int pumpOn = 0;
int vacuumOn = 0;
int allDone = 600;

void setup() {
  pinMode(vacuum, OUTPUT);
  pinMode(pump, OUTPUT);
  pinMode(nitroSolenoid, OUTPUT);
  pinMode(vacuumSolenoid, OUTPUT);
  pinMode(button, INPUT);
  //buttonPushMillis = currentMillis - currentMillis + ;
  Serial.begin(9600);


}

void loop() {
  //int duration = millis();


  buttonState = digitalRead(button);    //read button
  if (buttonState == HIGH)    //proceed when button pushed
  {

    newButtonState = !newButtonState;  //change the status of button push detection

    delay(200);   //cheap debounce
  }


  if (newButtonState == HIGH)   //react from new button state
  {
    runTime;    //initialize runtime
    Serial.print("current time ");
    Serial.println(runTime);
    runTime == runTime++;   //start "runtime clock"
    //Serial.print("buttonpushmillis ");
    // Serial.println(buttonPushMillis);
  }

  if (runTime > vacuumDelay)    //"delay" to wait for vacuum to start
  {
    digitalWrite(vacuum, HIGH);   //vacuum started

    //vacuumOn = !vacuumOn;   //vacuum state changed
  }

  if (runTime > vacuumDuration)    //"timer" for running the vacuum
  {
    //vacuumOn = !vacuumOn;
    digitalWrite(vacuum, LOW);    //turn off vacuum

  }



  //return;
  /*if (runTime > pumpDelay)
    {
    digitalWrite(pump, HIGH);
    //pumpOn = !pumpOn;
    }
    if (runTime > pumpDuration)
      {
      //pumpOn = !pumpOn;
      digitalWrite(pump, LOW);
      }
  */


  if (runTime > allDone)    //"delay" to reset the timer
  {
    runTime = 0;    //timer reset
    newButtonState = 0;   //makes unit wait for next input
  }
}

What do you think this line is doing ?

runTime; //initialize runtime

side note, all comments are made to indicate what my logic was. I didn’t have any until prior to posting. anything that has been edited out was just where i was when i gave up. everything there is supposed to stay, as far as I know. obviously, unneeded or problem code will be dealt with.

its initializing my runtime variable prior to using it as my “timer”

runTime; //initialize runtime

Do you see a difference ?

runTime = 0;


When ‘ newButtonState’ is HIGH, how many times per second will this code block be executed ?

  if (newButtonState == HIGH)   //react from new button state
  {
    runTime;    //initialize runtime
    Serial.print("current time ");
    Serial.println(runTime);
    runTime == runTime++;   //start "runtime clock"
    //Serial.print("buttonpushmillis ");
    // Serial.println(buttonPushMillis);
  }

Well, that will never be true… perhaps it is just as well that you throw away the result of the comparison.
It’s equivalent to this:

false;

I would suggest taking a look at the State Change Detection example in the IDE (File->examples->02.Digital->StateChangeDetection) so you can learn how to detect WHEN a button is pressed, not just IF a button is pressed. Your code also demands that there be a pull-up resistor on your button - do you have one installed?

What LED? Is it on the pin named ‘vacuum’?

usually just once, sometimes twice. the delay above is there for that. i understand its not riht but im really just focused on getting the program roughly operational, less the button push at the moment.

yeah, vacuum and pump are both on leds. sorry for not clarifying

im not sure what you mean. I have it working as a timer. i understand its not right and i should be using millis. but its goes up and i am able to “time” off of it. and im able to reset it after everything has run through.

== is not the same as =

runTime == runTime++;
runTime= runTime++;
BUT why not just >>> - - - > runTime++;

ahh, i see what you’re saying now. i am aware they’re different. but i did notice that when i make runTime = 0 in the code block, it doesn’t work. but leaving it just as runTime; even with it being = 0 in the setup portion, it does time like i was wanting it to do

if (newButtonState == HIGH) //react from new button state
{
runTime; //initialize runtime
Serial.print("current time ");
Serial.println(runTime);
runTime == runTime++; //start “runtime clock”
//Serial.print("buttonpushmillis ");
// Serial.println(buttonPushMillis);
}

The above will run 1000s of time per second if newButtonState == HIGH
since loop() loops continually.

so basically remove the “runTime;” variable from from the code block you’re referencing?

runTime++;

The above is the same as

runTime = runTime + 1;

Is that what you wanted ?

This part is part of your problem. When ‘runTime’ is greater than ‘vacuumDelay’ it turns the ‘vacuum’ pin HIGH but if it is also greater than ‘vacuumDuration’ it immediately turns it LOW.

This is one way to make it so both ‘if’ statements aren’t true at the same time:

  if (runTime > vacuumDuration)    //"timer" for running the vacuum
  {
    digitalWrite(vacuum, LOW);    //turn off vacuum
  }
  else if (runTime > vacuumDelay)    //"delay" to wait for vacuum to start
  {
    digitalWrite(vacuum, HIGH);   //vacuum started
  }

  if (newButtonState == HIGH)   //react from new button state
  {
    //runTime;    //initialize runtime
    Serial.print("current time ");
    Serial.println(runTime);
    /*runTime ==*/ runTime++;   //start "runtime clock"
    //Serial.print("buttonpushmillis ");
    // Serial.println(buttonPushMillis);

this is what changes have been made, now it hangs up with the led on, whereas before the led would turn off at 600

too many problems to describe completely

consider

/* PERHIPERALS- VACUUM, TRANSDUCER, PUMP, SOLENOID, DISPLAY, BUTTONS OR ENCODER, (x2) solenoid
MENU- RUN, CLEAN, COUNTS, DIAGNOSTICS (INPUTS, OUTPUTS), SETTINGS (SCREEN BRIGHTNESS/CONTRAST, BREW TIME, NITRO PRESSURIZATION LEVEL)
*/

#undef MyHW
#ifdef MyHW
enum { Off = HIGH, On = LOW };

const int button          = A1;
const int vacuum          = 10;
const int pump            = 11;
const int nitroSolenoid   = 12;
const int vacuumSolenoid  = 13;

#else
enum { Off = HIGH, On = LOW };

const int button = 2;
const int vacuum = 3;
const int pump = 4;
const int nitroSolenoid = 5;
const int vacuumSolenoid = 6;
#endif

byte pinsOut [] = { vacuum, pump, nitroSolenoid, vacuumSolenoid };

int buttonState = 0;
int oldButtonState = 0;

unsigned long vacuumDelay       = 0;
unsigned long pumpDelay         = 1000;
unsigned long vacuumDuration    = 2000;
unsigned long pumpDuration      = 3000;
unsigned int allDone            = 4000;

int pumpOn   = 0;
int vacuumOn = 0;

// -----------------------------------------------------------------------------
void setup () {
    for (unsigned n = 0; n < sizeof(pinsOut); n++)  {
        digitalWrite (pinsOut [n], Off);
        pinMode      (pinsOut [n], OUTPUT);
    }

    pinMode (button, INPUT_PULLUP);
    buttonState = digitalRead (button);

    Serial.begin (9600);
}

// -----------------------------------------------------------------------------
void loop () {
    static unsigned long msecLst = 0;
           unsigned long msec    = millis ();

    // capture button state and check for change
    buttonState = digitalRead (button);

    if (oldButtonState != buttonState)  {
        oldButtonState = buttonState;

        if (On == buttonState)  {
            msecLst = msec;     // restart sequence
            digitalWrite (vacuum, vacuumOn = On);
            digitalWrite (pump, pumpOn = Off);
        }

        Serial.println (msecLst);

        delay (10);         //debounce
    }

    // must sequencially check interval from highest to lowest
    if ( (msec - msecLst) > pumpDuration) {
        digitalWrite (pump, pumpOn = Off);
    }

    else if ( (msec - msecLst) > vacuumDuration) {
        digitalWrite (vacuum, vacuumOn = Off);
    }

    else if ( (msec - msecLst) > pumpDelay) {
        digitalWrite (pump, pumpOn = On);
    }

    else if ( (msec - msecLst) > vacuumDelay) {
        digitalWrite (vacuum, vacuumOn = On);
    }
}

that did solve the lighting issue. oh, so it clicked… I looked at that so many times and never realized they were crossed like that. thank you. is there a better method to doing this? or are a ton of ifs, just fine?