Help needed to stop a stepper in home position using interrupt.


hi,
this first post, , in traditional noob fashion cry help!!
i have built filter wheel 9 positions, driven smal stepper motor on rim, , controlled arduino nano 3.0 , easydriver.
it requires 22366 steps complete 1 complete revolution, , has mechanical switch act home position.
i controlling arduino via the  serial monitor inputing filter number , wheel turnes position.
changing position working correctly, code using set wheel home not. have been trying different things several days can't crack it. time admit defeat , ask help!
here code;
code: [select]
int home_pin = 0;  // interrupt 0 on digital pin 2!
int step_pin = 5;  //easydrver step pin.
int current_position = 0;
int new_position = 0;
volatile int state = low;  // input state toggle

// interupt state change, (if switch state changes, stop stepper , delay).
void statechange()

{
  state = !state;
  digitalwrite(step_pin, low);
  delay(50000);
  current_position=1;
  serial.println("home position");
}

//rotate specific number of microsteps (8 microsteps per step)
//speed number .01 -> 1 1 being fastest - slower stronger
void rotate(int steps, float speed)

{
  steps = abs(steps);
  float usdelay = (1/speed) * 70;

  for(int i=0; < steps; i++){
    digitalwrite(step_pin, high);
    delaymicroseconds(usdelay);

    digitalwrite(step_pin, low);
    delaymicroseconds(usdelay);
  }
}
 
void setup()



  serial.begin(9600); 
  pinmode(step_pin, output);
  attachinterrupt(home_pin, statechange, change);  //attach interrupt input pin , monitor change (digital pin 2).
 
    rotate(23000, 1);  //rotate 23000 seps (just on 1 revolution) @ full speed.
}

void loop()
{
  detachinterrupt (0);
  while (serial.available()==0);
  int val=serial.read()-'0';
  int new_position = (current_position - val);
  serial.print ("positions moved = ");
  serial.println (new_position);
 
  if ((new_position == -1)||(new_position == 8))
{
  rotate (2487, .7);
serial.print("position ");
serial.println( val);
current_position = (val);

}
  else  if ((new_position == -2)||(new_position == 7))
{
  rotate (4974, .7);
serial.print("position ");
serial.println( val);
current_position = (val);

}
  else  if ((new_position == -3)||(new_position == 6))
{
  rotate (7461, .7);
serial.print("position ");
serial.println( val);
current_position = (val);

}
  else  if ((new_position == -4)||(new_position == 5))
{
  rotate (9948, .7);
serial.print("position ");
serial.println( val);
current_position = (val);

}
else  if ((new_position == -5)||(new_position == 4))
{
  rotate (12435, .7);
serial.print("position ");
serial.println( val);
current_position = (val);

}
else  if ((new_position == -6)||(new_position == 3))
{
  rotate (14922, .7);
serial.print("position ");
serial.println( val);
current_position = (val);

}
else  if ((new_position == -7)||(new_position == 2))
{
  rotate (17409, .7);
serial.print("position ");
serial.println( val);
current_position = (val);

}
else  if ((new_position == -8)||(new_position == 1))
{
  rotate (19896, .7);
serial.print("position ");
serial.println( val);
current_position = (val);

}
else (new_position == 0);
{
}

}


on startup wheel rotates , stops @ home position, best can pause it, finishes 23000 steps.
how can skip section of code once home position has been reached, , go on main loop?
thanks in advance help.

jason.


well start...

code: [select]
// interupt state change, (if switch state changes, stop stepper , delay).
void statechange()

{
  ...
  delay(50000);
  ...
  serial.println("home position");
}
...
  attachinterrupt(home_pin, statechange, change);  //attach interrupt input pin , monitor change (digital pin 2).


interrupts driven events such delay() , serial communication should not used in isrs. isrs meant quick, such setting flag or variable. in case, when receive notification home position reached, should set sort of flag variable. in code moving stepper, should checking see if variable set. if so, clear , stop moving stepper.
 


Arduino Forum > Using Arduino > Programming Questions > Help needed to stop a stepper in home position using interrupt.


arduino

Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

How to format a Get Request

avrdude: verification error, first mismatch at byte 0x0000 0x0c != 0x62