Pages: [1]   Go Down
Author Topic: Stepper Kill Switch  (Read 668 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 121
Posts: 8434
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Shannon Member
****
Karma: 160
Posts: 10416
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 121
Posts: 8434
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
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
« Last Edit: April 30, 2012, 08:36:56 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: May 01, 2012, 02:35:41 am by Coding Badly » Logged

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 121
Posts: 8434
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Rob,
it working great now!
Logged

Pages: [1]   Go Up
Jump to: