start button and proximity stop

I hooked up a gear box motor with a syren 10a driver. I’ve been able to program a loop to make it run back and forth, but I’ve been attempting to make a start button and a proximity sensor that stops the loop. So ive been spinning my wheels for a while trying to make some kind of start button and stop button.
Ive been looking around for examples of start and stop buttons but I’m guessing my code skills arent where they need to be to fiddle with things because I keep having different issues. I’ve tried several different variations.

With the current example I was focusing on having one of the two buttons, so I decided to try making the proximity sensor both the on and off button.

but it is behaving like this:

  1. plug in ( motor is at rest)
  2. trip proximity sensor ( motor runs according to loop)
  3. trips proximity sensor again (no effect) facepalm

here is the code for this example. its very messy…

#include <SyRenSimplified.h>

SyRenSimplified ST; 

int run;
int buttonPin;

void setup()
{
  run = 0; //starts stopped
   buttonPin = 7; //whatever pin your button is plugged into

   pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{
  //code you always run here; you can leave this section blank if you want the entire program to stop and start, or add code here if you want it to always run
ST.motor(1,0);  

delay(100);

ST.motor(1,127);  //-127 FULL reverse.  0 is stop.  +127 is FULL forward

delay(2000);

ST.motor(1,0);

delay(100);

ST.motor(1,-127);

delay(2000);
  //check button press here and if it is pressed then toggle run variable between 0 and 255; REQUIRED!
  if(digitalRead(buttonPin) == LOW) //funcitons based off of button pulling input pin LOW
  {
     if(run == 0)
     {
         run = 255;
     }
     else
     {
         run = 0;
     }
  }

  if(run > 0)
  {
     //code you only run if button was pressed, stops running when button pressed again, so forth...
  }
}

Ultimately I would like a button that starts the loop for the motor, and have a proximity sensor to the side that when triggered, stops the motor loop. is there anyone who could maybe help me out? I’m very lost.

In the code you posted I don't see how the state of the button pin has any effect on the running of the motor.

I’m afraid I saved multiple files for everytime I changed large sections… :o It could be :

int run;
int buttonPin;
int switchState = 0;

#include <SyRenSimplified.h>
SyRenSimplified ST;

void setup()
{
SyRenTXPinSerial.begin(9600); // This is the baud rate you chose with the DIP switches.  
  ST.motor(1, 0);   
   run = 0; //starts stopped
   buttonPin = 7; //whatever pin your button is plugged into

   pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{


   //check button press here and if it is pressed then toggle run variable between 0 and 255; REQUIRED!
   if(digitalRead(buttonPin) == LOW) //funcitons based off of button pulling input pin LOW
   {
      if(run == 0)
      {
          run = 255;
      }
      else
      {
          run = 0;
      }
   }

   if(run > 0)
   {
        ST.motor(1,0);  

delay(100);

ST.motor(1,127);  //-127 FULL reverse.  0 is stop.  +127 is FULL forward

delay(2000);

ST.motor(1,0);

delay(100);

ST.motor(1,-127);

delay(2000);   }
}