push button interrupt

hi
i new for arduino. i make a robot with arduino and have some problems with interrupts.
Firstly robots have one pushbutton and one switch. Push button for opening and closing the robot.
touch switch is activeted in someone touch. add a interrrupt to push button because close the robot when i want.
But this time pusbutton stuck in interrupt. Robot always close.

try to add intterrupt to switch this time switch loop just pause not stop and start again.

What can i do for this?

thanks…

What can i do for this?

Write the code properly. If you are having problems with this, post your code, along with a description of the pushbutton and switch wiring (or a schematic or photo would be even better), and explain the difference between a switch and a pushbutton.

Push button for opening and closing the robot.

Opening and closing what?

#include <Servo.h>

const int sPin=2;          //switchs connected in digital pin 0
const int bPin=3;          //button connected in digital pin 1

int motorpin=6;            //button connected in digital pin 1
float nctime=0;            //not crash time:sonar detect the distance calculate the time not to crash. 
Servo leftservo;           //define new servo, left servo
Servo rightservo;          //define new servo, right servo

int ultraAn = 5;           //sonar connected in analog pin 5   
int val1 = 0;              //value from sonar that arduino measure in analog pin
float dist = 0;            //distance obtain from sonar
float sum=0;               //50 distance values sum
float average;             //avarage of 50 distance

int redledPin=7;           //red led connected in digital pin 3
int greenledPin=8;         //green led connected in digital pin 2
int yellowledPin=9;        //yellow connected in digital pin 4

// Speed Setup
int robotSpeed = 75;       //Robot speed
int rightSpeed = 50;       //right servo speed
int leftSpeed = 50;        //left servo speed

// Button Setup
int buttonPushCounter = 0; //Button push counter
int buttonState = 0;       //Button state      
int lastButtonState = 0;   //Last button state

int val=0;                 //Switch sensors value

volatile int state = LOW;  // The input state toggle		

long randtime;             //Random time for servos

void setup()
{
  Serial.begin(9600);
  setSpeed(robotSpeed);            //set the robot speed
  pinMode(redledPin, OUTPUT);      //red led pin output
  pinMode(greenledPin, OUTPUT);    //green led pin output
  pinMode(yellowledPin, OUTPUT);   //yellow led pin output
  pinMode(sPin, INPUT);            //switch sensor input
  pinMode(bPin, INPUT);            //button input
  leftservo.attach(12);            //left servo connected in digital pin 12
  rightservo.attach(13);           //right servo connected in digital pin 13
  randomSeed(analogRead(0));       //random seed


}

void loop()
{

  buttonState = digitalRead(bPin);      
  if (buttonState != lastButtonState) {   
    if (buttonState == LOW) {
      buttonPushCounter++;      } 
    else {}
  }
  lastButtonState = buttonState;
    
    if (buttonPushCounter % 2 == 1) {  //push button counts at mode 2 is equal 1
      digitalWrite(greenledPin, HIGH); //green led open 
//    digitalWrite(motorpin,HIGH);     //cleaners motor start
      val=digitalRead(sPin);         //digital read at switch pin
      delay(2000);
      if(val==0){                   //if switch value high
      digitalWrite(redledPin,LOW);
      goForward();
      Serial.print("switch e basilmadi.....go Forward");
      Serial.print("\n"); 
      delay(400); 
      }
      if(val==1){               //if switch value low
      Serial.print("switche basildi......go back");
      Serial.print("\n"); 
      delay(400); 
      goBack();
      }    
    }else{                             //push button counts at mode 2 is not equal 0
    digitalWrite(greenledPin,LOW);
    turnoff();
    Serial.print("stooop");
    Serial.print("\n");  
    delay(400); 
    
    
    } 
  attachInterrupt(0, sswitch, CHANGE);
  attachInterrupt(1, bbutton, CHANGE);
}

void setSpeed(int newSpeed){
  setSpeedLeft(newSpeed);                   
  setSpeedRight(newSpeed);                  
}
void setSpeedLeft(int newSpeed){
  if(newSpeed >= 100) {newSpeed = 100;}                                           
  if(newSpeed <= 0) {newSpeed = 0;}                                        
  leftSpeed = newSpeed * 0.9;               
}

void setSpeedRight(int newSpeed){
  if(newSpeed >= 100) {newSpeed = 100;}                                           
  if(newSpeed <= 0) {newSpeed = 0;}                                             
  rightSpeed = newSpeed * 0.9;               
}

