Micro switch "kill switch", Sharp sensor and DC motor

Hello,

Could someone point me in the right direction on how I can implement a code into arduino when a micro switch is press it turns off a motor.

I am using a SHARP distance sensor that makes a motor spin once an object is detected however I also want to install a micro switch that once pressed it will turn off the motor even though the sensor still detects an object in its path.

Thanks,

Milo

If this is a safety application, don't involve software.

its college project, no ones live is at stake apart form my grade unless I cant get this micro switch from damaging the motor.

It would help us help you if you posted your code.

I found it on the net so I am using as it works:

/*

*/

// These constants won’t change. They’re used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 10, 0, 10);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor:
Serial.print(“sensor = " );
Serial.print(sensorValue);
Serial.print(”\t output = ");
Serial.println(outputValue);

// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(1);

}

outputValue = map(sensorValue, 0, 10, 0, 10);

Why?

I was playing around with the values to see what they do...

I tried inputting some code for the kill switch but it wont work. Could someone have a look at it and see what am I doing wrong? everything on the arduino and bread board is done correctly so I know its the code problem. If you could point out any mistakes then it would be greatly appreciated :slight_smile:

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

everything on the arduino and bread board is done correctly so I know its the code problem

What have you done to the input pin to stop it floating?

but it wont work

Without qualification or observation, that clause will get you no friends.

Do you want me to buy you a cake Mr ?

mspisz: Do you want me to buy you a cake Mr ?

Huh?

You need to be specific.
How is your switch wired? Do you have a pull up or pull down resistor?
You also need to debounce the switch. Switch state = high only if 2 successive high readings with some delay in between. Low set the same way.

After verifying the switch works, simply create a ‘kill motor’ variable. If button pressed set it true. If true turn off motor. IR reading is ignored until button state has been switched and killmotor set to false. Just a few ‘if’ statements.

Joe