I want to control a stepper motor based on the input pulses. The pulses will come from a source to pin 2. Each pulse will be counted. A timer1 interrupt will interrupt after 100milli seconds and check the count. If less than 31 the stepper will move in a direction. If more than 35, then in another direction. if it is between 31 and 35 then stepper should be idle.
I have played a lot to my knowledge and now I am stuck and need help with this. My code is messed up due to experimenting but I am posting it anyway. I hope someone can make sense of it and guide me what I am doing wrong.
/*
* Timer1 library example
* June 2008 | jesse dot tane at gmail dot com
*/
#include "TimerOne.h"
int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int delayTime = 50; //this is delay between each step of the stepper motor i.e. motor speed
//int pin = 13; //declared LED for output (blink)
//volatile int state = RISING; //Trigger on rising edge
//int pin7 = 7;
int Hertz = 0; //Global integer
int Astep = 0; //Global integer which will be used as a flag to keep track of stepper movement
int Bstep = 0;
int Cstep = 0;
int Dstep = 0;
void setup()
{
pinMode(13, OUTPUT); //timer test blink
Timer1.initialize(100000); // initialize timer1, and set a 1/2 second period
Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
pinMode(motorPin1, OUTPUT); // pins set for output
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
//pinMode(pin, OUTPUT);
attachInterrupt(0, count, RISING);
}
void loop()
{
//digitalWrite(pin, state); //for blinking
//delay (300); //setting delay so that after 200mili seconds number of pulses is counted
//Hertz = counter();
// check(Hertz); //function for counting pulses and taking a decision
}
void count() //responsible for blinking of LED and calling counter
{
//state = !state;
counter(); //increment the count of hertz
}
int counter() //pulses added here after the delay thus after 300 milli secounds, the count will be compared
{
Hertz++;
return (Hertz); //glbal integer returned
}
void callback()
{
digitalWrite(13, digitalRead(13) ^ 1);
Hertz= counter();
check(Hertz);
}
void check(int Hertz){
if (Hertz < 31 && Hertz >5 )
{
low();
}
else if (Hertz > 35)
{
high();
}
if (Hertz <=5){
sustain();
}
}
int low()
{
if (Astep == 0) {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
Astep = 1;
Dstep = 0;
}
else if (Bstep == 0 && Astep ==1)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
Bstep = 1;
}
else if (Cstep == 0 && Bstep ==1)
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
Cstep =1;
}
else if (Dstep == 0 && Astep ==1)
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
Dstep=1;
Cstep=0;
Bstep=0;
Astep=0;
}
return (Astep, Bstep, Cstep, Dstep);
}
int high()
{
if (Astep == 0) {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
Astep = 1;
Dstep = 0;
}
else if (Bstep == 0 && Astep ==1)
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
Bstep = 1;
}
else if (Cstep == 0 && Bstep ==1)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
Cstep =1;
}
else if (Dstep == 0 && Astep ==1)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
Dstep=1;
Cstep=0;
Bstep=0;
Astep=0;
}
return (Astep, Bstep, Cstep, Dstep);
}
void sustain()
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}