Interrupting program by pressing buttons

Hello guys,

first of all i'd like to tell you that i'm a total beginner when it comes to programming in general. I have some basic understanding of electronics but that's about it. Arduino has always fascinated me though.

My father and i are building a model railway. We would like to control the lights by using a Arduino. Our idea is as follows:

  • Main lights of the room stay on for about 10 minutes which simulates daytime.

  • When the 10 minutes are over, it's time to simulate night mode, so the relay kicks in and turns off the main lights of the room.

  • A couple of seconds later all the lights of the model railway should light up by using seperate relays, BUT (and here is the tricky part) it should happen in stages. So let's say our model railway is divided in 8 'modules' which should randomly switch on every 1 second. So that it simulates a realistic way of city lighting during the evening instead of just turning off the main light, and switching on all the model railway lights all together.

  • All the lights stay on for a good 5 minutes until the whole loop starts again and it becomes day time again.

Alright, i've gotten this far, by using delays in between the switching of the seperate relays. It worked well.

But, i'd like to have 3 buttons on the control panel side of the railway. One for never ending daytime (work light), one for never ending nighttime, and the last one to resume the program of 10 minutes daytime, 5 minutes nighttime.

I tried putting the button in the beginning of the loop, but as expected they don't work anymore once the loop continues and is hold up by the delays which slow everything down.

Now, how do i do this? Here's the original (working) code of the first test without the buttons.

int Relay1 = 12;
int Relay2 = 11;
int Relay3 = 10;
int Relay4 = 9;
int Relay5 = 8;
int Relay6 = 7;

int Mainlight = 2;

// the setup routine runs once when you press reset:

void setup() {
// initialize the digital pin as an output.
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
pinMode(Relay5, OUTPUT);
pinMode(Relay6, OUTPUT);

pinMode(Mainlight, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(Mainlight, LOW);
delay(2000);

digitalWrite(Relay5, HIGH);
delay(1000);
digitalWrite(Relay2, HIGH);
delay(1000);
digitalWrite(Relay4, HIGH);
delay(1000);
digitalWrite(Relay1, HIGH);
delay(1000);
digitalWrite(Relay6, HIGH);
delay(1000);
digitalWrite(Relay3, HIGH);
delay(5000);

digitalWrite(Relay5, LOW);
digitalWrite(Relay2, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay1, LOW);
digitalWrite(Relay6, LOW);
digitalWrite(Relay3, LOW);

digitalWrite(Mainlight, HIGH);
delay(5000);

}

Thanks in advance! It would help us a lot!

First thing to note is use the code tag </> rather than quote tags.

I would dim the lights rather than switch them off for night. This would probably be an enhancement at a later time.

Another thing to look at is Demonstration code for several things at the same time - Project Guidance - Arduino Forum doing more than one thing at a time.

Weedpharma

Forget the delays, yeah they work now but your arduino is doings delays 99.99% of the time

I think you should take a look at:
http://playground.arduino.cc/Code/StopWatchClass

This is pretty easy to understand because of the analogy with real world stopwatches.
Something like: Get a stopwatch, start it, watch it and when it reaches a certain value, stop or reset it.

The main ingredients for your code:

  • stopwatch library, 8 stopwatches
  • some if-else statements
  • debounce library for your buttons: bounce2 for example

In the loop :

  • you check your buttons
  • check your stopwatches, if they have reached a certain value, do something (switch lights, reset the stopwatch, ...)

Attached: Two example files for stopwatch and debounce

Stopwatch_example.ino (1021 Bytes)

Bounce2_example.ino (1.35 KB)

Over, Captain Over. Over and over. Learn to use arrays to avoid repetition of code:

int Relays[] = {11,8,10,7,12,9};

int Mainlight = 2;

// the setup routine runs once when you press reset:

void setup() {
  // initialize the digital pin as an output.
for (i=0; i<6; i++)
  pinMode(Relay[i], OUTPUT);
 
  pinMode(Mainlight, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(Mainlight, LOW); 
  delay(2000);             
 
for (i=0; i<6; i++)
  { 
  digitalWrite(Relay[i], HIGH);   
  delay(1000);             
  }
  delay(5000);

for (i=0; i<6; i++)
 {
  digitalWrite(Relay[i], LOW);               
  digitalWrite(Mainlight, HIGH);
  }
  delay(5000);
 
}

Have a look at how millis() is used to manage timing in the demo Several Things at a Time. You could imagine extending the periods for the LED flashes to represent the timing of your lighting.

Also have a look at Planning and Implementing a Program - especially the planning part. If you make a clear statement of the steps that must happen that is 70% or 80% of the program done.

Both examples include buttons that can operate at any time. If you use the delay() function that can't happen because the Arduino can do nothing during a delay().

...R