Need Guidance! 1 button to control 3 different outputs with different timings

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
rrankin8801
 
Posts: 1
Joined: Fri May 24, 2013 8:40 pm

Need Guidance! 1 button to control 3 different outputs with different timings

Post by rrankin8801 »

I just bought a Mega 2560 a week ago and am very new to programming and the arduino code. I have 1 input (button) that once pressed, starts 3 outputs. I am trying to program my board to be able to press one button that then, turns two 12v dc motors on for 1 minute, and at the same time turns a 12v solenoid valve on for 3 seconds every 15 seconds (inside 1 minute). The 3 outputs operate within a minute. My motors and solenoid are connected from a pwm pin on the board to a transistor which switches them on and off. Im just trying to figure out how to program 1 input to set off 3 outputs. I have been staring at code and trying to figure this out for the past week but I cannot find a simple solution. I have several programs wrote but I cant figure out how to get it all to work together. Any sort of help would be greatly appreciated!


So basically:

1.) board is looking for button to be pressed
2.)once pressed 2 12v dc motors start
3.) a 12v solenoid valve turns on for 3 seconds
4.)solenoid turns off for 15 seconds (motors still running)
5.)solenoid turns on for 3 seconds (motors still running)
6.)solenoid turns off for 15 seconds (motors still running)
7.)solenoid turns on for 3 seconds (motors still running)
8.)solenoid turns off for 15seconds (motors still running)
9.)solenoid turns on for 3 seconds (motors still running) (at this point the program is running for 57 seconds)
10.)solenoid turns off for 3 seconds (motor still running) (I say its off for "3" seconds because after 3 seconds the loop is over)
11.)Once 1 minute is reached the dc motors and solenoid are stopped.
12.)the board is looking for button to be pressed
13.) .....etc.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Need Guidance! 1 button to control 3 different outputs with different timings

Post by adafruit_support_mike »

Well, you've clearly stated the sequence of operations you need, so that won't be a problem.

The brute force way to execute those steps would look like so:

Code: Select all

void loop () {
    if (buttonWasPressed()) {
        turnOnMotors();

        turnOnSolenoid();
        delay( 3000 );      //  keep the solenoid on for 3s
        turnOffSolenoid();
        delay( 15000 );     //  keep the solenoid off for 15s

        turnOnSolenoid();
        delay( 3000 );      //  keep the solenoid on for 3s
        turnOffSolenoid();
        delay( 15000 );     //  keep the solenoid off for 15s

        turnOnSolenoid();
        delay( 3000 );      //  keep the solenoid on for 3s
        turnOffSolenoid();
        delay( 15000 );     //  keep the solenoid off for 15s

        turnOnSolenoid();
        delay( 3000 );      //  keep the solenoid on for 3s
        turnOffSolenoid();
        delay( 3000 );      //  wait 3s to reach the end of 60s

        turnOffSolenoid();
        turnOffMotors();
    }
}
A more fine-grained approach would be to keep a timer and a few status flags, and decide what should happen moment by moement:

Code: Select all

boolean RUNNING = false;
boolean MOTORS_ON = false;
boolean SOLENOID_ON = false;
int START_TIME = 0;

void loop () {
    int runtime = 0;

    if ((! RUNNING) && (buttonWasPressed())) {
        RUNNING = true;
        START_TIME = millis();        //  NOTE:  overflows after about 50 days
    }
    
    if (RUNNING) {
        runtime = millis() - START_TIME;
        
        if (motorsShouldStartAt( runtime )) {
            turnOnMotors();
        }
        if (motorsShouldStopAt( runtime )) {
            turnOffMotors();
        }

        if (solenoidShouldStartAt( runtime )) {
            turnOnSolenoid();
        }
        if (solenoidShouldStopAt( runtime )) {
            turnOffSolenoid();
        }
        
        if (60000 >= runtime) {
            RUNNING = false;
        }
    }
    delay( 1 );
}


boolean motorsShouldStartAt (int time) {
    if ((! MOTORS_ON) && (time < 10)) {
        return( true );
    }
    return( false );
}

void turnMotorsOn () {
    MOTORS_ON = true;
    //  etc
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”