SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By tim2985
#198619
Hi

Can anyone help me add a line of code to a project that is ongoing?
I have a relay switch sequence a, ab, a, abc, all off on a relay pcb connected to the Arduino and I need to add a line of code so that when the sequence is started and the relays have finished then I need a 2 second break where if you press the start button to re-initiate another sequence then it's locked out and wont operate.
The existing code is here::
#define out_a 9
#define out_b 8
#define out_c 7
#define out_d 6

#define in1 4

unsigned long delay_ms = millis();
unsigned long seq_delay=50; //delay in ms
unsigned long start_delay=25; //delay ms
unsigned long mode_delay=millis();


byte mode=0;
bool start=false;

void setup() {
Serial.begin(9600);
pinMode(in1,INPUT_PULLUP);
pinMode(out_a,OUTPUT);
pinMode(out_b,OUTPUT);
pinMode(out_c,OUTPUT);
pinMode(out_d,OUTPUT);

digitalWrite(out_a,HIGH);
digitalWrite(out_b,HIGH);
digitalWrite(out_c,HIGH);
digitalWrite(out_d,HIGH);


}

void loop() {

switch (mode){
//ON = LOW
//OFF = HIGH
case 1:
//a on
digitalWrite(out_a,LOW);
break;

case 2:
//a anb b on
digitalWrite(out_a,LOW);
digitalWrite(out_b,LOW);
break;

case 3:
//a on (b off)
digitalWrite(out_a,LOW);
digitalWrite(out_b,HIGH);
break;

case 4:
//a on, b on,
digitalWrite(out_a,LOW);
digitalWrite(out_b,LOW);
break;

case 5:
//a on, b on, c on
digitalWrite(out_a,LOW);
digitalWrite(out_b,LOW);
digitalWrite(out_c,LOW);
break;

case 6:
//then all off
digitalWrite(out_a,HIGH);
digitalWrite(out_b,HIGH);
digitalWrite(out_c,HIGH);
digitalWrite(out_d,HIGH);
mode=0;
start=false;
break;
}

if (start){
if ((millis()-mode_delay)>=seq_delay){
mode_delay=millis();
/*Serial.print("mode ");
Serial.print(mode);
Serial.print("; a=");
Serial.print(!digitalRead(out_a));
Serial.print("; b=");
Serial.print(!digitalRead(out_b));
Serial.print("; c=");
Serial.println(!digitalRead(out_c));
*/
mode++;
}
}

if (!digitalRead(in1)){
if ((millis()-delay_ms)>=start_delay){
start=true;
}
} else {
delay_ms = millis();
}

}
By paulvha
#198642
I have not tested it, but it might help you to get on the right track.
Code: Select all
#define out_a 9
#define out_b 8
#define out_c 7
#define out_d 6

#define in1 4

unsigned long seq_delay=50; //delay in ms in between relay switch
unsigned long mode_delay;

byte mode=0;

void setup() {
    Serial.begin(9600);
    pinMode(in1,INPUT_PULLUP);
    pinMode(out_a,OUTPUT);
    pinMode(out_b,OUTPUT);
    pinMode(out_c,OUTPUT);
    pinMode(out_d,OUTPUT);
    
    digitalWrite(out_a,HIGH);
    digitalWrite(out_b,HIGH);
    digitalWrite(out_c,HIGH);
    digitalWrite(out_d,HIGH);
}

void loop() {

    switch (mode)
    {
        //ON = LOW
        //OFF = HIGH
        
        case 1:
            //a on
            digitalWrite(out_a,LOW);
        break;
    
        case 2:
            //a anb b on
            digitalWrite(out_a,LOW);
            digitalWrite(out_b,LOW);
        break;
    
        case 3:
            //a on (b off)
            digitalWrite(out_a,LOW);
            digitalWrite(out_b,HIGH);
        break;
    
        case 4:                             // ?????? again AB on ????
            //a on, b on,
            digitalWrite(out_a,LOW);
            digitalWrite(out_b,LOW);
        break;
    
        case 5:
            //a on, b on, c on
            digitalWrite(out_a,LOW);
            digitalWrite(out_b,LOW);
            digitalWrite(out_c,LOW);
        break;
    
        case 6:
            //then all off
            digitalWrite(out_a,HIGH);
            digitalWrite(out_b,HIGH);
            digitalWrite(out_c,HIGH);
            digitalWrite(out_d,HIGH);       // ?????? d ???
           
        break;
    }
    
    // wait in between relay mode
    if (mode > 0)
    {
        if ((millis()- mode_delay) >= seq_delay){
            mode_delay=millis();
            
            /*Serial.print("mode ");
            Serial.print(mode);
            Serial.print("; a=");
            Serial.print(!digitalRead(out_a));
            Serial.print("; b=");
            Serial.print(!digitalRead(out_b));
            Serial.print("; c=");
            Serial.println(!digitalRead(out_c));
            */
            
            mode++;
            
            // we have come to the end of sequence
            if(mode == 7)
            {
                // loop for ~2 seconds
                while (millis() - mode_delay < 2000)
                {
                    // if key pressed within the 2 seconds
                    if (! digitalRead(in1))
                    {
                        mode = 1;
                        break;
                    }
                }
                
                // key was not pressed within timeout
                if (mode == 7)
                {
                   Serial.println("loop forever"); 
                   for (;;);
                }
                
            }
                
        }
    }
    else
    {
        // wait for start button pressed to begin
        if (!digitalRead(in1))
        {
            mode_delay=millis();
            mode = 1;
        }
    }
}