Stuck with mills and IF / WHILE

I've got this public art project deadline looming and trouble figuring out this code. If anyone could help me out I'd be incredibly grateful!!!

Basically I have a couple motors that watch for a button press which turns them on. They then watch for another button press to turn off BUT I'd like for them to also turn off if 5 seconds have passed and not need to wait for the second button press.

I've got everything working fine besides figuring out where/how to put in a millis code to do this.

Again, I REALLY appreciate any help on this!


int trigger = 2; ///coming from mother board
int spiral1 = 3; //spiral 1
int spiral2 = 4; //spiral 2

int micro1 = 9; //micro switch on spiral 1
int micro2 = 10; //micro switch on spiral 2

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 5000;

void setup() {

pinMode(trigger, INPUT);
pinMode(spiral1, OUTPUT);
pinMode(spiral2, OUTPUT);

pinMode(micro1, INPUT_PULLUP);
pinMode(micro2, INPUT_PULLUP);

}

void loop() {

///////spiral1 code starts////////
/////////////////////////////////

if (digitalRead(micro1)==1)
{

{
while (digitalRead(trigger)==0)
{
delay(10);
}
}

digitalWrite(spiral1, HIGH);
delay(2000); //lets micro switch 1 get a chance to release

if (digitalRead(micro1)==0)
{

{
while (digitalRead(micro1)==0)
{
delay(10);

//////// IF 5 SCEONDS HAS PASSED THOUGH DIGITALWRITE(SPIRAL1, LOW)///////
}
}

digitalWrite(spiral1, LOW);

}
}

///////spiral2 code starts////////
/////////////////////////////////

if (digitalRead(micro2)==1)
{

{
while (digitalRead(trigger)==0)
{
delay(10);
}
}

digitalWrite(spiral2, HIGH);
delay(2000); //lets micro switch 1 get a chance to release

if (digitalRead(micro2)==0)
{
while (digitalRead(micro2)==0)
{
delay(10);
}
}

digitalWrite(spiral2, LOW);

}

}

Please use the </> icon in the posting menu and use the code tags to attach your sketch.
Use CTRL T, in the Arduino editor, to format your code.

There is an example in the IDE called blink without delay that you should master.
Robin2 has a great discussion about 'doing several things at once' that you should review.

What are you having trouble understanding about millis()?

.

Ok, there's a few things here.

if (digitalRead(micro1)==1)

The digitalRead function is defined to return HIGH or LOW. Now, these values are actually 1 and 0 in actual fact, but the function is not defined that way.

Also, you do not need to put an if() around a while() to check for a condition that the while itself checks for.

but, having said that, here's your sketch fix. Normally I'd just provide programming hints, but I approve of art and so I'll treat this as though it were posted on the "gigs and collaborations" sub, where it belongs:

const byte trigger = 2; ///coming from mother board
const byte spiral1 = 3; //spiral 1
const byte spiral2 = 4; //spiral 2


const byte micro1 = 9;  //micro switch on spiral 1
const byte micro2 = 10; //micro switch on spiral 2

unsigned long startMillis;
const unsigned long period = 5000;

void setup()   {
  pinMode(trigger, INPUT);
  pinMode(spiral1, OUTPUT);
  pinMode(spiral2, OUTPUT);

  pinMode(micro1, INPUT_PULLUP);
  pinMode(micro2, INPUT_PULLUP);
}


void loop()  {
  doSpiral(micro1, spiral1);
  doSpiral(micro2, spiral2);
}

void doSpiral(const byte micro, const byte spiral) {
  if (digitalRead(micro) == HIGH) {
    while (digitalRead(trigger) == LOW) {
      delay(10);
    }

    digitalWrite(spiral, HIGH);
    delay(2000); //lets micro switch get a chance to release

    startMillis = millis();
    while (digitalRead(micro) == 0  && millis() - startMillis < period)
    {
      delay(10);
    }
    digitalWrite(spiral, LOW);
  }
}

The code to let the micro release is a little bit odd - an unconditional 2-second delay. You realise that you cannot stop the spiral by pressing the micro a second time during this 2 seconds?

This code will also only run one spiral at a time. It's relatively easy to run both simultaneously if both micros are pressed if it makes sense to do so given that they share this "trigger" thingy.