Pages: [1]   Go Down
Author Topic: Relay Chase Sequencer  (Read 468 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm working on a project where I would like to sequence 12 relays and control the speed of said relays using a pot. I have a start button that starts the sequence when pressed and stops the sequence and holds the current relay when depressed.

The included code seems to work so far except I cant figure out a few things. I would like to have a reset button that when pressed will reset to Relay #1 or later on light a standby LED my current reset just takes all outputs to LOW but doesn't reset the sequence.

The other thing I haven't been able to figure out is how to keep last relay ON for a predetermined time before it is switched OFF in the sequence.

i.e- sequence starts with Start Button pushed and starts to switch relays ON then OFF through the sequence . Once the relay has been activated I would like it to stay ON 20ms having the relay OFF state trail the ON state.

Any suggestions.

Im very much new to any programming and this example is my first shot at coding anything. I do appreciate any suggestion on what I have coded so far as well as any ways to clean it up to work better.

Thanks
-M
Code:
/*
Device changes speed of relay chase using pot to change speed.
Chase activated when Start Button (startPin) is pressed and continues with chase till Start Button is released.
Chase reset button (relayRest) sets all relays to off and makes current relay #1 (relayPin 1)


*/

byte relayPin[]= {1,2,3,4,5,6,7,8,9,10,11,12}; // Create array for cue pins
int relayDelay; // delay between relays
int direction = 1; // set relay direction
int currentRelay = 0;
unsigned long changeTime;
int startPin = 2;  //Sets Start button pin
int startState = 0;
int relayRest= 3; //Sets Relay reset button pin
int cueState= 0;
int potPin = 1;   //select the input pin for potentiometer


void setup() {
for (int x=0; x<12; x++) { // set all relay pins to output
pinMode(relayPin[x], OUTPUT); }
changeTime = millis();
pinMode(startPin, INPUT);
pinMode(potPin, INPUT);
pinMode(relayRest, INPUT);
}


void loop() {
  relayDelay = analogRead(potPin); //read value of potPin
  int state = digitalRead(startPin);
// check if button is pushed and continue to changeRelay
if (state == HIGH && (millis() - changeTime) > relayDelay) {
 
// if it has been relayDelay ms since last change

changeRelay();
changeTime = millis();
}
int state2 = digitalRead(relayRest);
if (state2 == HIGH) {
for (int x=0; x<12; x++) {          // turn off all relay's
digitalWrite(relayPin[x], LOW);

}}}


void changeRelay() {
for (int x=0; x<12; x++) {          // turn off all relays's
digitalWrite(relayPin[x], LOW);

}
digitalWrite(relayPin[currentRelay], HIGH);   // turn on the current relay

currentRelay += direction;                  // increment by the direction value

// change direction if we reach the end
if (currentRelay == 0) {direction = 1;}

if (currentRelay == 12) {direction = -12;}

}/code]
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With a community as large as this, there has to been someone that might be able to help.

-M

Logged

Montreal
Offline Offline
Edison Member
*
Karma: 23
Posts: 2487
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would like to have a reset button that when pressed will reset to Relay #1 or later on light a standby LED my current reset just takes all outputs to LOW but doesn't reset the sequence.
Code:
if (state2 == HIGH) {
for (int x=0; x<12; x++) {          // turn off all relay's
digitalWrite(relayPin[x], LOW);
currentRelay = 0;                        // <<<--------- reset a sequence
}}
Quote
The other thing I haven't been able to figure out is how to keep last relay ON for a predetermined time before it is switched OFF in the sequence.
Code:

void changeRelay() {
/* REMOVE
for (int x=0; x<12; x++) {          // turn off all relays's
digitalWrite(relayPin[x], LOW);

*/
currentRelay += direction;                  // increment by the direction value

// change direction if we reach the end
if (currentRelay == 0) {direction = 1;}

if (currentRelay == 12) {direction = -12;}

digitalWrite(relayPin[currentRelay], HIGH);   // turn on the current relay
delay(20);
digitalWrite(relayPin[currentRelay - 1], LOW);   // turn on the current relay

}

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Magician for your help.  smiley-grin

I will try these changes later when I get home.

-M
Logged

Pages: [1]   Go Up
Jump to: