Go Down

Topic: Stepper Kill Switch (Read 778 times) previous topic - next topic

DLGuest

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

Graynomad

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
Rob Gray aka the GRAYnomad www.robgray.com

DLGuest

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

MarkT

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 won't respond to messages, use the forum please ]

DLGuest

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?

Graynomad

#5
Apr 30, 2012, 03:32 pm Last Edit: Apr 30, 2012, 03:36 pm by Graynomad Reason: 1
I haven't got time to look through all that code but in principle you have

Code: [Select]
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

Code: [Select]
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
Rob Gray aka the GRAYnomad www.robgray.com

DLGuest

#6
Apr 30, 2012, 04:54 pm Last Edit: May 01, 2012, 09:35 am by Coding Badly Reason: 1
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;

Code: [Select]
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: [code] [/code] tags added.

Graynomad

Code: [Select]
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


Code: [Select]
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
Rob Gray aka the GRAYnomad www.robgray.com

DLGuest

Thanks Rob,
it working great now!

Go Up