Stepper Kill Switch

I know this is very noobish but i can’t get a kill switch working. Can anyone lend a hand?

Without any information all I can suggest is to make sure you press it.

Maybe we can help if you say what you've done and show the existing code and hardware.


Rob

Sorry. I have been following this tutorial; http://schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.html and have been trying to adapt the button tutorial on the Arduino website to fit my needs

With all problems of combining two parts of a project where one is an input device (kill switch) and one is an output device (motor), you need to define an appropriate variable or variables. Test that the input section correctly sets this variable, test that the output code correctly interprets this variable (can be tested entirely separately, see).

Then you combine the code and they communicate via this variable. Here you just need a boolean, perhaps called “kill_switch”, or alternatively use the opposite logic and call it “motor_enabled”.

Slightly more sophisticated (and more useful if each section is destined to be in a library eventually), you define a function called something like “emergency_kill_switch()” that operates the variable - then the variable is encapsulated inside the motor handling code and you just have to add a call to this function in the switch-detecting code.

I appologise for my noobishness, but i'm still on a quite basic level and am not very good with booleans. Could you write a quick example code for me to see how to do it and then i can have a go myself?

I haven't got time to look through all that code but in principle you have

loop () {
    read_switch_code();
    control_motor_code();
}

read_switch_code() {
    // code to read a switch
}

control_motor_code() {
   // code to control the motor
}

and the switch and motor parts of the code do not interact in any way.

This can be changed to something like this

boolean kill = false;

loop () {
    read_switch_code();
    control_motor_code();
}

read_switch_code() {
    // code to read a switch
   if (switch is set)
     kill = true;
}

control_motor_code() {
   // code to control the motor
   if (kill)
     stop the motor
}

Now the switch code can tell the motor code what to do.


Rob

I wrote a code that i thought would work, but when i hit my kill switch the motor simply started making a higher pitched hum.
Here is the code could you please explain why it didn’t work;

const int buttonPin = 1;
int dirPin = 8;
int stepPin = 9;

int Distance = 0;
int buttonState = 0;

void setup() {                

  pinMode(buttonPin, INPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  digitalWrite(dirPin, LOW);
  digitalWrite(stepPin, LOW);
}

void loop() {
  
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(100);          
  digitalWrite(stepPin, LOW); 
  delayMicroseconds(100);

  Distance = Distance + 1;
  if (Distance == 5000)
  {
    if (digitalRead(dirPin) == LOW)
    {
      digitalWrite(dirPin, HIGH);
    }
    else
    { 
      digitalWrite(dirPin, LOW);
    }
    Distance = 0;
    delay(500);  
 }
    if (digitalRead(buttonPin) == HIGH) {          
      digitalWrite(stepPin, LOW); 
}
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

void loop() {

  digitalWrite(stepPin, HIGH);
  delayMicroseconds(100);         
  digitalWrite(stepPin, LOW);
  delayMicroseconds(100);
...

if (digitalRead(buttonPin) == HIGH) {         
      digitalWrite(stepPin, LOW);
}

You "disable" stepPin then immediately set it high again. Try

void loop() {

if (digitalRead(buttonPin) == HIGH) {         
      digitalWrite(stepPin, LOW);
} else {
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(100);         
  digitalWrite(stepPin, LOW);
  delayMicroseconds(100);
}

Note also the use of the code tags (the # button) to make the code easier to read.


Rob

Thanks Rob, it working great now!