Stepper motor with a 4 digit 7 segment display and a 74HC595

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;
}
 }
}

Please post your entire sketch after formatting it with ctrl-T, inside code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.

Okay, thank you. I have posted the code i used to make the motor turn a certain amount in one direction and then in the opposite directions while using a one digit display

Simple Image Posting Guide

…R

Picture of how i have the display is added in. Thank you

The display uses pin 8 in the diagram, and the motor uses pin 8 in the code.

@aarg i corrected this when trying to combine the motor and display. I am using 6, 9-11 pins for the motor. Sorry about that

agomez101:
@aarg i corrected this when trying to combine the motor and display. I am using 6, 9-11 pins for the motor. Sorry about that

Please re-post anything that has changed, so that we can be sure that we have a correct schematic and code, and that they agree.