Interrupt code not working as supposed to.....
hello,
i got stepper motor running code , works fine. trying write code enable me pause , turn on led on pin 13, when press button , have continue running left off when release button.
i tried attaching interrupt didn't work. modifications made in bold. know did wrong?
i'm using arduino mega 2560.
here code below. appreciated.
//////////////////////////////////////////////////////////////////
//©2011 bildr
//released under mit license - please reuse change , share
//using easy stepper arduino
//use rotate and/or rotatedeg controll stepper motor
//speed number .01 -> 1 1 being fastest -
//slower speed == stronger movement
/////////////////////////////////////////////////////////////////
#include <avr/interrupt.h>
# define led_pin 13
# define switch_pin 5 // pin 18 interrupt on arduino mega 2560[/b]
#define dir_pin 2
#define step_pin 3
void setup() {
pinmode(dir_pin, output);
pinmode(step_pin, output);
pinmode(led_pin, output);
// switch input
pinmode(18, input);
// attach interrupt on pin 18
attachinterrupt(switch_pin, light, low);
}
void light() {
digitalwrite(led_pin, high);
}
void loop(){
//rotate specific number of degrees
rotatedeg(360, 1);
delay(1000);
rotatedeg(-360, .1); //reverse
delay(1000);
//rotate specific number of microsteps (8 microsteps per step)
//a 200 step stepper take 1600 micro steps 1 full revolution
rotate(1600, .5);
delay(1000);
rotate(-1600, .25); //reverse
delay(1000);
}
void rotate(int steps, float speed){
//rotate specific number of microsteps (8 microsteps per step) - (negitive reverse movement)
//speed number .01 -> 1 1 being fastest - slower stronger
int dir = (steps > 0)? high:low;
steps = abs(steps);
digitalwrite(dir_pin,dir);
float usdelay = (1/speed) * 70;
for(int i=0; < steps; i++){
digitalwrite(step_pin, high);
delaymicroseconds(usdelay);
digitalwrite(step_pin, low);
delaymicroseconds(usdelay);
}
}
void rotatedeg(float deg, float speed){
//rotate specific number of degrees (negitive reverse movement)
//speed number .01 -> 1 1 being fastest - slower stronger
int dir = (deg > 0)? high:low;
digitalwrite(dir_pin,dir);
int steps = abs(deg)*(1/0.225);
float usdelay = (1/speed) * 70;
for(int i=0; < steps; i++){
digitalwrite(step_pin, high);
delaymicroseconds(usdelay);
digitalwrite(step_pin, low);
delaymicroseconds(usdelay);
}
}
i got stepper motor running code , works fine. trying write code enable me pause , turn on led on pin 13, when press button , have continue running left off when release button.
i tried attaching interrupt didn't work. modifications made in bold. know did wrong?
i'm using arduino mega 2560.
here code below. appreciated.
//////////////////////////////////////////////////////////////////
//©2011 bildr
//released under mit license - please reuse change , share
//using easy stepper arduino
//use rotate and/or rotatedeg controll stepper motor
//speed number .01 -> 1 1 being fastest -
//slower speed == stronger movement
/////////////////////////////////////////////////////////////////
#include <avr/interrupt.h>
# define led_pin 13
# define switch_pin 5 // pin 18 interrupt on arduino mega 2560[/b]
#define dir_pin 2
#define step_pin 3
void setup() {
pinmode(dir_pin, output);
pinmode(step_pin, output);
pinmode(led_pin, output);
// switch input
pinmode(18, input);
// attach interrupt on pin 18
attachinterrupt(switch_pin, light, low);
}
void light() {
digitalwrite(led_pin, high);
}
void loop(){
//rotate specific number of degrees
rotatedeg(360, 1);
delay(1000);
rotatedeg(-360, .1); //reverse
delay(1000);
//rotate specific number of microsteps (8 microsteps per step)
//a 200 step stepper take 1600 micro steps 1 full revolution
rotate(1600, .5);
delay(1000);
rotate(-1600, .25); //reverse
delay(1000);
}
void rotate(int steps, float speed){
//rotate specific number of microsteps (8 microsteps per step) - (negitive reverse movement)
//speed number .01 -> 1 1 being fastest - slower stronger
int dir = (steps > 0)? high:low;
steps = abs(steps);
digitalwrite(dir_pin,dir);
float usdelay = (1/speed) * 70;
for(int i=0; < steps; i++){
digitalwrite(step_pin, high);
delaymicroseconds(usdelay);
digitalwrite(step_pin, low);
delaymicroseconds(usdelay);
}
}
void rotatedeg(float deg, float speed){
//rotate specific number of degrees (negitive reverse movement)
//speed number .01 -> 1 1 being fastest - slower stronger
int dir = (deg > 0)? high:low;
digitalwrite(dir_pin,dir);
int steps = abs(deg)*(1/0.225);
float usdelay = (1/speed) * 70;
for(int i=0; < steps; i++){
digitalwrite(step_pin, high);
delaymicroseconds(usdelay);
digitalwrite(step_pin, low);
delaymicroseconds(usdelay);
}
}
i tried attaching interrupt
...
anyone know did wrong?
trying use interrupts main mistake. it's not problem calls interrupts. f adopt non-blocking approach motor control code can handle button presses , serial i/o , on @ same time moving stepper, , use these control stepper movement. cruder approach, 1 implement without refactoring sketch, poll button input in stepper control loops , block loop when button held down. limited approach though , makes difficult add in additional logic. non-blocking approach far better.
Arduino Forum > Using Arduino > Programming Questions > Interrupt code not working as supposed to.....
arduino
Comments
Post a Comment