digitalWrite tricks???

alright so here is my issue. I am basically writing an on/off sequence for a haunted prop… to make it simple i have two pneumatic valves that operate open or thru when contact is HIGH and closed when contact is LOW

in my code i am trying to figure out how to make 2 digital OUTPUT pins go HIGH at the same time but go LOW on different delays. CODE to follow.

const int red = 2;
const int blue = 3;
const int black = 4;
const int runLights = 5;
const int strobe = 6;
const int start = 7;
const int twitch = 8;
const int dropBod = 9;
const int surgicalTube = 10;
const int soundR2 = 11;



const int trigger1 = 14;
const int trigger2 = 15;
const int trigger3 = 16;
const int trigger4 = 17;

void setup ()

{
  pinMode (red, OUTPUT);
  pinMode (blue, OUTPUT);
  pinMode (black, OUTPUT);
  pinMode (runLights, OUTPUT);
  pinMode (strobe, OUTPUT);
  pinMode (start, OUTPUT);
  pinMode (twitch, OUTPUT);
  pinMode (dropBod, OUTPUT);
  pinMode (soundR2, OUTPUT);
  pinMode (surgicalTube, OUTPUT);

  
 
  pinMode (trigger1, INPUT);
  pinMode (trigger2, INPUT);
  pinMode (trigger3, INPUT);
  pinMode (trigger4, INPUT);
  digitalWrite (runLights, HIGH);
}

void loop ()
{ // identifing different trigger sequences based on button pushed
  // running trigger one through for entire first room, trigger two through four TBD
  
  if (digitalRead(trigger1) == HIGH) //first room trigger via NO/NC switch
  {
    firstRm();
  }
  
  
  if (digitalRead (trigger2) == HIGH)
  {
    room2();
  }
}

  void firstRm ()
  {                               //first room runs lights called out to be red, blue, running lights, strobe lights, and black lights, as well as trigger for chainsaw standalone sequence
  digitalWrite (runLights, LOW);
  delay (2);
  digitalWrite (runLights, HIGH);
  delay (2);
  digitalWrite (runLights, LOW);
  delay (2);
  digitalWrite (runLights, HIGH);
  delay (2);
  digitalWrite (runLights, LOW);
  delay (2);
  digitalWrite (runLights, HIGH);
  delay (2);
  digitalWrite (runLights, LOW);
  delay (2); 
  digitalWrite (runLights, HIGH);
  delay (2);
  digitalWrite (runLights, LOW);
  delay (2);
  digitalWrite (runLights, HIGH);
  delay (2);
  digitalWrite (runLights, LOW);
  delay (2);
  digitalWrite (runLights, HIGH);
  delay (2);
  digitalWrite (runLights, LOW);
  delay (2);
  digitalWrite (runLights, HIGH);
  delay (2);
  digitalWrite (runLights, LOW);
  delay (2);
  digitalWrite (runLights, HIGH);           
  delay (2);
   sawMachine ();
  }
  
  void sawMachine ()//stand alone chain saw machine run program
  {
   digitalWrite (start, HIGH);
   delay (100);
   digitalWrite (start, LOW);
  lightStart ();
  }
  
  void lightStart ()//light sequence in first room run program
  {
    digitalWrite (blue, HIGH);
    delay (2);
    digitalWrite (blue, LOW);
    delay (2);
    digitalWrite (red, HIGH);
    delay (2);
    digitalWrite (red, LOW);
    delay (2);
    digitalWrite (blue, HIGH);
    delay (2);
    digitalWrite (blue, LOW);
    delay (2);
    digitalWrite (red, HIGH);
    delay (2);
    digitalWrite (red, LOW);
    delay (2);
    digitalWrite (black, HIGH);
    delay (2);
    digitalWrite (strobe, HIGH);
    delay (2);
    digitalWrite (black, LOW);
    delay (2);
    digitalWrite (strobe, LOW);
    delay (2);
    twitchingBody();
    room2();
  }
  
 void room2 ()
  {
    
    digitalWrite (soundR2, HIGH);
    delay (500);
    digitalWrite (soundR2, LOW);
    
    digitalWrite (surgicalTube, HIGH);
    delay (2);
    digitalWrite (surgicalTube, LOW);
    delay (2);
    digitalWrite (surgicalTube, HIGH);
    delay (2);
    digitalWrite (surgicalTube, LOW);
    delay (2);
    digitalWrite (surgicalTube, HIGH);
    delay (2);
    digitalWrite (surgicalTube, LOW);
    delay (2);
    digitalWrite (surgicalTube, HIGH);
    delay (2);
    digitalWrite (surgicalTube, LOW);
    delay (2);
    
  }
    
    void twitchingBody ()
    {
      digitalWrite (twitch, HIGH);
      delay (2);
      digitalWrite (twitch, LOW);
      delay (2);
      digitalWrite (twitch, HIGH); 
      delay (2);
      digitalWrite (twitch, LOW);
      delay (2) ;
      digitalWrite (twitch, HIGH);
      delay (2);
      digitalWrite (twitch, LOW);
      delay (2);
      digitalWrite (twitch, HIGH); 
      delay (2);
      digitalWrite (twitch, LOW);
      delay (2); 
  }

where digitalWrite (twitch,HIGH); is I would like to run that HIGH at the same time digitalWrite (surgicalTube,HIGH); but be able to control their delays seperatly!..

Also I know there is an easier way to compile this just not sure how! :slight_smile: any forward links to answers or personal answers would be MUCH appriciated!

What you want is a Finite State Machine. Goggle will help.

But the gist follows. The idea is that you are always looping through the code waiting for a target time to occur (or an event like someone trips a light beam). You then do your thing, set up for the next time (is appropriate) change the state, and leave.

loop() {

  now = millis();

  switch (state) {
    case 1:
      if (now > target) {
        Do something
        target = now + 1000*10
        state = 2
      }
      break

  case 2:
    if (now > target) {
      Do something
      target = now + 1000*30
      state = 3:
    }
    break

  case 3:
    if (now > target) {
      Do something
      target = now + 1000*10
      state = 2
    }
   break;
  }

}

I have a feeling he wants the events to happen simultaneously. Correct?(As in independent of each other, with the exception of the high state at the beginning of the cycle.)

I did not get that from the post, but that could be the case. If two back to back digital writes are not “simultaneous” enough than you will need to look into direct port manipulation. Again Goggle will be your friend.

In the event that a photoelectric eye is tripped i would be having 2 digital writes go HIGH at the same time however that is simple but the problem is how to make them then go Low on different delay times

Maybe better explained this way…

A and B are two cylinders and are actuated by a HIGH current. Both A and B must fire at the same time… A LOW or stopping current brings the cylinders back to origional position. Cylinder A needs to return to LOW in say 500 mil. Sec. And cylinder B needs to return to origional position in 200 mil.sec. . . Thank you very much for the replies

If the time domain is less than a second, then the following should work fine.

DigitalWrite(pinA, HIGH);
DigitalWrite(pinB, HIGH);
delay(200)
digitalWrite(pinB, LOW);
delay(500-200)
digitalWrite(pinA, LOW);

Thank you very Much