Make a "BUmp Sensor" with a button activate at any time.

Hello,
What I want to happen is when a button that is glued to the front of jabduino :slight_smile: and it gets pressed (bumped into a wall) how can I make the robot then "DO THIS." The problem is I want it to be able to sense a press and "DO THIS" at any time, not just at certain times...

Thanks in Advanced, Jabelone

void setup() {                
//Setup our motors and speed control plus define our pins

// Motor 1
#define pwma 11
#define ain2 10
#define ain1 9

//Motor 2
#define pwmb 7
#define bin1 6
#define bin2 5
  
// ==== Motor 1 ===== //
  //PWMA
  pinMode(pwma, OUTPUT);
  //AIN2
  pinMode(ain2, OUTPUT); 
  //AIN1 
  pinMode(ain1, OUTPUT);
  
// ==== Motor 2 ==== //
  //BIN1
  pinMode(bin1, OUTPUT);
  //BIN2
  pinMode(bin2, OUTPUT);
  //PWMB
  pinMode(pwmb, OUTPUT);
  
  //BUTTON STUFF
  #define button 2
  pinMode(button, INPUT);
}

void loop() { 
  
 
  
// ===== Straight ===== //
  //M1
  digitalWrite(pwma, 50);
  digitalWrite(ain2, HIGH);
  digitalWrite(ain1, LOW);
  //M2
  digitalWrite(pwmb, 100);
  digitalWrite(bin2, HIGH);
  digitalWrite(bin1, LOW);
  delay(2000);
  
  // == Stop one wheel ==//
  digitalWrite(pwma, LOW);
  digitalWrite(ain2, LOW);
  digitalWrite(ain1, HIGH);
  delay(200);
    
  // === turn around === //
  //M1
  digitalWrite(pwma, 50);
  digitalWrite(ain2, LOW);
  digitalWrite(ain1, HIGH);
  //M2
  digitalWrite(pwmb, 100);
  digitalWrite(bin2, HIGH);
  digitalWrite(bin1, LOW);
  delay(750);

  button3:{
  //Go backwards
    //M1
    digitalWrite(pwma, 50);
    digitalWrite(ain2, LOW);
    digitalWrite(ain1, HIGH);
    //M2
    digitalWrite(pwmb, 100);
    digitalWrite(bin2, LOW);
    digitalWrite(bin1, HIGH);
    delay(1000);
  
  // === turn around === //
  //M1
  digitalWrite(pwma, 50);
  digitalWrite(ain2, LOW);
  digitalWrite(ain1, HIGH);
  //M2
  digitalWrite(pwmb, 100);
  digitalWrite(bin2, HIGH);
  digitalWrite(bin1, LOW);
  delay(250);
  
  }  
}

To make it work all the time you remove all the delay() calls from your code and write your code using the technique shown in the blink without delay sketch.

Thanks Heaps!!
I have been stuck on this (baaaasic thing :slight_smile: ) for ages! I have also just learnt how to make functions so I don't have to tell what motor to do what each time now I just have to write straight(2000); or left(300); etc.
Once again thanks!
:smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: