Hi im controlling a stepper motor and positions them according to the button pressed. 4 positions and 4 buttons in total.. the 4 positions are as follow: 0,90,180,270;
if left button pressed go to 90 degrees
if top button pressed go return to 0 degrees
if right button pressed go to 270 degrees
if down button pressed go to 180 degrees
all works well but my problem is delay.. becoz im also controlling a servo while controlling a stepper. the servo moves from left to right but when i push the button to control the stepper the movement of the servo stops.. it first execute the rotation of the stepper.
here's the code..
void button(){
int reading1 = digitalRead(TOP_BT);
int reading2 = digitalRead(LEFT_BT);
int reading3 = digitalRead(DOWN_BT);
int reading4 = digitalRead(RIGHT_BT);
int reading5 = digitalRead(RANDOM_BT);
if (reading1 != lastReading1) {
lastDebounceTime1 = millis();
lastReading1 = reading1;
}
if (reading2 != lastReading2) {
lastDebounceTime2 = millis();
lastReading2 = reading2;
}
if (reading3 != lastReading3) {
lastDebounceTime3 = millis();
lastReading3 = reading3;
}
if (reading4 != lastReading4) {
lastDebounceTime4 = millis();
lastReading4 = reading4;
}
if(digitalRead(TOP_BT) && (millis() - lastDebounceTime1) > debounceDelay){
MoveAbsoluteAngle(0);
}
if(digitalRead(LEFT_BT) && (millis() - lastDebounceTime2) > debounceDelay){
MoveAbsoluteAngle(45); // 90 degrees
}
if(digitalRead(DOWN_BT) && (millis() - lastDebounceTime3) > debounceDelay){
MoveAbsoluteAngle(90); // 180 degrees
}
if(digitalRead(RIGHT_BT) && (millis() - lastDebounceTime4) > debounceDelay){
MoveAbsoluteAngle(135); // 270 degrees
}
}
void MoveAbsolute(int position){
if (present_position<position) // present_postion is declared globally and is initialize to 0
{
digitalWrite(dirPin,LOW);
for( ; present_position<position; present_position++)
{
delay(5);
digitalWrite(clkPin,LOW);
delay(5);
digitalWrite(clkPin,HIGH);
}
}
else
{
digitalWrite(dirPin,HIGH);
for( ; present_position>position; present_position--)
{
delay(5);
digitalWrite(clkPin,LOW);
delay(5);
digitalWrite(clkPin,HIGH);
}
}
}
void MoveAbsoluteAngle(int degrees)
{
int position = (50*degrees)/45; // 200 steps for 360 degrees
MoveAbsolute(position);
}
My code works but after modifying it removing the delay. it doesn't move at all with button presses.
here's the code..
void button(){
int reading1 = digitalRead(TOP_BT);
int reading2 = digitalRead(LEFT_BT);
int reading3 = digitalRead(DOWN_BT);
int reading4 = digitalRead(RIGHT_BT);
int reading5 = digitalRead(RANDOM_BT);
if (reading1 != lastReading1) {
lastDebounceTime1 = millis();
lastReading1 = reading1;
}
if (reading2 != lastReading2) {
lastDebounceTime2 = millis();
lastReading2 = reading2;
}
if (reading3 != lastReading3) {
lastDebounceTime3 = millis();
lastReading3 = reading3;
}
if (reading4 != lastReading4) {
lastDebounceTime4 = millis();
lastReading4 = reading4;
}
if(digitalRead(TOP_BT) && (millis() - lastDebounceTime1) > debounceDelay){
MoveAbsoluteAngle(0);
}
if(digitalRead(LEFT_BT) && (millis() - lastDebounceTime2) > debounceDelay){
MoveAbsoluteAngle(45); // 90 degrees
}
if(digitalRead(DOWN_BT) && (millis() - lastDebounceTime3) > debounceDelay){
MoveAbsoluteAngle(90); // 180 degrees
}
if(digitalRead(RIGHT_BT) && (millis() - lastDebounceTime4) > debounceDelay){
MoveAbsoluteAngle(135); // 270 degrees
}
}
void MoveAbsolute(int position){
if((millis() - lastStepTime) > stepDelay) //stepDelay = 5; and lastStepTime = 0;
{
digitalWrite(dirPin,LOW);
if(present_position < position)
{
digitalWrite(clkPin,LOW);
digitalWrite(clkPin,HIGH);
present_position++;
}
lastStepTime = millis();
}
if((millis() - lastStepTime) > stepDelay)
{
digitalWrite(dirPin,HIGH);
if(present_position > position)
{
digitalWrite(clkPin,LOW);
digitalWrite(clkPin,HIGH);
present_position--;
}
lastStepTime = millis();
}
}
void MoveAbsoluteAngle(int degrees)
{
int position = (50*degrees)/45; // 200 steps for 360 degrees
MoveAbsolute(position);
}
It does not work anymore with button presses.. I did the same thing with the servo removing the delay and it works well but what am i doing wrong with the stepper?.. hope somebody can help me been doing this already for 3 days..