Stop button and start button ... separately but not together...

Ive been struggling to make a start and stop button for my project. Separately, the stop and start button on their own work fine, and separately, the motor loop works fine… but together I keep getting error after error. when I try to change the code according to the error messages, It changes things until nothing works… so lost.

Motor loop by itself:

#include <SyRenSimplified.h>
int switchState = 0;
SyRenSimplified ST; 

void setup(){
  SyRenTXPinSerial.begin(9600); // This is the baud rate you chose with the DIP switches.  
  ST.motor(1, 0);   
  }

void loop(){

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);

}

Start button (without stop button):

int switchState = 0;


void setup(){
  int switchState = 0;
 
  pinMode(2,INPUT);
}

//Checks the voltageo of the digital input and chooses the pin for voltage ( pin 2 )
void loop(){
  switchState = digitalRead(2);
  
  if(switchState == LOW){
    //Button is not pressed 
    
    
    digitalWrite(1,LOW); 
  }

else { //the button is pressed
  
}

void loop()
{

//Motor code here

} //go back to the beginning of the loop

I feel like this is suppose to be a easy things to do… but I’ve been almost halted by it. any help, including other projects to reference would be appreciated. I’ve scoured the forum for different types of stop and start buttons but none have really helped me out.

You should provide the actual code that is giving you errors. Otherwise people can only guess how you've combined them and what mistakes you may have made in doing so.

int switchState = 0;
#include <SyRenSimplified.h>
SyRenSimplified ST; 

void setup(){
  int switchState = 0;
 SyRenTXPinSerial.begin(9600);  
  ST.motor(1, 0); 
  
  pinMode(2,INPUT);
}

//Checks the voltageo of the digital input and chooses the pin for voltage ( pin 2 )
void loop(){
  switchState = digitalRead(2);
  
  if(switchState == LOW){
    //Button is not pressed 
    
    
    digitalWrite(1,LOW); 
  }

else { //the button is pressed
  
}

void loop()
{

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);

}

You have two loops in the combined version. You can't do that. You also seem to be missing a curly brace somewhere.

Also where does this line come from?

SyRenTXPinSerial.begin(9600);

It doesn't appear to be a valid function from the SyRenSimplified library.

Do you have a pulldown resistor (10k) from pin 2 to GND? You only need one "int switchState = 0;", remove the one in setup(). You are using pin 1 for the motor, that is the serial TX pin, use another for the motor (3?).

Oh gosh! I’m so embarrassed. two void loops… how did that even happen. no wonder it was going nuts.

“9600”
that line is suppose to define the baud rate that communicates to the driver.

(I’m not aloud any more posts for the day so I’m tacking this on my last post, my apologies )

1.pin 2 is my start button
2.the tx pin is connected to my driver (i’m not sure how to change this, I’m going off of a different project a little and that part wasnt clear to me. is “1” in “St.motor(1,0)” indicating which pin perhaps?

that line is suppose to define the baud rate that communicates to the driver

OK, what does this line do?

digitalWrite(1,LOW);

?

OK, I see now :blush: Which pins are you using for buttons?

I was a little frustrated last night and was working on this. I said before that I worked on a few different methods from some examples I was looking through in the forum.

the code that I posted was a simple digital button code pretty much straight from the book… but I figured that just writing the pin LOW wouldnt work because If I power down the arduino before the motor, the motor goes a little crazy… I didnt know if it would work or not… but since I couldnt get it to even compile I switched tactics. I was right to assume that I had to communicate what I wanted to the driver… I had a code that did this but the button wouldn’t work. but when you mentioned the 10k resistor a light bulb went off.
I wired the button incorrectly.

I was able to get a start button to work using this code:

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) == HIGH) //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);   }
}

the start button works… but I have to use the hard reset to make it stop… I dont think I can make another reset button can i?