Help with millies: do action for 1.5 sec

Hi,

Hope that someone can help me with this piece of code to make it run only for 1500 millies once!
The code should run when argumnets are met on line 135 in the whole scatch.

void solenoids() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillisS > intervalS) {
    previousMillisS = currentMillis;

    if (solenoidState == LOW)
      solenoidState = HIGH;
    else
      solenoidState = LOW;

    digitalWrite(solenoidsPIN, solenoidState);
  }
}

Whole scatch:

#include <elapsedMillis.h>
#include <AccelStepper.h>
AccelStepper stepperR(AccelStepper::DRIVER, 4, 3);     // (Step, Directon)
AccelStepper stepperL(AccelStepper::DRIVER, 7, 6);     // (Step, Directon)
int enableR = 5;
int enableL = 2;

//////////////////////////////////////////////////////


///////BLINK INTERNAL LED////////////////
const int ledPin =  13;
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 200;           // interval at which to blink (milliseconds)

///////SOLENOIDS//////
const int solenoidsPIN = 8;
int solenoidState = LOW;           
long previousMillisS = 0;       
long intervalS = 40;           

long previousMillisS1 = 0;
long intervalS1 = 1500;

//////ALARM TONE///////
const int piep = 10;

///////SENSOREN//////////
const int ENDpninnenPIN = 32;
const int HOMEpinnenPIN = 31;
const int HOMEuitbreekplaatPIN = 30;
int ENDpinnenSTATE;
int HOMEpinnenSTATE;
int HOMEuitbreekplaatSTATE;

///////CONTROLE BOARD BUTTONS-LEDS////////
const int UP = 25; // Groene draad
const int DOWN = 11; //Witte draad
int UPstate;
int DOWNstate;

boolean atStartPosition;
elapsedMillis sinceON;
elapsedMillis SolenoidInterval;

void setup() {//================================================================
  Serial.begin(9600);
  stepperR.setMaxSpeed(10000); // 90.000
  stepperR.setAcceleration(10000); // 100.000
  stepperL.setMaxSpeed(10000);//90.000
  stepperL.setAcceleration(10000); // 100.000

  pinMode(ledPin, OUTPUT);
  pinMode(solenoidsPIN, OUTPUT);
  pinMode(ENDpninnenPIN, INPUT);
  pinMode(HOMEpinnenPIN, INPUT);
  pinMode(HOMEuitbreekplaatPIN, INPUT_PULLDOWN);
  pinMode(UP, INPUT_PULLDOWN);
  pinMode(DOWN, INPUT_PULLDOWN);
  pinMode(piep, OUTPUT);


  pinMode(enableL, OUTPUT);
  pinMode(enableR, OUTPUT);
  digitalWrite(piep, HIGH);

  delay(1000);
  HOMEpinnenSTATE = digitalRead(HOMEpinnenPIN);

  while (HOMEpinnenSTATE == 1) {
    interal_led();
    Serial.println("Pinnen gaan terug naar positie");
    stepperR.setMaxSpeed(10000); // 90.000
    stepperR.setAcceleration(10000); // 100.000
    stepperL.setMaxSpeed(10000);//90.000
    stepperL.setAcceleration(10000); // 100.000
    HOMEpinnenSTATE = digitalRead(HOMEpinnenPIN);
    stepperR.move(-72500);
    stepperL.move(-72500);
    stepperR.run();
    stepperL.run();
  }
  if (HOMEpinnenSTATE == 0) {
    Serial.println("Pinnen in positie");
    stepperR.setCurrentPosition(0);
    stepperL.setCurrentPosition(0);
  }

  stepperR.setMaxSpeed(60000); // 90.000
  stepperR.setAcceleration(100000); // 100.000
  stepperL.setMaxSpeed(60000);//90.000
  stepperL.setAcceleration(100000); // 100.000

}
void loop() {//=========================================================
  ENDpinnenSTATE = digitalRead(ENDpninnenPIN);
  HOMEpinnenSTATE = digitalRead(HOMEpinnenPIN);
  HOMEuitbreekplaatSTATE = digitalRead(HOMEuitbreekplaatPIN);

  UPstate = digitalRead(UP);
  DOWNstate = digitalRead(DOWN);


  if (UPstate == 1) {
    stepperR.moveTo(71700);
    stepperL.moveTo(71700);
    stepperR.run();
    stepperL.run();
  }

  if (DOWNstate == 1) {
    stepperR.moveTo(0);
    stepperL.moveTo(0);
    stepperR.run();
    stepperL.run();
  }

  //stepperR.run();
  //stepperL.run();
  //  if (HOMEuitbreekplaatSTATE == 1) {
  //  Serial.print("HOMEuitbreekplaatSTATE:");
  //  Serial.println(HOMEuitbreekplaatSTATE);
  //  }
  //
  //  if (ENDpinnenSTATE == 0) {
  //  Serial.print("ENDpinnenSTATE:");
  //  Serial.println(HOMEuitbreekplaatSTATE);
  //}
  //  Serial.print("ENDpinnenSTATE:");
  //  Serial.print(ENDpinnenSTATE);



  if (HOMEuitbreekplaatSTATE == 1 and HOMEpinnenSTATE != 0) {
    unsigned long currentMillisS = millis();

    if (currentMillisS - previousMillisS1 >= intervalS1) {
      // save the last time you blinked the LED
      solenoids();
      previousMillisS1 += intervalS1;
      Serial.println("Nermin");

    }
  }


  if (HOMEuitbreekplaatSTATE == 1 and ENDpinnenSTATE != 0) {
    //digitalWrite(piep, LOW);
    //delay(300);
    //digitalWrite(piep, HIGH);
    //delay(300);
    digitalWrite(piep, LOW);
  }
  else
    digitalWrite(piep, HIGH);

  //  Serial.print("ENDpinnenSTATE:");
  //  Serial.print(ENDpinnenSTATE);
  //  Serial.print(" - HOMEpinnenSTATE:");
  //  Serial.print(HOMEpinnenSTATE);
  // Serial.print(" - Piep toon:");
  //  Serial.print(digitalRead(piep));
  //  Serial.print(" - HOMEuitbreekplaatSTATE:");
  //  Serial.println(HOMEuitbreekplaatSTATE);


  //Serial.print(" - UPstate:");
  //Serial.print(UPstate);
  //Serial.print(" - DOWNstate:");
  //Serial.println(DOWNstate);
  //Serial.println (digitalRead(homeL));
  //delay(10);
  interal_led();
}


void interal_led () {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }

}

void solenoids() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillisS > intervalS) {
    previousMillisS = currentMillis;

    if (solenoidState == LOW)
      solenoidState = HIGH;
    else
      solenoidState = LOW;
    digitalWrite(solenoidsPIN, solenoidState);
  }
}

So when the if statement (ile 135) is TRUE the solenoids should go on and off with interval of 40 millies and do this only for 1500 millies and that stop and wait untill the if statement is TRUE again.

I tried using millies but somehow code in the scatch (lines 135 till 145) now only make the solenoid go ON for 1500.

Hope im clear enough?
N

But what is the problem? Aka, where are you stuck?

Glazing at the code I think you want to implement state change detection.

Also, turning something on and turning it off after some time are two completely separate action.

So if the only condition to turn something off is the 1,5seconds, you should unconditionally check if that time has passed. So in pseudo code

void loop(){
  if(someCodition){
    if(anotherCondition){
      if(triggerToTurnOn){
        turnOn();
        turnOnTime = millis();
      }
    }
  }

  //completely separate
  //is check unconditionally
  if(isOn && millis() - turnOnTime >= OnTime){
    turnOff();
  }
}

Note that 'triggerToTurnOn' should only happen ONCE at the moment you want to turn it on. That's where the state change (see examples) comes into play.

Your program is confusing to me. You have this piece with millis() stuff in it

 if (HOMEuitbreekplaatSTATE == 1 and HOMEpinnenSTATE != 0) {
    unsigned long currentMillisS = millis();

    if (currentMillisS - previousMillisS1 >= intervalS1) {
      // save the last time you blinked the LED
      solenoids();
      previousMillisS1 += intervalS1;
      Serial.println("Nermin");

    }
  }

and one of the things it does is call the function solenoids() which also has millis() stuff in it.

How are those two sets of timings intended to work with each other? OR maybe there should only be timing in one place?

Will the values of HOMEuitbreekplaatSTATE and HOMEpinnenSTATE remain the same throughout the timing period? OR are those values just used to start the timing interval?

If you want something to run once only then the usual way is to have a variable (let's call it jobDone) which starts as false and is set to true when the task is complete. The code would be something like

if (jobDone == false) {
  // do the job
  jobDone = true;
}

...R

Hi Guys,

Thanks for thinking with me.

There are indeed 2 diffrent codes that should work together.

There is this piece that indefinitley truns solenoids ON and OFF every 40 milies called "void solenoids()"

void solenoids() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillisS > intervalS) {
    previousMillisS = currentMillis;

    if (solenoidState == LOW)
      solenoidState = HIGH;
    else
      solenoidState = LOW;
    digitalWrite(solenoidsPIN, solenoidState);
  }
}

Now what i need is when this statemant
if (HOMEuitbreekplaatSTATE == 1 and HOMEpinnenSTATE != 0)
is true the void solenoids() should be called to run NOT indefinutly like now but only for 1.5 seconds.

I tried to accomplish this with this piece of code (refered by Robin):

  if (HOMEuitbreekplaatSTATE == 1 and HOMEpinnenSTATE != 0) {
    unsigned long currentMillisS = millis();

    if (currentMillisS - previousMillisS1 >= intervalS1) {
      solenoids();
      previousMillisS1 += intervalS1;

    }
  }

