Pages: [1]   Go Down
Author Topic: Make a "BUmp Sensor" with a button activate at any time.  (Read 534 times)
0 Members and 1 Guest are viewing this topic.
Brisbane, QLD, Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
What I want to happen is when a button that is glued to the front of jabduino smiley 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
Code:
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);
 
  } 
}
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34143
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Brisbane, QLD, Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Heaps!!I have been stuck on this (baaaasic thing smiley ) 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-grin smiley-grin smiley-grin smiley-grin smiley-grin smiley-grin smiley-grin smiley-grin smiley-grin smiley-grin
Logged

Pages: [1]   Go Up
Jump to: