Noob Needs Help with Unsigned Long


I am trying to use Unsigned Long to be able to turn on 5 different zone arrays of LED's in sequence with the first Zone coming on and staying on until all 5 zones have lit up. I tried using Delay function but that will not work for my application. I need Zone 1 to come on, then 1.2 seconds later, Zone 2, then 1.2 seconds later Zone 3, etc, through Zone 5. Then after all 5 zones are ON for 20 seconds, turn ALL off. Using Arduino Uno with PWM outputs driving relays for LED arrays. I can get it to drive arrays in sequence using Delay but it does not work properly. Please check my code...I keep getting 'else without a previous if' error in void loop( ). I only have two arrays connected right now for testing but I can't move forward without getting rid of this error. Hopefully it is only a syntax error but I can't figure it out. Please help.

AHC 5 Zone LED Relay Circuit With Reset Button

Turns on and off 5 Zone LED Arrays and pin 13, when pressing a pushbutton attached to pin 2.

The circuit:

  • LED attached from pin 13 to ground
  • pushbutton attached to pin 2 from +5V
  • 10K resistor attached to pin 2 from switch ground

// Constants used here to set pin numbers:
const int buttonPin = 2; // Pushbutton Input Pin
const int ledPin = 13; // Built In LED pin
const int relayPin1 = 3; // Output Pin for Zone 1
const int relayPin2 = 5; // Output Pin for Zone 2
const int relayPin3 = 6; // Output Pin for Zone 3
const int relayPin4 = 9; // Output Pin for Zone 4
const int relayPin5 = 10; // Output Pin for Zone 5
const unsigned long zone1interval = 10800; //Zone 1 On Time Interval
const unsigned long zone2interval = 9600; //Zone 2 On Time Interval
const unsigned long zone3interval = 8400; //Zone 3 On Time Interval
const unsigned long zone4interval = 7200; //Zone 4 On Time Interval
const unsigned long zone5interval = 6000; //Zone 5 On Time Interval

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
unsigned long push_button; //user Pushes Button
unsigned long zone1time;
unsigned long zone2time;
unsigned long zone3time;
unsigned long zone4time;
unsigned long zone5time;

void setup() {
// initialize the LED pins as Zone Outputs:
//Pin 3 is Zone 1
//Pin 5 is Zone 2
//Pin 6 is Zone 3
//Pin 9 is Zone 4
//Pin 10 is Zone 5

pinMode(ledPin, OUTPUT);
pinMode(relayPin1, OUTPUT); //Zone 1
pinMode(relayPin2, OUTPUT); //Zone 2
pinMode(relayPin3, OUTPUT); //Zone 3
pinMode(relayPin4, OUTPUT); //Zone 4
pinMode(relayPin5, OUTPUT); //Zone 5
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
zone1time=millis(); //Sets Zone Intervals to milliseconds
} //End Of Setup

void loop()
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH);
// turn LED's on by Zone
digitalWrite(ledPin, HIGH);
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, HIGH);
digitalWrite(relayPin3, HIGH);
digitalWrite(relayPin4, HIGH);
digitalWrite(relayPin5, HIGH);

digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin4, LOW);
digitalWrite(relayPin5, LOW);
} //End of Loop

You need to walk before you run.
You must learn a little C++.
Read this, then ask yourself if you are following the syntax.


If statements shouldn't have semicolons. And if you want more than one line to be included in the block you need to add curly braces around the block. Do a google search for if statements in C++ to see some examples.

thanks for all your input. I removed the semi after my IF statement and added to curly brackets to my \action1 to digitalWrite my relayPin1 and that I need to figure out how to setup the time delay to keep the relay active for 10800 ms.

i will be chipping away at this later tonight if anybody has insomnia and wants to help me have a hard deadline for this project tomorrow and I'm soooooo close I can taste it. i still have to resolder the LED arrays once I get this timing thing worked out.

on my way out for pizza and beer...i'll start the coffee at 11....hehehe...Thanks for your help and interest!!

deadline for this project tomorrow

Oh to be young again :slight_smile:


I would start with a state variable that keep track of how many zones are lit:

static int zones = 0;

Then a 'switch' statement to execute code for each state:

switch (zones) {
case 0:  // No zones lit
    if (buttonPressed) {
        digitalWrite(Zone1Pin, HIGH); // Light zone 1
        zoneLitStartTime = millis();
       zones = 1;

case 1: // Zone 1 is lit
      if (millis() - zoneLitStartTime > 1200)  {  // After 1.2 seconds
          digitalWrite(Zone2Pin, HIGH); // Light zone 2
          zoneLitStartTime = millis();
         zones = 2;


@ rayjamz:

What up?? Still working on this project? If you are maybe I could assist you?

I think in your initialisation part, you could reduce the size of the code by using const byte or const uint8_t (short for unsigned integer 8 bit ) or you could even use #define to make it a real constant.

I saw that you have a lot of repetition using the name relayPin from 1 to 5, two comment there...
1 ) why don't you used a better name such as Zone1 - Zone5.
2 ) if you are up for it try using array such as zone = { ...../*fill in the value */.... };

I saw in your code you have made some attempt to record and have some timing sequence in it..

however if I understand your Project requirement correctly zone1 light up, wait 1.2s then zone2 and so on so forth until zone 5. When all 5 zone light up, wait for 2s and finally all 5 zone dim out.

If I'm not mistaken, the sequence only happen when the buttonPin is press right?

Now we could try to make it using FSM....
such as the ASCII art that I would try to draw below

(PB) (1.2s) (1.2s) (1.2s) (1.2s) (2s)
Zone0--------> Zone1 ------> Zone2------>Zone3----->Zone4----->Zone5-----
^ |

Zone 0 mean all Zone is turn off or easier to understand ( reset )
I also assume that maybe while at any zone, if PB is release, state when to Zone0 again
and lets assume that even though the PB is still press, once the sequence is done, it will not light up again until PB have been re press.

for me, Anything that require a mechanical input, I would like to used a debounce/state change detection in my program....
Like the lazy person that I am, I used a library call bounce2 Bounce2 Library in github made by thomasfredericks.. I been using his library for ages and feel its really taken the load of my shoulder.
you could either download from github it self or you could install the library from Arduino IDE itself
(hope you know how to install the library )

Thanks for all your advice and input guys! i am still learning how to code and it has been extremely frustrating. I will play with your examples and re-post my code once it is complete. Thanks again for your good advice and programming ideas. I know there are many ways to skin a cat...I just need to know a few :slight_smile:

I think in your initialisation part, you could reduce the size of the code by using const byte or const uint8_t (short for unsigned integer 8 bit ) or you could even use #define to make it a real constant.

It's never a good idea to use "#define" for pin allocations. 'const byte' was a much better recommendation.

There's a good discussion of this topic here:- Const vs #define – When do you use them and why?