Remembering Inputs

I am having trouble with wrapping my head around the concept of remembering that an input was once high. What I am trying to do is just turn a DC motor with a NO push button, but I only want to have to push the button once. Thanks for any help in advance.

The button press should set a status variable.
You test that variable and while it is set you power the motor.

If you want a button to then switch the motor off, program it to unset the status variable.

Does this help?

byte
    lastSwitch;

void setup()
{
    //.
    //.
    //.
    pinMode( pinSwitch, INPUT_PULLUP );
    lastSwitch = digitalRead( pinSwitch );
}


void loop()
{
    byte
        currSwitch;

    //read the switch
    currSwitch = digitalRead( pinSwitch );
    
    //if the current reading is not the same as the last reading...
    if( currSwitch != lastSwitch )
    {
        //...and the current reading is low it means we've seen a high-to-low transition
        if( currSwitch == LOW )
        {
            //do thing associated with falling edge
            
        }//if

        //our "current" reading will now be our "last" reading next time through the loop
        lastSwitch = currSwitch;
        
    }//if
    
    //...
}

(Side note: hoping you are using a driver like a transistor to switch the motor, not powering it off the digital pin? And you have a flyback diode?)

Thank you Blackfin I came up with code from your example and it compiled correctly, just to try it out now. Not_a_noob, can you elaborate on the need of a transistor. I was just hoping to run 5V directly to the MTR to power it. It is a DC mtr.

int Start_PB_NO = 50;
int Main_MTR = 23;
byte lastState;
byte currState;

void setup(){
  pinMode (Start_PB_NO, INPUT);
  
  pinMode (Main_MTR, OUTPUT);

}

void loop(){

  currState = digitalRead(Start_PB_NO);

  if (currState != lastState){
    if (currState == LOW){
      digitalWrite(Main_MTR, HIGH);
    }
  }

Janevski:
Not_a_noob, can you elaborate on the need of a transistor. I was just hoping to run 5V directly to the MTR to power it.

The digital pins are only capable of providing small currents, like the 10-20mA you might want in an LED. But even the tiniest motor draws more than that. So you need to provide the right voltage (5V in your case) from some other source that can provide the decent current, and switch that current on and off using the digital pin to switch a transistor.

The pic below, taken from here, shows how you might do that. The 12V there is of course 5V in your case.

You can read about the need for that fly-back diode here.

not_a_noob:
The digital pins are only capable of providing small currents, like the 10-20mA you might want in an LED. But even the tiniest motor draws more than that. So you need to provide the right voltage (5V in your case) from some other source that can provide the decent current, and switch that current on and off using the digital pin to switch a transistor.

The pic below, taken from here, shows how you might do that. The 12V there is of course 5V in your case.

You can read about the need for that fly-back diode here.

Thank you this makes sence to me I will be doing it.

Janevski:
Thank you this makes sence to me I will be doing it.

Meantime you can test your code with an led... even just use pin13 for that.

Janevski:
Thank you Blackfin I came up with code from your example and it compiled correctly, just to try it out now. Not_a_noob, can you elaborate on the need of a transistor. I was just hoping to run 5V directly to the MTR to power it. It is a DC mtr.

You’re close. You left out two lines (see below):

int Start_PB_NO = 50;
int Main_MTR = 23;
byte lastState;
byte currState;

void setup(){
    pinMode (Start_PB_NO, INPUT_PULLUP);
    lastState = digitalRead( start_PB_NO );     //<-- establish "last" switch reading
    pinMode (Main_MTR, OUTPUT);

}

void loop()
{

    currState = digitalRead(Start_PB_NO);

    if (currState != lastState)
    {
        if (currState == LOW)
        {
            digitalWrite(Main_MTR, HIGH);
        }
        lastState = currState   //<-- remember this as the "last state"
    }

}

You want to establish the “last” state in setup() and you want to make lastState equal to currState when see the two are different.

why not use a switch?
No code, no transistor ...
?

demkat1:
why not use a switch?
No code, no transistor ...
?

Where's the fun in that?

And it's possible there's other logic in play which might turn the motor off and on according to some sensors (temperature?, water levels?) or time.

not_a_noob:
Where's the fun in that?

And it's possible there's other logic in play which might turn the motor off and on according to some sensors (temperature?, water levels?) or time.

Initial OP's post : What I am trying to do is just turn a DC motor with a NO push button

Doesnt say anything about ...whatever more complex.
Anyway , no problem.