But it does now work. So the code should be something like this:

As soon as this becomes true:
if (HOMEuitbreekplaatSTATE == 1 and HOMEpinnenSTATE != 0) {

Run void solenoids() for 1500 millies and than stop.

In this time periode The if statemant stays true for about 3000 millies and then (because of a humen interaction) it becomes false. The code should than wait untill the if statement become true and do the Void solenoids() run again for 1500 millies, then stop again.

I hope i have explained it better now?

tnx
I have tried many more solutions but nothing works like it should.

But how would it make sense that if solenoids() on it's own, when called frequently, turns on/off every 40 seconds, to randomly call it for 1,5 seconds... The change solenoids() will actually do something in that 1,5 is pretty small if it only does stuff every 40 seconds...

septillion:
But how would it make sense that if solenoids() on it’s own, when called frequently, turns on/off every 40 seconds, to randomly call it for 1,5 seconds…

He did say “40 millies” :slight_smile:

…R

Kumalix:
Now what i need is when this statemant
if (HOMEuitbreekplaatSTATE == 1 and HOMEpinnenSTATE != 0)
is true the void solenoids() should be called to run NOT indefinutly like now but only for 1.5 seconds.

I would go about that differently.

In my solenoids() function I would have a test

void solenoids() {
  if (solenoidsMayWork == true) {
     // rest of the code
  }
}

and elsewhere I would have code that makes the variable solenoidsMayWork = true for 1500 millis - something like this pseudo code

if (buttons arre correct) {
  solenoidsMayWork = true;
  solenoidsStartMillis = millis();
}
if (solenoidsMayWork == true and millis() - solenoidsStartMillis >= 1500) {
  solenoidsMayWork = false;
}

and I would call solenoids() from loop() so it is checked all the time

...R

Hi Robin,

Thanks for your reply.
Now i just relised i need to controle the NO and OFF motion of the solenoids.
Because after the 1500 millis of turning solenoids ON and OFF. The solenoids should stay in ON (HIGH) position untill the if buttons get FALSE.

So the void Soleneloid () code may not be usefull.

This is the actual piece of code that works fine BUT has delays in it.
What i was trying to do is to get rid of the delays.

 if (HOMEuitbreekplaatSTATE == 1 and HOMEpinnenSTATE != 0) {
    if (sinceON < 1500) {
      digitalWrite(solenoidsPIN, HIGH);
      delay(40);
      digitalWrite(solenoidsPIN, LOW);
      delay(40);
      digitalWrite(solenoidsPIN, HIGH);
    }
  }
  else {
    sinceON = 0;
    digitalWrite(solenoidsPIN, LOW);

Also this code uses elapsedmillis.h.
so i tried to do this:

if (HOMEuitbreekplaatSTATE == 1 and HOMEpinnenSTATE != 0) {
    if (sinceON < 2000) {
      digitalWrite(solenoidsPIN, HIGH);
      if (sinceON > 60 and sinceON <= 120) {digitalWrite(solenoidsPIN, LOW);}
      if (sinceON > 120 and sinceON <= 180) {digitalWrite(solenoidsPIN, HIGH);}
      if (sinceON > 180 and sinceON <= 240) {digitalWrite(solenoidsPIN, LOW);}
      if (sinceON > 240 and sinceON <= 300 ) {digitalWrite(solenoidsPIN, HIGH);}
      if (sinceON > 300 and sinceON <= 360) {digitalWrite(solenoidsPIN, LOW);}
      if (sinceON > 420 and sinceON <= 480) {digitalWrite(solenoidsPIN, HIGH);}
      if (sinceON > 480 and sinceON <= 540) {digitalWrite(solenoidsPIN, LOW);}
      if (sinceON > 540 and sinceON <= 600) {digitalWrite(solenoidsPIN, HIGH);}
    }
  }
 else {
    sinceON = 0;
    digitalWrite(solenoidsPIN, LOW);
  }

With this code the solenoids do work but not equely. Sometimes go ON and OFF 4 times sometimes they go only 3 times ON and OFF.

Robin2:
I would go about that differently.

In my solenoids() function I would have a test

void solenoids() {

if (solenoidsMayWork == true) {
    // rest of the code
  }
}




and elsewhere I would have code that makes the variable solenoidsMayWork = true for 1500 millis - something like this pseudo code


if (buttons arre correct) {
  solenoidsMayWork = true;
  solenoidsStartMillis = millis();
}
if (solenoidsMayWork == true and millis() - solenoidsStartMillis >= 1500) {
  solenoidsMayWork = false;
}




and I would call solenoids() from loop() so it is checked all the time

...R

By the way. This way the solenoids work perfect.
The only thing is they don’t stop after 1500 millis.
They stop only when the if statement becomes false AND after 1500 millis.

This is what i have made out of your reply:

#include <elapsedMillis.h>
#include <AccelStepper.h>
AccelStepper stepperR(AccelStepper::DRIVER, 4, 3);     // (Step, Directon)
AccelStepper stepperL(AccelStepper::DRIVER, 7, 6);     // (Step, Directon)
int enableR = 5;
int enableL = 2;

//////////////////////////////////////////////////////


///////BLINK INTERNAL LED////////////////
const int ledPin =  13;
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 200;           // interval at which to blink (milliseconds)

///////SOLENOIDS//////
const int solenoidsPIN = 8;
int solenoidState = LOW;             // ledState used to set the LED
long previousMillisS = 0;        // will store last time LED was updated
long intervalS = 40;           // interval at which to blink (milliseconds)
boolean solenoidsMayWork = false;
unsigned long solenoidsStartMillis;

long previousMillisS1 = 0;
long intervalS1 = 1500;

//////ALARM TONE///////
const int piep = 10;

///////SENSOREN//////////
const int ENDpninnenPIN = 32;
const int HOMEpinnenPIN = 31;
const int HOMEuitbreekplaatPIN = 30;
int ENDpinnenSTATE;
int HOMEpinnenSTATE;
int HOMEuitbreekplaatSTATE;

///////CONTROLE BOARD BUTTONS-LEDS////////
const int UP = 25; // Groene draad
const int DOWN = 11; //Witte draad
int UPstate;
int DOWNstate;

boolean atStartPosition;
elapsedMillis sinceON;
elapsedMillis SolenoidInterval;

void setup() {//================================================================
  Serial.begin(9600);
  stepperR.setMaxSpeed(10000); // 90.000
  stepperR.setAcceleration(10000); // 100.000
  stepperL.setMaxSpeed(10000);//90.000
  stepperL.setAcceleration(10000); // 100.000

  pinMode(ledPin, OUTPUT);
  pinMode(solenoidsPIN, OUTPUT);
  pinMode(ENDpninnenPIN, INPUT);
  pinMode(HOMEpinnenPIN, INPUT);
  pinMode(HOMEuitbreekplaatPIN, INPUT_PULLDOWN);
  pinMode(UP, INPUT_PULLDOWN);
  pinMode(DOWN, INPUT_PULLDOWN);
  pinMode(piep, OUTPUT);


  pinMode(enableL, OUTPUT);
  pinMode(enableR, OUTPUT);
  digitalWrite(piep, HIGH);

  delay(1000);
  HOMEpinnenSTATE = digitalRead(HOMEpinnenPIN);

  while (HOMEpinnenSTATE == 1) {
    interal_led();
    Serial.println("Pinnen gaan terug naar positie");
    stepperR.setMaxSpeed(10000); // 90.000
    stepperR.setAcceleration(10000); // 100.000
    stepperL.setMaxSpeed(10000);//90.000
    stepperL.setAcceleration(10000); // 100.000
    HOMEpinnenSTATE = digitalRead(HOMEpinnenPIN);
    stepperR.move(-72500);
    stepperL.move(-72500);
    stepperR.run();
    stepperL.run();
  }
  if (HOMEpinnenSTATE == 0) {
    Serial.println("Pinnen in positie");
    stepperR.setCurrentPosition(0);
    stepperL.setCurrentPosition(0);
  }

  stepperR.setMaxSpeed(60000); // 90.000
  stepperR.setAcceleration(100000); // 100.000
  stepperL.setMaxSpeed(60000);//90.000
  stepperL.setAcceleration(100000); // 100.000

}
void loop() {//=========================================================
  ENDpinnenSTATE = digitalRead(ENDpninnenPIN);
  HOMEpinnenSTATE = digitalRead(HOMEpinnenPIN);
  HOMEuitbreekplaatSTATE = digitalRead(HOMEuitbreekplaatPIN);

  UPstate = digitalRead(UP);
  DOWNstate = digitalRead(DOWN);


  if (UPstate == 1) {
    stepperR.moveTo(71700);
    stepperL.moveTo(71700);
    stepperR.run();
    stepperL.run();
  }

  if (DOWNstate == 1) {
    stepperR.moveTo(0);
    stepperL.moveTo(0);
    stepperR.run();
    stepperL.run();
  }

  //stepperR.run();
  //stepperL.run();
  //  if (HOMEuitbreekplaatSTATE == 1) {
  //  Serial.print("HOMEuitbreekplaatSTATE:");
  //  Serial.println(HOMEuitbreekplaatSTATE);
  //  }
  //
  //  if (ENDpinnenSTATE == 0) {
  //  Serial.print("ENDpinnenSTATE:");
  //  Serial.println(HOMEuitbreekplaatSTATE);
  //}
  //  Serial.print("ENDpinnenSTATE:");
  //  Serial.print(ENDpinnenSTATE);

/////////////////////////////SOLENOIDS///////////////////////////////////////
  solenoids();
  if (HOMEuitbreekplaatSTATE == 1 and HOMEpinnenSTATE != 0) {
    solenoidsMayWork = true;
    solenoidsStartMillis = millis();
  }
  if (solenoidsMayWork == true and millis() - solenoidsStartMillis >= 1) {
    solenoidsMayWork = false;
  }

////////////////////////////////////////////////////////////////////////////

  if (HOMEuitbreekplaatSTATE == 1 and ENDpinnenSTATE != 0) {
    //digitalWrite(piep, LOW);
    //delay(300);
    //digitalWrite(piep, HIGH);
    //delay(300);
    digitalWrite(piep, LOW);
  }
  else
    digitalWrite(piep, HIGH);

  //  Serial.print("ENDpinnenSTATE:");
  //  Serial.print(ENDpinnenSTATE);
  //  Serial.print(" - HOMEpinnenSTATE:");
  //  Serial.print(HOMEpinnenSTATE);
  // Serial.print(" - Piep toon:");
  //  Serial.print(digitalRead(piep));
  //  Serial.print(" - HOMEuitbreekplaatSTATE:");
  //  Serial.println(HOMEuitbreekplaatSTATE);


  //Serial.print(" - UPstate:");
  //Serial.print(UPstate);
  //Serial.print(" - DOWNstate:");
  //Serial.println(DOWNstate);
  //Serial.println (digitalRead(homeL));
  //delay(10);
  interal_led();
}


void interal_led () {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }

}

void solenoids() {
  if (solenoidsMayWork == true) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillisS > intervalS) {
      previousMillisS = currentMillis;

      if (solenoidState == LOW)
        solenoidState = HIGH;
      else
        solenoidState = LOW;
      digitalWrite(solenoidsPIN, solenoidState);
    }
  }
}

This line is not very useful

if (solenoidsMayWork == true and millis() - solenoidsStartMillis >= 1)

as it will stop things after 1 millisec

Also ...
where is the value of previousMillisS set when solenoids() is first called? It should probably be set at the same time as you set solenoidsMayWork to true

You may also wish to have a variable that identifies when solenoidsMayWork is changed to true so that the solenoids() function start things off properly.

And if you want the solenoid to be in a particular state after the 1.5 secs is complete then you should have a variable to identify when the time expires so that appropriate action can be taken.

By the way, all of these issues have to do with the logic of solving the problem rather than how to write code. It is a good discipline to write out in English (or your native language) the exact sequence of events that must happen with one step on each line. When you have that it is usually straightforward to write code to give effect to the steps and you also have a check-list that is very useful for debugging.

...R

Hi,

I got it written out. But it is still hard for me to implement millis in an reverse way.
With solenoids them selfs i could get it running.

Anyhow this should be it:

  • Two buttons get pressed (The “uitbreekplaat” starts moving and stays moving for 3 minutes)

  • ————— at the same moment ——————

  • Check if the buttons are pressed and when they are...

  • SolenoidsMayWork (but only for 1500 millis)

  • Remember the time at this moment and store it in solenoidsStartMillis??

————————at the same moment———————
This part is blurry to me.

  • Check if 1500 millis have passed with the time stored in solenoidsStartMillis
  • If time has passed Solenoids should stop
  • and the last command should be solenoidState = HIGH (the solenoids stay ON)

————————Do nothing until the two buttons get released—————————————

  • When the buttons get released set solenoidState = LOW (the solenoids get turned OFF)

----------- When the two buttons get pressed do the same all over again. ----------------

Referring to Reply #10 ...

My guess is that your list was written with the mindset of writing a program. IMHO you need to put all thoughts of programming out of your head and just concentrate on what your "machine" needs to do. If you are only focused on your machine nothing should be "blurry".

And did you consider the other comments I made in Reply #9?

...R

And did you consider the other comments I made in Reply #9?

yes i did.

where is the value of previousMillisS set when solenoids() is first called? It should probably be set at the same time as you set solenoidsMayWork to true

I made this. But i don't think its going to work :confused:
The first part ( if (solenoidsStartMillis - previousMillisS1 >=1500)) is een interval and i need the 1500 done only once every time the two buttons are pressed.

  solenoids();
  
  if (HOMEuitbreekplaatSTATE == 1 and HOMEpinnenSTATE != 0) {
  solenoidsStartMillis = millis();
  if (solenoidsStartMillis - previousMillisS1 >=1500)
     solenoidsMayWork = true;
     previousMillisS1 = solenoidsStartMillis;
  }
  if (solenoidsMayWork == true and millis() - solenoidsStartMillis >= 1500) {
    solenoidsMayWork = false;
  }

You may also wish to have a variable that identifies when solenoidsMayWork is changed to true so that the solenoids() function start things off properly.

And if you want the solenoid to be in a particular state after the 1.5 secs is complete then you should have a variable to identify when the time expires so that appropriate action can be taken.

These two i thought to implement later.
And first to focus on the 1500 millis part?

And about the machine working part.
I know exactly what the machine has to do:

  • A human presses two buttons
  • The solenoids go ON and OFF (interval of 40 millis) for 1.5 sec. When this time (1.5 sec) has past solenoids should stay ON untill the human releases the buttons. At that point the solenoids have to go OFF and stay off untill the human presses two buttons again.
    That is it.

tnx for your help!

Kumalix:
And about the machine working part.
I know exactly what the machine has to do:

  • A human presses two buttons
  • The solenoids go ON and OFF (interval of 40 millis) for 1.5 sec. When this time (1.5 sec) has past solenoids should stay ON untill the human releases the buttons. At that point the solenoids have to go OFF and stay off untill the human presses two buttons again.
    That is it.

That seems to me to require a few parts.

  • Check if the two buttons have been pressed after being off
    Must they both be off, or is it sufficient for one to be off?
  • When the buttons change from OFF to ON start the solenoid process
  • When the solenoid process starts the blink timer begins
  • Check continuously that neither button has been released
  • if a button is released
  • turn the solenoids off and reset everything
  • Provided the buttons have not been released
  • Check continuously if the solenoid timer has expired
  • if the timer has not expired
  • cycle the solenoids
  • else
  • set the solenoids on

...R

Ok, i have something like this:

  solenoids();
  
    if (HOMEuitbreekplaatSTATE == 1 and ENDpinnenSTATE == 0) {
      buttonsPressed = true;
  }
   else
      buttonsPressed = false;
      
  
  if (buttonsPressed = true) {
  solenoidsStartMillis = millis();
  if (solenoidsStartMillis - previousMillisS1 >=1500){
     solenoidsMayWork = true;
     previousMillisS1 = solenoidsStartMillis;
  }
    else
  digitalWrite(solenoidsPIN, HIGH);
  }
  
  if (buttonsPressed = false){
  solenoidsMayWork = false;
  }

only this part i'm not sure of:
solenoidsStartMillis = millis();
if (solenoidsStartMillis - previousMillisS1 >=1500){
solenoidsMayWork = true;
previousMillisS1 = solenoidsStartMillis;
}

Because with this code the solenoids never stop :frowning:

Kumalix:
Ok, i have something like this:

It is impossible to help without seeing the latest version of the whole program - parts of a program (especially this type of program) don't work in a vacuum

...R

Ok, sry.

Here is the whole program:

#include <AccelStepper.h>
AccelStepper stepperR(AccelStepper::DRIVER, 4, 3);     // (Step, Directon)
AccelStepper stepperL(AccelStepper::DRIVER, 7, 6);     // (Step, Directon)
int enableR = 5;
int enableL = 2;

//////////////////////////////////////////////////////


///////BLINK INTERNAL LED////////////////
const int ledPin =  13;
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 200;           // interval at which to blink (milliseconds)

///////SOLENOIDS//////
const int solenoidsPIN = 8;
int solenoidState = LOW;
long previousMillisS = 0;
long intervalS = 40;
boolean solenoidsMayWork = false;
unsigned long solenoidsStartMillis;

long previousMillisS1 = 0;
long intervalS1 = 1500;

//////ALARM TONE///////
const int piep = 10;

///////SENSOREN//////////
const int ENDpninnenPIN = 32;
const int HOMEpinnenPIN = 31;
const int HOMEuitbreekplaatPIN = 30;
int ENDpinnenSTATE;
int HOMEpinnenSTATE;
int HOMEuitbreekplaatSTATE;

///////CONTROLE BOARD BUTTONS-LEDS////////
const int UP = 25; // Groene draad
const int DOWN = 11; //Witte draad
int UPstate;
int DOWNstate;

boolean buttonsPressed;

void setup() {//================================================================
  Serial.begin(9600);
  stepperR.setMaxSpeed(10000); // 90.000
  stepperR.setAcceleration(10000); // 100.000
  stepperL.setMaxSpeed(10000);//90.000
  stepperL.setAcceleration(10000); // 100.000

  pinMode(ledPin, OUTPUT);
  pinMode(solenoidsPIN, OUTPUT);
  pinMode(ENDpninnenPIN, INPUT);
  pinMode(HOMEpinnenPIN, INPUT);
  pinMode(HOMEuitbreekplaatPIN, INPUT_PULLDOWN);
  pinMode(UP, INPUT_PULLDOWN);
  pinMode(DOWN, INPUT_PULLDOWN);
  pinMode(piep, OUTPUT);

  pinMode(enableL, OUTPUT);
  pinMode(enableR, OUTPUT);
  digitalWrite(piep, HIGH);

  delay(300);
  HOMEpinnenSTATE = digitalRead(HOMEpinnenPIN);

  while (HOMEpinnenSTATE == 1) {
    interal_led();
    Serial.println("Pinnen gaan terug naar positie");
    stepperR.setMaxSpeed(10000); // 90.000
    stepperR.setAcceleration(10000); // 100.000
    stepperL.setMaxSpeed(10000);//90.000
    stepperL.setAcceleration(10000); // 100.000
    HOMEpinnenSTATE = digitalRead(HOMEpinnenPIN);
    stepperR.move(-72500);
    stepperL.move(-72500);
    stepperR.run();
    stepperL.run();
  }
  if (HOMEpinnenSTATE == 0) {
    Serial.println("Pinnen in positie");
    stepperR.setCurrentPosition(0);
    stepperL.setCurrentPosition(0);
  }

  stepperR.setMaxSpeed(60000); // 90.000
  stepperR.setAcceleration(100000); // 100.000
  stepperL.setMaxSpeed(60000);//90.000
  stepperL.setAcceleration(100000); // 100.000
}
void loop() {//=========================================================
  ENDpinnenSTATE = digitalRead(ENDpninnenPIN);
  HOMEpinnenSTATE = digitalRead(HOMEpinnenPIN);
  HOMEuitbreekplaatSTATE = digitalRead(HOMEuitbreekplaatPIN);

  UPstate = digitalRead(UP);
  DOWNstate = digitalRead(DOWN);

  if (UPstate == 1) {
    stepperR.moveTo(71700);
    stepperL.moveTo(71700);
    stepperR.run();
    stepperL.run();
  }

  if (DOWNstate == 1) {
    stepperR.moveTo(0);
    stepperL.moveTo(0);
    stepperR.run();
    stepperL.run();
  }

  /////////////////////////////SOLENOIDS///////////////////////////////////////
  solenoids();

  if (HOMEuitbreekplaatSTATE == 1 and ENDpinnenSTATE == 0) {
    buttonsPressed = true;
  }
  else
    buttonsPressed = false;

  if (buttonsPressed = true) {
    solenoidsStartMillis = millis();
    if (solenoidsStartMillis - previousMillisS1 >= 1500) {
      solenoidsMayWork = true;
      previousMillisS1 = solenoidsStartMillis;
    }
    else
      digitalWrite(solenoidsPIN, HIGH);
  }

  if (buttonsPressed = false) {
    solenoidsMayWork = false;
  }

  ////////////////////////////////////////////////////////////////////////////

  if (HOMEuitbreekplaatSTATE == 1 and ENDpinnenSTATE != 0) {
    digitalWrite(piep, LOW);
  }
  else
    digitalWrite(piep, HIGH);
  interal_led();
}

void interal_led () {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite(ledPin, ledState);
  }
}

void solenoids() {
  if (solenoidsMayWork == true) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillisS > intervalS) {
      previousMillisS = currentMillis;

      if (solenoidState == LOW)
        solenoidState = HIGH;
      else
        solenoidState = LOW;
      digitalWrite(solenoidsPIN, solenoidState);
    }
  }
}

And again thank you very much for the help and your time!
N

It still seems very mixed up.

The most important thing that I see is that you are not testing for when buttonPressed changes from false to true because that is the moment when you need to initiate the solenoid action.

At the moment your code seems to initiate the solenoid action all the time that the buttons are pressed.

And why are you testing for the 1500 millisecs in loop() ? Surely that is something that should happen within the solenoids() function?

...R

hi,

The most important thing that I see is that you are not testing for when buttonPressed changes from false to true because that is the moment when you need to initiate the solenoid action.

I thought i did it with this line: if (buttonsPressed = true) or??
Or do you meen that i need to remember ButtonPress changes in a variabele?
That that doesn't make sense to me :confused:
Is it because it have to remember the only one time when the scripts runs? and al the other times it have to skip it? And in that run i have to remember current millis?

At the moment your code seems to initiate the solenoid action all the time that the buttons are pressed.

That is correct! :frowning:

And why are you testing for the 1500 millisecs in loop() ? Surely that is something that should happen within the solenoids() function?

I thought it can be done here?? Because in the solenoids() void i already are dealing with remembering millis and all other stuff? :confused:

