Momentary Switch? [newbie]

Harro, Just before I go on I must warn everyone that I'm a huge noob with minimal electronic knowledge, so apologise for cluelessness :fearful:

I'm starting on a project that essentially is a motor that turns at a trigger pull, a light censor cuts off the motor's circuit when it is exposed, to reset the whole thing 2 buttons need to be pressed in the right order, I can't quite seem to get that to work since the buttons I have are momentary buttons, however due to the space and way the buttons are pressed, they can only be momentary switches.

So here's the sequence:

Trigger pull -> Motor turns -> Light censor exposed for longer than 1/2 second -> Trigger disabled -> Button 1 pressed -> Button 2 pressed -> Trigger re-activated

Since the buttons are momentary, I tried making the Arduino to "remember" that button 1 was pressed for a while by using int, but clearly that's not what it's for.

By the way I'm not home at the moment so this is just me making things up from memory, hopefully it helps to show my point.

int pressed; buttonState1 = digitalRead(1); buttonState2 = digitalRead(2);

if (buttonState1 == HIGH && !pressed) { pressed = 1; }

if (buttonState2 == HIGH && pressed == 1) { digitalWrite(MotorPin, HIGH); }

Any advice is appreciated, thanks for the patience.

No, your code is too simple. It may work but the “pressed” is not reset to 0 after use. It should also be initialized too. You need a loop to keep looking at the button states. Then how you should run the motor will be affected since arduino can’t do two things together, sensing buttons and running motor. The “break;” makes sure you skip out of the loop and then go back to beginning, sensing trigger.

Use the “#” button on forum for codes:

int pressed=0;
  buttonState1 = digitalRead(1);
  buttonState2 = digitalRead(2);

if (buttonState1 == HIGH  && !pressed) { pressed = 1; }

if (buttonState2 == HIGH && pressed == 1) { digitalWrite(MotorPin, HIGH); pressed=0; break;}

//Do your motor stuff

Thanks liudr, that was really very helpful, I'll keep trying.

You're welcome. I was in your situation some time ago and I decided to write my own buttons class. I just create a few button objects and call button1.sense() to find out if a button has been pushed and released, that is my cue to do something. If you're interested here it is: