Using 2 buttons instead of a switch - advise needed

Hello everyone - I've been playing with Arduino for a couple of months and have been working on a couple of small projects, mainly lighting and simple keyboard/mouse applications using Leonardo and now Micro.

I'm working on a project right now that allows the Micro to move the mouse on it's own - in an effort to fool my (corporate) instant messenger into thinking that I am still at my desk and not moving me to inactive. Right now it works with using an ON/OFF slider switch and I've developed it from mashing together a couple of the example Arduino codes:

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 5;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Mouse.begin();
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    //move mouse
    Mouse.move(0, 1, 0);
    //wait one minute
    delay(60000);
    
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

What I am trying to accomplish is to replace the slider switch with momentary buttons to start and stop the action.

Something like: pushing button 1 starts the loop but pushing button 2 stops it

I can write the code to have the button start the process, which is like what I've done above, but can't figure out how to get the Arduino to check for the stop button to be hit.

Any help would be appreciated, and thank you all in advance.

Store if you are in the on or the off state and switch on a button press.

bool state;


if(button goes pressed){ //So NOT button IS pressed but went from not pressed to pressed
  state != state;
}

if(state){
  //do what you want to do when on
}

When you use the bounce2 library it will look like:

bool state;

if( button.fell() ){ //So NOT button IS pressed but went from not pressed to pressed
  state != state;
}

if(state){
  //do what you want to do when on
}

First - THANK YOU for your help and so quickly!

I see how that would work if I wanted to measure the activity of the same button, and it actually might work easier than what I was originally trying to do, which was essentially a "play" button and a "stop" button.

It works compiling - now to test on the board to see if it behaves the desired way or if I've got to swap the code around any to utilize the pull down resistors!

If you want two buttons:

bool state;

if( startButton.fell() ){ //So NOT button IS pressed but went from not pressed to pressed
  state = true;
}
else if( stopButton.fell() ){
  state = false;
}

if(state){
  //do what you want to do when on
}

Error city..

// constants won't change. They're used here to
// set pin numbers:
const int startButton = 5;     // the number of the pushbutton pin
const int stopBbutton =6;

// variables will change:
bool state;

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(startButton, INPUT);
  pinMode(stopButton, INPUT);
  // start mouse
  Mouse.begin();
  
}

void loop() {
  // read the state of the pushbutton value:
  State = digitalRead(startButton);

 if( startButton.fell() ) //So NOT button IS pressed but went from not pressed to pressed
   state = true;
  }
  
else if( stopButton.fell() ){
  state = false; 
}
  
if(state){
    //do what you want to do when on
    //move mouse
    Mouse.move(0, 1, 0);
    //wait one minute
    delay(60000);
  }
}

I'm working on it, but I've got trouble with that fell command I think..

I've also realized that I think there is a nesting problem with my if/then statements I'm going to have to resolve but that should fix most of my logged errors.

The fell function is part of the Bounce library. You cannot just call a function on a variable...

Got that library opened up and with a little reading I've got it working the way I'd like and thank you!