Maybe something like this :-[ :
Have to test it tomorrow

#include <AccelStepper.h>
AccelStepper stepperR(AccelStepper::DRIVER, 4, 3);     // (Step, Directon)
AccelStepper stepperL(AccelStepper::DRIVER, 7, 6);     // (Step, Directon)
int enableR = 5;
int enableL = 2;

//////////////////////BLINK INTERNAL LED////////////////
const int ledPin =  13;
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 200;           // interval at which to blink (milliseconds)

/////////////////////SOLENOIDS/////////////////////////
const int solenoidsPIN = 8;
int solenoidState = LOW;
long previousMillisS = 0;
long intervalS = 40;
boolean solenoidsMayWork = false;
unsigned long solenoidsStartMillis;

long previousMillisS1 = 0;
long intervalS1 = 1500;

////////////////////ALARM TONE////////////////////////
const int piep = 10;

////////////////////SENSORS//////////////////////////
const int ENDpninnenPIN = 32;
const int HOMEpinnenPIN = 31;
const int HOMEuitbreekplaatPIN = 30;
int ENDpinnenSTATE;
int HOMEpinnenSTATE;
int HOMEuitbreekplaatSTATE;

///////////////CONTROLE BOARD BUTTONS-LEDS//////////
const int UP = 25; // Groene draad
const int DOWN = 11; //Witte draad
int UPstate;
int DOWNstate;

boolean buttonsPressed;

void setup() {//================================================================
  Serial.begin(9600);
  stepperR.setMaxSpeed(10000); // 90.000
  stepperR.setAcceleration(10000); // 100.000
  stepperL.setMaxSpeed(10000);//90.000
  stepperL.setAcceleration(10000); // 100.000

  pinMode(ledPin, OUTPUT);
  pinMode(solenoidsPIN, OUTPUT);
  pinMode(ENDpninnenPIN, INPUT);
  pinMode(HOMEpinnenPIN, INPUT);
  pinMode(HOMEuitbreekplaatPIN, INPUT_PULLDOWN);
  pinMode(UP, INPUT_PULLDOWN);
  pinMode(DOWN, INPUT_PULLDOWN);
  pinMode(piep, OUTPUT);

  pinMode(enableL, OUTPUT);
  pinMode(enableR, OUTPUT);
  digitalWrite(piep, HIGH);

  delay(300);
  HOMEpinnenSTATE = digitalRead(HOMEpinnenPIN);

  while (HOMEpinnenSTATE == 1) {
    interal_led();
    Serial.println("Pinnen gaan terug naar positie");
    stepperR.setMaxSpeed(10000); // 90.000
    stepperR.setAcceleration(10000); // 100.000
    stepperL.setMaxSpeed(10000);//90.000
    stepperL.setAcceleration(10000); // 100.000
    HOMEpinnenSTATE = digitalRead(HOMEpinnenPIN);
    stepperR.move(-72500);
    stepperL.move(-72500);
    stepperR.run();
    stepperL.run();
  }
  if (HOMEpinnenSTATE == 0) {
    Serial.println("Pinnen in positie");
    stepperR.setCurrentPosition(0);
    stepperL.setCurrentPosition(0);
  }

  stepperR.setMaxSpeed(60000); // 90.000
  stepperR.setAcceleration(100000); // 100.000
  stepperL.setMaxSpeed(60000);//90.000
  stepperL.setAcceleration(100000); // 100.000
}
void loop() {//=========================================================
  ENDpinnenSTATE = digitalRead(ENDpninnenPIN);
  HOMEpinnenSTATE = digitalRead(HOMEpinnenPIN);
  HOMEuitbreekplaatSTATE = digitalRead(HOMEuitbreekplaatPIN);

  UPstate = digitalRead(UP);
  DOWNstate = digitalRead(DOWN);

  if (UPstate == 1) {
    stepperR.moveTo(71700);
    stepperL.moveTo(71700);
    stepperR.run();
    stepperL.run();
  }

  if (DOWNstate == 1) {
    stepperR.moveTo(0);
    stepperL.moveTo(0);
    stepperR.run();
    stepperL.run();
  }
  /////////////////////////////SOLENOIDS///////////////////////////////////////
  solenoids();

  if (HOMEuitbreekplaatSTATE == 1 and ENDpinnenSTATE == 0) {
    buttonsPressed = true;
  }
  else
    buttonsPressed = false;

  if (buttonsPressed = true) {
      solenoidsMayWork = true;
    }
    else
    solenoidsMayWork = false;
  
  ///////////////////////////ALARM-TONE//////////////////////////////////

  if (HOMEuitbreekplaatSTATE == 1 and ENDpinnenSTATE != 0) {
    digitalWrite(piep, LOW);
  }
  else
    digitalWrite(piep, HIGH);
  interal_led();
}

/////////////////////////////END OF VOID LOOP ()///////////////////////

void interal_led () {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite(ledPin, ledState);
  }
}

void solenoids() {
  if (solenoidsMayWork == true) {
    unsigned long currentMillis = millis();

    if (currentMillis - previousMillisS1 >= 1500) {
      previousMillisS1 = currentMillis;

      if (currentMillis - previousMillisS > intervalS) {
        previousMillisS = currentMillis;

        if (solenoidState == LOW)
          solenoidState = HIGH;
        else
          solenoidState = LOW;
        digitalWrite(solenoidsPIN, solenoidState);
      }
    }
    else
    digitalWrite(solenoidsPIN, HIGH);
  }
}