void goForward(){ 
  leftservo.write(90 + leftSpeed);  
  rightservo.write(90 - rightSpeed);
}  
void goBackward(){ 
  leftservo.write(90 - leftSpeed); 
  rightservo.write(90 + rightSpeed);
}

void goLeft(){ 
  randtime = random(927.75,2783.25);
  leftservo.write(40);
  rightservo.write(40);
  delay(randtime);
}
  
void goStop(){ 
  leftservo.write(91); 
  rightservo.write(91);
}

void turnoff(){
  digitalWrite(greenledPin, LOW);
//  digitalWrite(motorpin,LOW);
  leftservo.write(91); 
  rightservo.write(91);
}

void goBack(){

  goStop();
  delay(500);
  goBackward();    
  delay(1000);
//  goStop();
//  delay(500);
  goLeft();
  delay(1000);
//  goStop();
//  delay(500);
  digitalWrite(redledPin,HIGH); 
  delay(500); 
  
}

void forward(){
  digitalWrite(redledPin,LOW);
  val1 = analogRead(ultraAn);    
  dist = (val1*3.870922509/2.54);          
  for(int i=0; i<100; i=i+1){
  sum=sum+dist;
  }
  average=sum/100;
  nctime=((average-5)/17.45)*1000;      
  goForward();
  delay(nctime);
  goStop();
  delay(500);
  goBackward();    
  delay(1000);
  goStop();
  delay(500);
  goLeft();
  delay(1000);
  goStop();
  delay(500);
}

void sswitch()
{

goBack();
    Serial.print("interrupt by switch....");
      Serial.print("\n"); 
      delay(400);
//   val==0;
      
}
void bbutton()
{
leftservo.write(LOW);
rightservo.write(LOW);
digitalWrite(greenledPin, LOW);
digitalWrite(redledPin, LOW);
//buttonPushCounter==0;
Serial.print("interrupt by button....");
Serial.print("\n");
}

hthis is the code,
button works as opening and closing all function for robot.

You are not activating the internal pull-up resistors. Therefore, you must have external pull-up or pull-down resistors. Do you? You didn’t answer all the questions I asked last time.

You get better responses if you answer all the questions asked. “I don’t know” or “I don’t understand” are perfectly acceptable answers.

    else {}

Useful…

      val=digitalRead(sPin);         //digital read at switch pin
      delay(2000);

Read the state of the switch, then twiddle your thumbs for 2 seconds. Well, OK, it’s your robot, but why?

You are attaching interrupt handlers to the same pins you are reading in loop(), on every pass through loop. Interrupt handlers need to be attached once, in setup(), unless they are detached somewhere else.

Interrupt handlers need to be fast. Your handler for the bPin includes Serial.print() calls, which are not fast, by any means, but are no where near as bad as the calls to delay() (several seconds worth) in the sPin handler. By the way, delay() does not work in an interrupt handler, because delay() relies on millis(), and millis() always returns the same value during an interrupt.

I see nothing that you are doing that requires any interrupts. Get rid of the attachInterrupt calls. Then, get rid of the delay() calls. Every last one of them.

Look at the blink with delay example, without delay. Learn how to use millis() to determine when it is time to change states, and write your robot sketch as a state machine (moving forward, moving backward, etc.).

  val1 = analogRead(ultraAn);    
  dist = (val1*3.870922509/2.54);          
  for(int i=0; i<100; i=i+1){
  sum=sum+dist;
  }
  average=sum/100;

Read the value once. Add it to itself 100 times. Then divide the result by 100. Oooh, that’s useful.

  goStop();

Now there’s a great function name!

thanks for helping but i dont understand this and how can i do
"You are not activating the internal pull-up resistors. Therefore, you must have external pull-up or pull-down resistors. Do you? You didn't answer all the questions I asked last time."

i dont understand this

What don't you understand?

How to properly wire up a switch?
This tutorial is pretty good: Arduino Tutorial - Lesson 5
Pay particular attention to the "Floating high above the clouds" section. I suspect that this is where your problem is. This section explains pull-up and pull-down resistors, and how to use external and internal ones.

Or is it that you don't know how to answer all the questions? Can't help you with that. Do the best you can to answer as many as you can. Ask more questions, if you need to.