Coding question **NEWBIE**

Ok I am sure you guys get a ton of these newbie questions but I am working on project that I need some help with. First off… I am very new to this and still dont fully grasp the concept of coding. I am trying to learn but it is slow process.

Here is my project:

I have a display that will be running 6 different LED projectors inside of a display case. Each projector will be using the supplied power adapter but I need to sequence the projectors to turn on individually for a undetermined amount of time by pressing a momentary switch. This is where the Arduino comes in. I will be using 8 channel relay module to interrupt the power going to each projector. And will use the Arduino to control the relay module.

Now I understand the concept of a simple relay. But my question comes with the coding… I think I have found the basic setup code to run the relay module indefinably but I need to know what coding is needed to enable the me to put a switch into circuit that will start a loop. And when that loop is finished the loop will stop and wait for another button press.

I have (I think) attached the code I am working with. I found it surfing the net and I think I have modified it to work with the relay module.

Please can anyone help a newbie out…

Thanks,
Derek

_6Chan_relay_controller_w_pushbutton.ino (2.57 KB)

I have a display that will be running 6 different LED projectors

How does a display run a projector?

but I need to know what coding is needed to enable the me to put a switch into circuit that will start a loop.

   if(digitalRead(switchPin) == LOW) // Assumes pin is in INPUT_PULLUP mode
   {
      startALoop();
   }
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);
  digitalWrite(Relay_5, RELAY_OFF);
  digitalWrite(Relay_6, RELAY_OFF);

Pins default to INPUT. When you write to an INPUT pin, you turn the internal pullup resistor on or off. That is NOT what you want to be doing.

  delay(4000); //Check that all relays are inactive at Reset

That is NOT what that code does.

//---( Turn all 4 relays ON in sequence)---

So, why are there 6 calls to digitalWrite()?

Why is it so hard to simply post your code?

/* YourDuino Example: Relay Control 1.10
  Handles "Relay is active-low" to assure
  no relay activation from reset until
  application is ready.
   terry@yourduino.com */

/*-----( Import needed libraries )-----*/
/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
#define Relay_1  2  // Arduino Digital I/O pin number
#define Relay_2  3
#define Relay_3  4
#define Relay_4  5
#define Relay_5  6
#define Relay_6  7

void setup()   /****** SETUP: RUNS ONCE ******/
{
//-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);
  digitalWrite(Relay_5, RELAY_OFF);
  digitalWrite(Relay_6, RELAY_OFF);  
  
//---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);    
  pinMode(Relay_5, OUTPUT);
  pinMode(Relay_6, OUTPUT);
  delay(4000); //Check that all relays are inactive at Reset

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
//---( Turn all 4 relays ON in sequence)---
  digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
  delay(1000);              // wait for a second
  digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
  delay(1000);              // wait for a second  
  digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
  delay(1000);              // wait for a second
  digitalWrite(Relay_4, RELAY_ON);// set the Relay ON
  delay(1000);              // wait for a second
  digitalWrite(Relay_5, RELAY_ON);// set the Relay ON
  delay(1000);              // wait for a second
  digitalWrite(Relay_6, RELAY_ON);// set the Relay ON
  delay(1000);              // wait see all relays ON
  
//---( Turn all 4 relays OFF in sequence)---  
  digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
  delay(1000);              // wait for a second
  digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
  delay(1000);              // wait for a second  
  digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
  delay(1000);              // wait for a second
  digitalWrite(Relay_4, RELAY_OFF);// set the Relay OFF
  delay(1000);              // wait for a second
  digitalWrite(Relay_5, RELAY_OFF);// set the Relay OFF
  delay(1000);              // wait for a second
  digitalWrite(Relay_6, RELAY_OFF);// set the Relay OFF
  delay(4000);              // wait see all relays OFF  
  

}//--(end main loop )---



//*********( THE END )***********

A detail:
In setup, pinMode should come before digitalWrite.
--Michael

I recommend reading the Arduino button tutorial. The code is included in the Arduino IDE under Examples=>Digital=>Button. Get just that code working on a breadboard, but change the button pin from 2 to a pin that you are not using for a relay.

If your button works with this code then add the variables from the button example to your program and add the call in setup() to set the button pin mode.

Then modify your loop() function to watch for the button press. Your existing relay code will go after the button example line:

 if (buttonState == HIGH) {

BTW, you might also read the Debounce tutorial, but you probably do not need to debounce the button due to all the delay time in the relay code.