Mega2560 and 16 relay board programming

OK, 1st thing... last time i wrote any code, was in '82 on a T.I. computer saved to a cassette, which is to say, I have NO idea how to code. That said, I do have 30+ years of Electronics, Mechanics, Fab, Repair experience, just not code.

I am building a "simple" aquarium lighting arrangement for my 200 gallon tank using 12V low power LED strip lights. so, on the advice of others who have done lighting projects (christmas sequencing etc.), I decided to make this my entry into Arduino and the 21st Century.

I have a Mega2560, an "HID relay" board and 2- "16 relay" boards w/opto isolation that I want to turn on at varying times throughout the day (or night for lunar) I figure I need some sort of timing reference so the Mega knows when to do it's job and the obvious code to make it work.

Where do I start ? I do not care to learn the ins-outs of code right now, just get this running with enough knowledge to change the timing of each relay if need be, and use this beginning code to expand that knowledge as time/projects allow.

I can power the relay board from 12V and the Mega from 5V both from a large computer power supply. all I'm missing is assistance in making it do it's thang.

I downloaded the software and am going to try my best. any help is appreciated.

Hello and welcome,

For keeping time accurately, you need an external Real Time Clock module ( I recommend a module based on the DS3231 RTC ), because the arduino itself is not very accurate for this task.

For switching a relay ON or OFF, you write LOW or HIGH to the corresponding digital output pin like it was a LED, for example:

const uint8_t pinRelay1 = 8;

void setup()
{
  pinMode( pinRelay1, OUTPUT );

  digitalWrite( pinRelay1, LOW ); // turn relay 1 ON (in case of a Active LOW relay which is probably the case for your relay boards)
}

void loop()
{
}

After all you are just switching the LED of the optoisolator :wink:

controlling 32 relays with out learning any real code could be done but you wont find it interesting. The program will be thousands of lines of digitalWrite on and off

Now using arrays, random, for loops, etc you will be able to do some neat stuff in a few hundred lines

you can either do something like Guix posted repeated 32 times (one for each relay) or something like the sketch I posted. Guix is correct in saying that a rtc is going to be your best option so you need to learn a few basics to get that to work

p.s I used delays as im lazy. You will need to learn how to use millis timers

byte relayBank1[] = {24, 25, 26, 27, 28, 29, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,};
byte relayBank2[] = {39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,};


void setup() {
  // put your setup code here, to run once:
  for (byte x = 0; x < 16; x = x + 1) {
    pinMode (relayBank1[x], OUTPUT);
    digitalWrite (relayBank1[x], HIGH);
    pinMode (relayBank2[x], OUTPUT);
    digitalWrite (relayBank2[x], HIGH);
    randomSeed(analogRead(A0));//do not connect A0 to anything
  }

}

void loop() {
  // put your main code here, to run repeatedly:


  for (byte x = 0; x < 8; x = x + 1) { //turn bank one first 8 relays on
    digitalWrite (relayBank1[x], LOW);
  }
  delay(1000);//wait one second
  for (byte x = 0; x < 8; x = x + 1) { //turn bank one first 8 relays off
    digitalWrite (relayBank1[x], HIGH);
  }
  for (byte x = 8; x < 16; x = x + 1) { //turn bank one last 8 relays on
    digitalWrite (relayBank1[x], LOW);
  }
  delay(1000);
  for (byte x = 0; x < 16; x = x + 1) { //turn bank one relays off
    digitalWrite (relayBank1[x], HIGH);
  }
  delay(1000);
  for (byte x = 0; x < 16; x = x + 2) { //turn every other relay on bank one
    digitalWrite (relayBank1[x], LOW);
  }
  delay(1000);
  for (byte x = 0; x < 16; x = x + 1) { //turn bank one relays off
    digitalWrite (relayBank1[x], HIGH);
  }
  delay(1000);

  for (byte x = 0; x < 16; x = x + 2) { //turn every other relay on bank two
    digitalWrite (relayBank2[x], LOW);
  }//set up for next step
  for (byte x = 0; x < 100; x = x + 1) { //not sure what this will look like
    byte randNumber = random(0, 16);//pick a number between 0 and 15
    digitalWrite (relayBank2[randNumber], LOW);//turn that number relay on it may already be on
    delay(250);
    randNumber = random(0, 16);//pick another number and turn that off
    digitalWrite (relayBank2[randNumber], HIGH);
    delay(250);
  }
  for (byte x = 0; x < 16; x = x + 1) { //turn bank two relays off
    digitalWrite (relayBank2[x], HIGH);
  }
  delay(1000);
}

