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
/*
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]