Trigger a loop

Hello, This is literally the first code I have ever written. I have an arduino uno 3. I have a 4 channel relay module, of which i will be using 3 of the relays. They are on pins 13, 12 and 8. I have written the code so that these pins are activated at certain times, and shut off at others. You will notice that pins 13 and 8 only change states a few times, while pin 12 goes on and off at intervals. I am hoping at least this part of the code is correct. (I receive my arduino sometime this week) If I have a pushbutton trigger to activate this, how do i code it so that this loop only plays through once triggered? I need the code to ignore all other inputs/ triggers until the loop has been completed ( total of around three minutes) then wait for the next input.

CODE:

define FOGGER 13

define MISTERAUDIO 12

define ECOSHUTOFF 8

void setup() { pinMode(FOGGER, OUTPUT) ; digitalWrite(FOGGER, LOW); pinMode(MISTERAUDIO, OUTPUT) ; digitalWrite(MISTERAUDIO, LOW); pinMode(ECOSHUTOFF, OUTPUT) ; digitalWrite(ECOSHUTOFF, LOW); } // End Setup

//// THIS NEEDS TO BE TRIGGERED AND IGNORE ALL OTHER TRIGGERS UNTILL LOOP IS COMPLETED\\

void loop() { digitalWrite(MISTERAUDIO, HIGH); // AUDIO is triggered delay(250); // Delay 1/4 second digitalWrite(MISTERAUDIO, LOW); // AUDIO trigger shut off, music playing at this point delay(1000); // Delay 1 sec. digitalWrite(ECOSHUTOFF, HIGH); // Ecozone lights are switched off digitalWrite(FOGGER, HIGH); // Fogger is switched on delay(3000); // Delay 3 sec. digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on delay(6000); digitalWrite(MISTERAUDIO, LOW); delay(8000); digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on delay(5000); digitalWrite(MISTERAUDIO, LOW); delay(4000); digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on delay(3000); digitalWrite(MISTERAUDIO, LOW); delay(7000); digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on delay(8000); digitalWrite(MISTERAUDIO, LOW); delay(3000); digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on delay(6000); digitalWrite(MISTERAUDIO, LOW); delay(9000); digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on delay(4000); digitalWrite(MISTERAUDIO, LOW); delay(9000); digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on delay(5000); digitalWrite(MISTERAUDIO, LOW); delay(9000); digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on delay(3000); digitalWrite(MISTERAUDIO, LOW); delay(8000); digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on delay(4000); digitalWrite(MISTERAUDIO, LOW); delay(3000); digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on delay(4000); digitalWrite(MISTERAUDIO, LOW); delay(9000); digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on delay(3000); digitalWrite(MISTERAUDIO, LOW); delay(10000); digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on delay(4000); digitalWrite(MISTERAUDIO, LOW); delay(40000); digitalWrite(FOGGER, LOW); // Fogger is switched off delay(3000); digitalWrite(ECOSHUTOFF, LOW); // Ecozone is switched back on

}

You've not told us, nor defined in your sketch, what pin this trigger is connected to. Let's assume it's on pin 9, you've defined it as an input and you have a pull-down resistor also on pin 9.

int trigger =9;  //or #DEFINE trigger 9
pinMode(trigger, INPUT);

Then simply look to see if the button is pressed each time round loop()

void loop(){
if (digitalRead(trigger)==HIGH){  //is button pressed? Yes, then
//do rest of your code
 //otherwise go directly here (do not pass Go, do not collect £200)
}  //End of if()
} //End of loop()

In cases where the button could be read multiple times you would need to debounce the button, but as your loop takes around three minutes to complete, this won't be necessary. You can't read the button whilst the delays() are being run, but you will be able to, once they've finished. This appears to be what you want. NB 1) By convention, variables are all lower case or 'camelCase'. 2) When posting code to the forum, please use code tags (the # button above the smileys)

CAN YOU explain PROBLEM STATEMENT PROPERLY. Without your code . Let us know what you are trying to do. what you want to trigger

When you find yourself writing the same lines over and over it is time to learn how to use arrays. The whole of that loop function can be written in about four lines. Then use an if statement to see if the trigger has happened and put that code to follow the if statement if it is true.

Sorry guys. I have taken your advice and applied it best i can, here is what I have. I do not receive my board until friday. So the same pins that would be controlling my relays, also can act as inputs?

This code is supposed to active 3 relays upon trigger and keep them active for three minutes. One of these three relays is supposed to alternate on and off during that three minute period. After three minutes, everything returns to original state and waits for next input trigger. This input trigger is to be ignored during a cycle until the cycle/loop is complete.

I do wish to educate myself on arrays, can you refer to me a good source that would apply to my application? Thank you for the help! I have always been interest in programming, but the syntax and structure has always deterred me.

I will attempt to use this Code tag properly.

Does this appear better ( minus using an array at this point :/ )

#define FOGGER 13
#define MISTERAUDIO 12
#define ECOSHUTOFF 8 

///// THIS NEEDS TO BE TRIGGERED AND IGNORE ALL OTHER TRIGGERS UNTILL LOOP IS COMPLETED\\\\
void setup() {
  pinMode(FOGGER, OUTPUT) ;
  digitalWrite(FOGGER, LOW);
  pinMode(MISTERAUDIO, OUTPUT) ;
  digitalWrite(MISTERAUDIO, LOW);
  pinMode(ECOSHUTOFF, OUTPUT) ;
  digitalWrite(ECOSHUTOFF, LOW);
  int trigger =9;  //or #DEFINE trigger 9
  pinMode(trigger, INPUT);
} // End Setup

void loop()  { 
  if (digitalRead(trigger)==HIGH){
  digitalWrite(MISTERAUDIO, HIGH); // AUDIO is triggered
  delay(250); // Delay 1/4 second
  digitalWrite(MISTERAUDIO, LOW); // AUDIO trigger shut off, music playing at this point
  delay(1000); // Delay 1 sec.
  digitalWrite(ECOSHUTOFF, HIGH); // Ecozone lights are switched off
  digitalWrite(FOGGER, HIGH); // Fogger is switched on
  delay(3000); // Delay 3 sec.
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(6000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(8000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(3000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(6000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(3000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(7000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(5000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(6000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(4000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(11000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(4000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(9000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(5000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(9000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(3000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(8000);  
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(4000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(3000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(4000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(9000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(3000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(10000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(4000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(40000);
  digitalWrite(FOGGER, LOW); // Fogger is switched off
  delay(3000);
  digitalWrite(ECOSHUTOFF, LOW); // Ecozone is switched back on
  }  //End of if()
} //End of loop()
}

I forgot to add, the trigger may be pushed multiple times during the 3 minute sequence. By default, Arduino will ignore this?

#define FOGGER 13
#define MISTERAUDIO 12
#define ECOSHUTOFF 8 
#define TRIGGER 9
void setup() {
  pinMode(FOGGER, OUTPUT) ;
  digitalWrite(FOGGER, LOW);
  pinMode(MISTERAUDIO, OUTPUT) ;
  digitalWrite(MISTERAUDIO, LOW);
  pinMode(ECOSHUTOFF, OUTPUT) ;
  digitalWrite(ECOSHUTOFF, LOW);
  int trigger =9;  //or #DEFINE trigger 9
  pinMode(trigger, INPUT);
} // End Setup

void loop()  {
  if (digitalRead(TRIGGER)); {
  digitalWrite(MISTERAUDIO, HIGH); // AUDIO is triggered
  delay(250); // Delay 1/4 second
  digitalWrite(MISTERAUDIO, LOW); // AUDIO trigger shut off, music playing at this point
  delay(1000); // Delay 1 sec.
  digitalWrite(ECOSHUTOFF, HIGH); // Ecozone lights are switched off
  digitalWrite(FOGGER, HIGH); // Fogger is switched on
  delay(3000); // Delay 3 sec.
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(6000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(8000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(3000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(6000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(3000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(7000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(5000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(6000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(4000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(11000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(4000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(9000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(5000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(9000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(3000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(8000);  
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(4000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(3000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(4000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(9000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(3000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(10000);
  digitalWrite(MISTERAUDIO, HIGH); // Mister is switched on
  delay(4000);
  digitalWrite(MISTERAUDIO, LOW);
  delay(40000);
  digitalWrite(FOGGER, LOW); // Fogger is switched off
  delay(3000);
  digitalWrite(ECOSHUTOFF, LOW); // Ecozone is switched back on
  }  //End of if()
} //End of loop()

Better?

Since you have multiple things happening, you're not going to be able to do it that way. While you are waiting to turn off one relay, you need to turn on another one, and you can't do anything else during the delay function. You need to use the BlinkWithoutDelay style of programming. My video has helped some people understand it a little better.

https://www.youtube.com/watch?v=3VZgaJBrZD8 - explaining how to manage the state of multiple things which need to work independently.

Secondly... why are you attempting something so complicated for "literally the first code I have ever written" ???! That's like using the Moonlight Sonata to learn piano.

Start with twinkle twinkle... when you understand how that works, then try more difficult songs. I would bet probably half of the questions on this forum could be eliminated if people simply did the examples. You need to do the examples. At the very least, read the examples and try to understand the code. Doing what you're doing is poor tactics. You are attempting something and you don't possess the tools to succeed, and you should have known that.

You are playing around with high-power electrical devices (stage lighting and foggers?) and you don't want to make any mistakes, which could result in injury to yourself, injury to guests, or failure/damage of the devices. Please be careful.

For learning general programming concepts, I LOVE THIS BOOK: http://www.rakuten.com/prod/beginning-programming-for-dummies/202837217.html?listingId=331542328&scid=pla_google_SynergyDataInc&adid=17260&gclid=CjgKEAjwkpacBRCNlprWw-u-nBwSJACwHiw-ObU5NNRQi_ZK3eP9FYHM4tGZB4MQlDPPPf_OEOSv_fD_BwE

And, I would totally loan you my copy, which I have done many times in the past, and I always get it back. It teaches programming with an obscure language that is similar enough to modern languages to teach the concepts correctly, but different enough that you learn those concepts rather than the language itself. Learning languages is something you do a lot as a programmer, but learning concepts is something you do once, hopefully. So, if you want to learn about arrays, get that book - it will work you up to arrays after learning the fundamentals below that concept.

tyterh: I forgot to add, the trigger may be pushed multiple times during the 3 minute sequence. By default, Arduino will ignore this?

Yes, once your loop function starts, the Arduino can't respond to anything because you've programmed an uninterruptable sequence using delay. Read my answer and watch my video to understand the correct way of doing it, then you will be able to respond to buttons during the sequence.

tyterh: I forgot to add, the trigger may be pushed multiple times during the 3 minute sequence. By default, Arduino will ignore this?

aside from all the great advice you already have, you will want to particularly look into if & else if statements.

it will allow you to hold a state for an amount of time, without a delay.

Pseudo Code:

if Time is Less Than 1 second
  SMOKE
else if Time is less than 2 seconds
  FIRE
else if Time is less than 3 seconds
  SPARKS
end of If/Else If loop

every time the program runs through the loop, it tests for time and does SMOKE, FIRE or SPARKS. But it will run continuously without any delays.

Look into that structure while you are learning

Lol, I read that as “SHARKS” and that would be really cool!

jasmine2501: Lol, I read that as "SHARKS" and that would be really cool!

Damn. I've now got the theme from 'Jaws' as an earworm! :0

Better?

No.

  if (digitalRead(TRIGGER)); {

digitalRead() does not return a boolean. Do not treat the return value as a boolean. Make it CLEAR what value you expect to take action on.

The action you are taking (;) is a no-op. Is that really ALL that you want to do if the switch is pressed? It will be an eon before you get a chance to press the switch again.

Why this line:

int trigger =9;  //or #DEFINE trigger 9

when you've already defined trigger (actually TRIGGER )?