Thanks guys. I started researching rtc shields figuring that's what I needed. Seem pretty cheap and simple to deal with. I'm learning that it all comes down to code.

The basic premise of my project is to turn on more and more leds as the day prgresses, then start turning them back off as the sun goes down in the afternoon, mimicking the sunrise and set.

will the mega be able to keep the optos lit for extended periods every day or should I be using latching relays ? I can't imagine it having any issues, but figured I'd ask.

"controlling 32 relays with out learning any real code could be done but you wont find it interesting. The program will be thousands of lines of digitalWrite on and off"

Hardly. I have a shift register based relay card, this is the Uno shield version and a standalone, daisychainable version I just received cards for, would be easy to manipulate 4 bytes and use shiftOut(), or SPI.transfer(), to update the TPIC6C595 shift register that drives the relay coil current, from 5V or 12V.
When a byte changes, send the 4 bytes of data out:

// time for an update?
digitalWrite (ssPin, LOW);
SPI.transfer(byte0); // or from an array, such as dataArray[0], dataArray[1], dataArray[2], dataArray[3], 
SPI.transfer(byte1); // faster execution if Not done in a for loop.
SPI.transfer(byte2);

SPI.transfer(byte3);

digitalWrite (ssPin, HIGH); // outputs update on this rising edge

Here's a youtube clip driving 8 strings of 120V Christmas LEDs.

desmato:
Thanks guys. I started researching rtc shields figuring that's what I needed. Seem pretty cheap and simple to deal with. I'm learning that it all comes down to code.

The basic premise of my project is to turn on more and more leds as the day prgresses, then start turning them back off as the sun goes down in the afternoon, mimicking the sunrise and set.

will the mega be able to keep the optos lit for extended periods every day or should I be using latching relays ? I can't imagine it having any issues, but figured I'd ask.

The mega shouldn't have any problem. Using a arduino the digital outputs stays set so if you code a digital output as low then it will stay in that state until you write a line telling it to go high and vise versa.

I like your idea (ive had large salt water tanks before) and the code should be easy once you have done a few tests to get a base line for how much light and over what time period you should be able to write a plan then convert that to code.

Trying not to sound like a prick but did you consider using mosfets and fading the led lights in and out with pwm.

The Mosfet and PWM was more than I wanted to deal with. I thought of Pixels w/controler as well, but keep coming back to the strip lights I currently have in use. They can be sectioned into 3 LED segments and all run on nominal current 12V DC..... TOO easy. Dump in 12V and the LEDs turn on, no drivers, current limiting etc. call it "LEDs for dummies" the only drawback is lack of dimming ability, which I am trying to overcome with my idea.

Currently I have 6 strips (39" long ea) on individual lamp timers with wall warts. it "works" but so did the 6' fluorescent 4 tube light I had. the LEDs are cooler temp wise, better wavelength, less power consumtion, WAY smaller and more ergo, etc.

Here's a link to the strips I am using... https://www.superbrightleds.com/moreinfo/led-strip-lights-standard-lengths/led-light-strips-led-tape-light-with-18-smdsft-3-chip-smd-led-5050-with-lc2-connector/1465/ no heatsinks or fans or drivers.

If this all works out smoothly, I want to provide my setup as a DIY kit for others (free plans)

like I said I like your idea my only concern is the clicking sound of a relay is annoying. (solid state don't click but there more expensive).

All projects have to start somewhere so anything that gets you learning the basics of code is a good project.

p.s The arduino is the driver if you ever decide to play with fading.