Bump switch/sensor question

Hi,

I'm having problems writing some code that will stop a DC motor when a bump switch/sensor is pressed once. I can get the motor to stop when the switch is HELD down but I really need it to stop when it is only pressed once.

It then needs to wait a number of seconds before starting again.

Anyone have any ideas?

Thanks!

Also, here is my code:

int switchState = digitalRead(2);
int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;

void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
digitalWrite(2, HIGH);
}

int direction = LOW;

void loop() {

int sensorValue = digitalRead(2);
Serial.println(sensorValue);

if (sensorValue == HIGH) {
int value;
for(value = 0; value <= 255; value += 5) {
digitalWrite(M1, direction);
digitalWrite(M2, direction);
analogWrite(E1, value); //PWM Speed Control
analogWrite(E2, value); //PWM Speed Control
}
}
else if (sensorValue == LOW){
int value;
int direction = LOW;

digitalWrite(M1, direction);
digitalWrite(M2, direction);
analogWrite(E1, LOW); //PWM Speed Control
analogWrite(E2, LOW); //PWM Speed Control

}
}

  1. format your code with Cntrl+T in arduino

  2. put it in code brackets

int value;
    for(value = 0; value <= 255; value += 5)

is the same as

for(int value = 0; value <= 255; value += 5)
  1. int switchState = digitalRead(2); should just be int switchState = 0; for initialization.

  2. in the arduino ide direction (part of int direction = LOW; and other places) is orange colored which means it may already be used my the Arduino as a function or something, change the name a little.

  3. just a tip, you dont have to use a int for reading a digital pin, as it is 0 or 1(LOW or HIGH), cant just use a boolean type instead.

  4. why do you create another value integer and direction integer in this code?:

else if (sensorValue == LOW){
    int value; //here----
    int direction = LOW; 

    digitalWrite(M1, direction);
    digitalWrite(M2, direction);
    analogWrite(E1, LOW); //PWM Speed Control
    analogWrite(E2, LOW); //PWM Speed Control
  }

should be like this, i think, if i understand correctly:

else if (sensorValue == LOW){
    digitalWrite(M1, direction);
    digitalWrite(M2, direction);
    analogWrite(E1, LOW); //PWM Speed Control
    analogWrite(E2, LOW); //PWM Speed Control
  }
  1. now onto your original question :stuck_out_tongue: you may want to try debouncing(http://arduino.cc/it/Tutorial/Debounce), and or interrupt based button press detection:
volatile long lastDebounceTime = 0;   // the last time the interrupt was triggered
#define debounceDelay  50    // the debounce time; decrease if quick button presses are ignored
#define BtnPin 2

void setup() {
  // put your setup code here, to run once:
  attachInterrupt(BtnPin,FireEvent, RISING);
}

void loop() {
  // put your main code here, to run repeatedly: 
}

void FireEvent()
{
  if ((millis() - lastDebounceTime) > debounceDelay)
  {
    lastDebounceTime = millis(); 
   //switch was pressed, do stuff like turn off your motor.
  }
}

hope it helps! feel free to ask more questions.