How to control digital out with digital in?

Hello all,

I have some experience with Arduino projects following the guides I have successfully made projects with leds using pwm pins etc. It's always been copy and paste the code hehehe, I have tried to fiddle with code It's fun but the programme part of it is the hard part and dread it always.
I don't know where to start on my next project. I urgently need help and hope to make first attempt to get this going tomorrow! Haha

I hope I can get some assistance with code for the following scenario. Don't even know what to start with.

I have a Arduino Uno and I want to
Control a pair of MOSFET boards. Which will control solenoids

I will have 2x MOSFET boards connected to two digital out pins.
MOSFET board 1 will be connected to solenoid 1
MOSFET board 2 will connect to solenoid 2

I need the code to cycle turning them on and off in order.
Eg.
Step 1, turn MOSFET board 1 on
Step 2, turn MOSFET board 1 off
Step 3, turn MOSFET board 2 on
Step 4, turn MOSFET board 2 off
Step 5, Goto step 1

Here's the thing. I have a special signal going into the Arduino digital in and need it to cycle the code as
Example:
*Arduino digital In, when the signal is high it would complete step 1 turning on solenoid 1.
*When the signal goes low, it will activate step 2 turning off the solenoid 1. Basically waiting for next step
*If and when the signal goes on again, it will activate step 3 turning solenoid 2 on.
*Then when goes off it will Goto step 4. Turning off the solenoid 2

The next time the signal goes high, it needs to turn on solenoid 1 again.. and so on and so on.

Anyways hope to get some comments or even something to start with..

Cheers everyone
Regards
S.

You need two simple things - code that detects when the state of the input pin changes and a variable to keep track of where you are in the process.

Something like this pseudo code

previousInputState = inputState;
inputState = digitalRead(inputPin);
if (inputState == HIGH and previousInputState == LOW) {
   if (actionStep == 1) {
      turn board 1 on
      actionStep = 2;
   }
   else if (actionStep == 3) {
     turn board 2 on
     actionStep = 4;
    }
}
if (inputState == LOW and previousInputState == HIGH) {
   if (actionStep == 2);
      turn board 1 off
      actionStep = 3;
   // etc
   // etc
}

...R

That is what would be referred to as a state machine. I would do it using switch:case commands.
I leave it to you to flesh out the rest.

// pre-setup pin assignments, variable declarations, etc.

void setup(){
pinMode (stepPin, INPUT_PULLUP);
// other pinMode assignments
// Turn off MOSFET outputs
}

void loop(){
  if (digitalRead(stepPin) == 0){  // button connects pin to Gnd when pressed
  step = step +1;
    if (step == 4){
    step = 0;
    }
  // maybe a delay(50) for button debouncing
  }

switch (step){
case 0:
//turn on MOSFET1 output pin
break;
case 1:
// turn off MOSFET1 output pin
break;
case 2:
// turn on MOSFET2 output pin
break;
case 3:
// turn off MOSFET2 output pin
break;
} // end of cases
} // end of loop

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.