Good afternoon everyone,
I am suppose to create a program where a 4 digit display will show the distance the stepper motor has traveled. Unfortunately I am having trouble creating this code. I currently have a code that uses a stepper motor and a one digit display. But this code only makes the motor turn in one direction for a certain amount of turns and then rotates the other way. The one digit display was used to show which turn the motor is currently one. I thought i can play around with this code to create the new code but i have had no luck.
I apologize if this post is in the wrong section. I am also attaching a picture of my current set up.
Thank you in advance for any help i receive.
Code:
int motorpin1=8;
int motorpin2=9;
int motorpin3=10;
int motorpin4=11;
int direction_rotation=1;
int delayValue=2;
int steps_per_rotation= 2048;
int stepCounter=0;
int turnCounter=0;
int stop_turn=9;
int latchPin = 2; //pin 12 on the 595
int dataPin = 3; //pin 14 on the 595
int clockPin = 4; //pin 11 on the 595
// display conversions
int digitvalue[] = {126,12,182,158,204,218,250,14,254,206};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(motorpin1,OUTPUT);
pinMode(motorpin2,OUTPUT);
pinMode(motorpin3,OUTPUT);
pinMode(motorpin4,OUTPUT);
pinMode(latchPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(dataPin,OUTPUT);
}
void loop() {
//count from 0 to 255 and display the number on the LEDs
if (direction_rotation ==1){ // 1 clockwise
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
digitalWrite(motorpin3, LOW);
digitalWrite(motorpin4, LOW);
stepCounter++;
delay(delayValue);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);
digitalWrite(motorpin3, LOW);
digitalWrite(motorpin4, LOW);
stepCounter++;
delay(delayValue);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
digitalWrite(motorpin3, HIGH);
digitalWrite(motorpin4, LOW);
stepCounter++;
delay(delayValue);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
digitalWrite(motorpin3, LOW);
digitalWrite(motorpin4, HIGH);
stepCounter++;
delay(delayValue);
if (stepCounter == steps_per_rotation*(turnCounter + 1) && turnCounter < stop_turn) {
turnCounter++;
Serial.println(turnCounter);
}
digitalWrite(latchPin,LOW);
// //shift out the bits:
shiftOut(dataPin,clockPin, MSBFIRST, digitvalue[turnCounter]);
// //take the latch pin high the LEDs will light up:
digitalWrite(latchPin,HIGH);
if (turnCounter >= stop_turn) {
delay(2000);
direction_rotation = -1;
}
}
if (direction_rotation == -1){ // -1 counterclockwise
digitalWrite(motorpin4, HIGH);
digitalWrite(motorpin3, LOW);
digitalWrite(motorpin2, LOW);
digitalWrite(motorpin1, LOW);
stepCounter--;
delay(delayValue);
digitalWrite(motorpin4, LOW);
digitalWrite(motorpin3, HIGH);
digitalWrite(motorpin2, LOW);
digitalWrite(motorpin1, LOW);
stepCounter--;
delay(delayValue);
digitalWrite(motorpin4, LOW);
digitalWrite(motorpin3, LOW);
digitalWrite(motorpin2, HIGH);
digitalWrite(motorpin1, LOW);
stepCounter--;
delay(delayValue);
digitalWrite(motorpin4, LOW);
digitalWrite(motorpin3, LOW);
digitalWrite(motorpin2, LOW);
digitalWrite(motorpin1, HIGH);
stepCounter--;
delay(delayValue);
if (stepCounter == steps_per_rotation*(turnCounter - 1) && turnCounter > 0) {
turnCounter--;
Serial.println(turnCounter);
}
digitalWrite(latchPin,LOW);
// //shift out the bits:
shiftOut(dataPin,clockPin, MSBFIRST, digitvalue[turnCounter]);
// //take the latch pin high the LEDs will light up:
digitalWrite(latchPin,HIGH);
if (stepCounter <= 0) {
delay(2000);
direction_rotation = 1;
}
}
}