Reading Analog Voltage ()

Hi, guy, i am very very new to using an Arduino and how i would like to implement one on a project i and working on. so basically i have a rotating wave plate controlled by a stepper motor and i want to analog read an number of voltages from a photo diode during that time of rotation. How would i go about this?
lastly, i have set the delay time to 50milliseconds between steps and i would like to analog read a voltage in that time span for the full revolution
my sketch code for the stepper motor operation is below:

#include <Stepper.h>

int delaylegnth = 50;
long startTime,stopTime;
long myDesiredTime;
void setup() {


 //establish motor direction toggle pins
 pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
 pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
 
 //establish motor brake pins
 pinMode(9, OUTPUT); //brake (disable) CH A
 pinMode(8, OUTPUT); //brake (disable) CH B
startTime=0;
stopTime=0;
myDesiredTime=20000;
}

void loop(){
if(stopTime-startTime<=myDesiredTime){
 digitalWrite(9, LOW);  //ENABLE CH A
 digitalWrite(8, HIGH); //DISABLE CH B

 digitalWrite(12, HIGH);   //Sets direction of CH A
 analogWrite(3, 255);   //Moves CH A
 
 delay(delaylegnth);
 
 digitalWrite(9, HIGH);  //DISABLE CH A
 digitalWrite(8, LOW); //ENABLE CH B

 digitalWrite(13, LOW);   //Sets direction of CH B
 analogWrite(11, 255);   //Moves CH B
 
 delay(delaylegnth);
 
 digitalWrite(9, LOW);  //ENABLE CH A
 digitalWrite(8, HIGH); //DISABLE CH B

 digitalWrite(12, LOW);   //Sets direction of CH A
 analogWrite(3, 255);   //Moves CH A
 
 delay(delaylegnth);
   
 digitalWrite(9, HIGH);  //DISABLE CH A
 digitalWrite(8, LOW); //ENABLE CH B

 digitalWrite(13, HIGH);   //Sets direction of CH B
 analogWrite(11, 255);   //Moves CH B
 
 delay(delaylegnth);
 stopTime=millis();
}
}

Any advice or help will be highly appreciated, Thank you!

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Read this first, why did you skip it: http://forum.arduino.cc/index.php/topic,148850.0.html

No, seriously, read it. Because when you improperly post code like that a lot of folks will just skip the thread because they figure if you're not going to read the instructions and help us to help you then you must not want help that bad.

uche_ofili7: i want to analog read an number of voltages from a photo diode during that time of rotation. How would i go about this?

Have you tried: https://www.arduino.cc/en/Reference/AnalogRead

Hi, sorry, thanks for the correction, i'll edit my initial post now

You #include the Stepper library, but then don’t use it!

Bite the bullet and look at the Stepper or AccelStepper library examples and figure out
how to use them properly, that will allow your code to be much clearer, perhaps something like:

  for (int i = 0 ; i < MAX ; i++)
  {
    stepper.moveTo (i) ;
    delay (50) ;
    handle_photo_diode (analogRead (photodiode_pin)) ;
  }

Clarity and simplicity of code is vital to managing it - don’t go down the road of combining
lots of low level operations for different things all mashed together in a single massive
1000-line function. Aim for most functions to be less than 10 lines long, a few upto perhaps 30 lines.

Hi guys, I am very very new to arduino but have researched this problem to the best of my ability. I have a project where i am using a stepper motor to rotate a wave plate whilst a laser beam is shunned through it, then i have to am store the value of each step in angle in my arduino memory so that i can later use in in MATLAB. I have programmed the code up to the point that it stores the analogread(). Can some white more experience check how feasible the current code is as i have no idea to check if it actually will work because the photodiode is my university lab is currently spoilt.

Thank you

#include <Stepper.h>
#include <EEPROM.h>

int delaylength = 30; 
long startTime, stopTime, myDesiredTime;
int analogPin = 3;
float val; 
/*the current address in the EEPROM (i.e. which byte we're going to write to next) **/
int addr = 0;


void enableChannel(int pinA, int pinB) 
{
  digitalWrite(pinA, LOW);  //ENABLE CH A
  digitalWrite(pinB, HIGH); //DISABLE CH B
  
}

void setPinDirection(int pin, int direction_pin, int pin_level) // i.e. setPinDirection(12, 8, HIGH)
{
  digitalWrite(pin, pin_level);   //Sets direction of CH
  analogWrite(direction_pin, 255);   //Moves CH at speed max speed - 255
}

void setup() {


  Serial.begin(9600);

  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW 
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW 
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
  
  startTime=0;
  stopTime=0;
  myDesiredTime=33000;
}
void function(void) {
int val = analogRead(0) / 4;
    EEPROM.write(addr, val);
    addr = addr + 1;
     if (addr == EEPROM.length()) {
    addr = 0;
  }
}



void loop(){
  if(stopTime-startTime<=myDesiredTime){
    enableChannel(9, 8); // Enable CH A
    setPinDirection(12, 3, HIGH); //Sets direction of CH A and Moves CH A
    Serial.println(analogRead(analogPin));
    function();

    delay(delaylength);
    
    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, LOW); //Sets direction of CH B and Moves CH B
    Serial.println(analogRead(analogPin));
    function();
    delay(delaylength);
    
    enableChannel(9, 8); // Enable CH A
    setPinDirection(12, 3, LOW); //Sets direction of CH A and Moves CH A
    Serial.println(analogRead(analogPin));
    function();
    
    delay(delaylength);
    
    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, HIGH); //Sets direction of CH B and Moves CH B
    Serial.println(analogRead(analogPin));
    function();
    
    delay(delaylength);
    stopTime=millis();
 }
}
void function(void) {
int val = analogRead(0) / 4;
    EEPROM.write(addr, val);
    addr = addr + 1;
     if (addr == EEPROM.length()) {
    addr = 0;
  }
}

"function" is a poor name for this function. Function names should always reflect what they actually do. If you change the function to do something different, give it a different name.

The EEPROM is addressed in bytes. An int is either 2 or 4 bytes long (depending on exactly which Arduino you have.) You will be overwriting part of each int with the int following.

I see you're using millis() to decide when to stop but why do you have this: delay(delaylength); You should probably be using millis() to control the timing of the events, so that you will know how many events. The way it's written, you have no idea if you overran the end of the EEPROM.

Hi, Thanks for the reply!, I have adjusted it so and i know the analog read() function works because i have tested it with a function generator and i get a decent sampled diagram for a triangular wave.However I need to to incorporate code so i know what angle i am at currently. Do you have any ideas as to how i could go about this?

#include <Stepper.h>


int val; 
int analogPin = 4;
int address = 0;
int delaylength = 1; 
long startTime, stopTime, myDesiredTime;
byte value;
float realvalue = 0;
float realvalue1 = 0;
float realvalue2;
float realvalue3;
float realvalue4;
float realvalue5;
int average1;
volatile boolean flag = false;
int A = 0;
void enableChannel(int pinA, int pinB) //enables the two channels A and B of the stepper motor  
{
  digitalWrite(pinA, LOW);  //ENABLE CH A
  digitalWrite(pinB, HIGH); //DISABLE CH B
  
}

void setPinDirection(int pin, int direction_pin, int pin_level) //  sets direction of the channel.i.e. setPinDirection(12, 8, HIGH)
{
  digitalWrite(pin, pin_level);   //Sets direction of CH
  analogWrite(direction_pin, 255);   //Moves CH at speed max speed - 255
}

void setup() {
  Serial.begin(9600);
  startTime = 0;
  stopTime = 0;
  myDesiredTime = 20000; //  my desired time i want program to run for In milliseconds

  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW 
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW 
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B

  
attachInterrupt(digitalPinToInterrupt(12),motorpinchange ,CHANGE);// interrupt to pin 12 operation 
}

void motorpinchange()// flag function to show  when pin has changed so it can be intterrupted and read
{
  flag = true;
}

void storeAnalogValue() {
 realvalue = analogRead(4);
 realvalue1=  analogRead(4)+realvalue; 
 realvalue2=  analogRead(4)+realvalue1;
 realvalue3=  analogRead(4)+realvalue2;
 realvalue4=  analogRead(4)+realvalue3;
 realvalue5= analogRead(4)+realvalue4;
 average1 = (realvalue5)/6;
  
  }




void loop(){
  //while( below)
  while (stopTime <= myDesiredTime){

    // start
    enableChannel(9, 8); // Enable CH A
    setPinDirection(12, 3, HIGH); //Sets direction of CH A and Moves CH A
    if (flag){
      flag = false;
      storeAnalogValue();
      detachInterrupt(digitalPinToInterrupt(12));
      attachInterrupt(digitalPinToInterrupt(13),motorpinchange ,CHANGE);
    }
    Serial.println(average1);
    //storeAnalogValue();
    delay(delaylength);
    // finish
    
    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, LOW); //Sets direction of CH B and Moves CH B
    if (flag){
      flag = false;
      storeAnalogValue();
    }
    Serial.println(average1);// outputs real value as a byte
    //storeAnalogValue();
    delay(delaylength);
    
    enableChannel(9, 8); // Enable CH A
    setPinDirection(12, 3, LOW); //Sets direction of CH A and Moves CH A
    if (flag){
      flag = false;
      storeAnalogValue();
    }
    Serial.println(average1);// outputs real value as a byte
    //storeAnalogValue();
    delay(delaylength);
    
    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, HIGH); //Sets direction of CH B and Moves CH B
    Serial.println(average1);
    storeAnalogValue();
    
    delay(delaylength);
    stopTime=millis();
}




}

Hi, I need to to incorporate code so i know what angle i am at currently at when a stepper motor rotates . does anyone have any ideas as to how i could go about this?

#include <Stepper.h>


int val; 
int analogPin = 4;
int address = 0;
int delaylength = 1; 
long startTime, stopTime, myDesiredTime;
byte value;
float realvalue = 0;
float realvalue1 = 0;
float realvalue2;
float realvalue3;
float realvalue4;
float realvalue5;
int average1;
volatile boolean flag = false;
int A = 0;
void enableChannel(int pinA, int pinB) //enables the two channels A and B of the stepper motor  
{
  digitalWrite(pinA, LOW);  //ENABLE CH A
  digitalWrite(pinB, HIGH); //DISABLE CH B
  
}

void setPinDirection(int pin, int direction_pin, int pin_level) //  sets direction of the channel.i.e. setPinDirection(12, 8, HIGH)
{
  digitalWrite(pin, pin_level);   //Sets direction of CH
  analogWrite(direction_pin, 255);   //Moves CH at speed max speed - 255
}

void setup() {
  Serial.begin(9600);
  startTime = 0;
  stopTime = 0;
  myDesiredTime = 20000; //  my desired time i want program to run for In milliseconds

  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW 
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW 
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B

  
attachInterrupt(digitalPinToInterrupt(12),motorpinchange ,CHANGE);// interrupt to pin 12 operation 
}

void motorpinchange()// flag function to show  when pin has changed so it can be intterrupted and read
{
  flag = true;
}

void storeAnalogValue() {
 realvalue = analogRead(4);
 realvalue1=  analogRead(4)+realvalue; 
 realvalue2=  analogRead(4)+realvalue1;
 realvalue3=  analogRead(4)+realvalue2;
 realvalue4=  analogRead(4)+realvalue3;
 realvalue5= analogRead(4)+realvalue4;
 average1 = (realvalue5)/6;
  
  }




void loop(){
  //while( below)
  while (stopTime <= myDesiredTime){

    // start
    enableChannel(9, 8); // Enable CH A
    setPinDirection(12, 3, HIGH); //Sets direction of CH A and Moves CH A
    if (flag){
      flag = false;
      storeAnalogValue();
      detachInterrupt(digitalPinToInterrupt(12));
      attachInterrupt(digitalPinToInterrupt(13),motorpinchange ,CHANGE);
    }
    Serial.println(average1);
    //storeAnalogValue();
    delay(delaylength);
    // finish
    
    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, LOW); //Sets direction of CH B and Moves CH B
    if (flag){
      flag = false;
      storeAnalogValue();
    }
    Serial.println(average1);// outputs real value as a byte
    //storeAnalogValue();
    delay(delaylength);
    
    enableChannel(9, 8); // Enable CH A
    setPinDirection(12, 3, LOW); //Sets direction of CH A and Moves CH A
    if (flag){
      flag = false;
      storeAnalogValue();
    }
    Serial.println(average1);// outputs real value as a byte
    //storeAnalogValue();
    delay(delaylength);
    
    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, HIGH); //Sets direction of CH B and Moves CH B
    Serial.println(average1);
    storeAnalogValue();
    
    delay(delaylength);
    stopTime=millis();
}




}

Basic math. angle = (current step number)*360/(steps per revolution)

uche_ofili7: Hi, I need to to incorporate code so i know what angle i am at currently at when a stepper motor rotates

A big part of that is knowing the start position. That is usually determined by driving the motor until it triggers a limit switch. Then the Arduino knows the motor is at the HOME or ZERO position.

After that just count the steps to know the current angle.

Of course that assumes the motor does not miss steps. If you have to know the position of a motor that loses steps then you need to put a rotary encoder on the motor shaft - which will add a lot of complication to the code.

...R

Hi, thanks alot for the quick reply, what i had in mind was just to simply add a increment in to angle after each analog read() and thus step function but for some reason my angle restarts at 160 degrees to -162?
Below is my code

#include <Stepper.h>


int val; 
int analogPin = 4;
int address = 0;
int delaylength = 1; 
long startTime, stopTime, myDesiredTime;
byte value;
float realvalue = 0;
float realvalue1 ;
float realvalue2;
float realvalue3;
float realvalue4;
float realvalue5;
int average1;
int angle;
int currentstepnumber;
volatile boolean flag = false;
int A = 0;
int stepnumber=0;
int stepsPerRevolution = 200;
void enableChannel(int pinA, int pinB) //enables the two channels A and B of the stepper motor  
{
  digitalWrite(pinA, LOW);  //ENABLE CH A
  digitalWrite(pinB, HIGH); //DISABLE CH B
  
}

void setPinDirection(int pin, int direction_pin, int pin_level) //  sets direction of the channel.i.e. setPinDirection(12, 8, HIGH)
{
  digitalWrite(pin, pin_level);   //Sets direction of CH
  analogWrite(direction_pin, 255);   //Moves CH at speed max speed - 255
}

void setup() {
  Serial.begin(9600);
  startTime = 0;
  stopTime = 0;
  myDesiredTime = 20000; //  my desired time i want program to run for In milliseconds

  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW 
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW 
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B

  
attachInterrupt(digitalPinToInterrupt(12),motorpinchange ,CHANGE);// interrupt to pin 12 operation 
}

void motorpinchange()// flag function to show  when pin has changed so it can be intterrupted and read
{
  flag = true;
}

void storeAnalogValue() {
 realvalue = analogRead(4);
 realvalue1=  analogRead(4)+realvalue; 
 realvalue2=  analogRead(4)+realvalue1;
 realvalue3=  analogRead(4)+realvalue2;
 realvalue4=  analogRead(4)+realvalue3;
 realvalue5= analogRead(4)+realvalue4;
 average1 = (realvalue5)/6;
  
  }




void loop(){
  //while( below)
  while (stopTime <= myDesiredTime){

    // start
    enableChannel(9, 8); // Enable CH A
    setPinDirection(12, 3, HIGH); //Sets direction of CH A and Moves CH A
    if (flag){
      flag = false;
      storeAnalogValue();
      detachInterrupt(digitalPinToInterrupt(12));
      attachInterrupt(digitalPinToInterrupt(13),motorpinchange ,CHANGE);
    }
    Serial.println(average1);
    //storeAnalogValue();
    currentstepnumber = ++stepnumber;
  angle = ((currentstepnumber)*360)/(stepsPerRevolution);
  Serial.println(angle);
    delay(delaylength);
    // finish
    
    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, LOW); //Sets direction of CH B and Moves CH B
    if (flag){
      flag = false;
      storeAnalogValue();
    }
    Serial.println(average1);// outputs real value as a byte
    //storeAnalogValue();
   currentstepnumber = ++stepnumber;
  angle = ((currentstepnumber)*360)/(stepsPerRevolution);
  Serial.println(angle);
    delay(delaylength);
    
    enableChannel(9, 8); // Enable CH A
    setPinDirection(12, 3, LOW); //Sets direction of CH A and Moves CH A
    if (flag){
      flag = false;
      storeAnalogValue();
    }
    Serial.println(average1);// outputs real value as a byte
    //storeAnalogValue();
    currentstepnumber = ++stepnumber;
  angle = ((currentstepnumber)*360)/(stepsPerRevolution);
  Serial.println(angle);
    delay(delaylength);
    
    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, HIGH); //Sets direction of CH B and Moves CH B
    Serial.println(average1);
    currentstepnumber = ++stepnumber;
  angle = ((currentstepnumber)*360)/(stepsPerRevolution);
  Serial.println(angle);
    storeAnalogValue();
    
    delay(delaylength);
    stopTime=millis();
}




}

What does analogRead() do?

Keep in mind that an int variable cannot exceed 32767. You will probably have to use longs to keep track of angle.

You attached a change interrupt to an output pin that you control? Don’t do that.

First make sure that only one function controls that pin. MotorMove() or something like that. Then each time you make the motor move, you know it moved and you can do whatever you were going to do when the flag was true.

Your storeAnalogValue() function is a bit of a mess. First it’s using global storage to store stuff that doesn’t need to be stored, it can easily overflow int integers, then it’s all hardcoded so you can’t easily change how many averages it does, then it is used like a function that returns a value but it’s a void.

Let’s attack the last problem first. You know you don’t have to call every function “void” don’t you? You can return a value so you can use it. Just like analgoRead() itself!

Then we can make the number of averages a constant at the top of the program and you can easily experiment with changing the number of averages. That won’t need any global storage either:

const int NumAverages = 6; //number of analogRead()s averaged together, should be between 1 and 4,000,000 (but that will be reaaallllly slow, try 6 as a good middle ground.)
int averagedAnalogRead(int PinNumber) {
  unsigned long accumulator = 0;
  for(int i=0; i<NumAverages; i++) {
    accumulator += analogRead(PinNumber);
  }
  return accumulator/NumAverages;
}

Hi Thank you vey much for the help, However when i try to run your correction i get an error saying i need a ; before the int averagedAnalogRead(0)

#include <Stepper.h>


float val; 
int analogPin = 1;
// The current address in the EEPROM (i.e. which byte we're going to write to next)
int address = 0;
int delaylength = 50; 
long startTime, stopTime, myDesiredTime;
byte value;
float realvalue = 0;
float realvalue1 = 0;
float realvalue2;
float realvalue3;
float realvalue4;
float realvalue5;
int average1;
void enableChannel(int pinA, int pinB) 

{
  digitalWrite(pinA, LOW);  //ENABLE CH A because brake is off so current passes through coil (set low)
  digitalWrite(pinB, HIGH); //DISABLE CH B because brake is on so current passes does not through coil (set high)
  
}

void setPinDirection(int pin, int direction_pin, int pin_level) // i.e. setPinDirection(12, 8, HIGH)
{
  digitalWrite(pin, pin_level);   //Sets direction of CH
  analogWrite(direction_pin, 255);   //Moves CH at speed max speed - 255(-255) spein backwards(100% duty cycle)
}

void setup() {
  Serial.begin(9600);
  startTime = 0;
  stopTime = 0;
  myDesiredTime = 20000; // In milliseconds

  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW 
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW 
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
  
}

/*void storeAnalogValue() {
 realvalue = analogRead(5);
 realvalue1=  analogRead(1)+realvalue; 
 realvalue2=  analogRead(1)+realvalue1;
 realvalue3=  analogRead(1)+realvalue2;
 realvalue4=  analogRead(1)+realvalue3;
 realvalue5= analogRead(1)+realvalue4;
 average1 =realvalue5/6;
 
}*/

const int NumAverages = 6; //number of analogRead()s averaged together, should be between 1 and 4,000,000 (but that will be reaaallllly slow, try 6 as a good middle ground.)
int averagedAnalogRead(0) {
  unsigned long accumulator = 0;
  for(int i=0; i<NumAverages; i++) {
    accumulator += analogRead(0);
  }
  return accumulator/NumAverages;
}


void loop(){
  //while( below)
  while (stopTime <= myDesiredTime){
    enableChannel(9, 8); // Enable CH A
   setPinDirection(12, 3, HIGH); //Sets direction of CH A and Moves CH A
   delay(delaylength);
   storeAnalogValue();
   Serial.println(accumulator/NumAverages);

    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, LOW); //Sets direction of CH B and Moves CH B
    delay(delaylength);
    storeAnalogValue();
    Serial.println(accumulator/NumAverages);
   
    
    enableChannel(9, 8); // Enable CH A
    setPinDirection(12, 3, LOW); //Sets direction of CH A and Moves CH A
    delay(delaylength);
   storeAnalogValue();
    Serial.println(accumulator/NumAverages);
 
    
    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, HIGH); //Sets direction of CH B and Moves CH B
    delay(delaylength);
    storeAnalogValue(); 
    Serial.println(accumulator/NumAverages);
    stopTime=millis();
}

}

You replaced int PinNumber with 0. The compiler knows that is an error.

It's a function with one input parameter - the pin number you wish to read. Much like analogRead(); Use it like analogRead().

thank you soo much! I have made the adjustments below and it seems to average the analog read() fine now. Now wit regard to how my motor would know where 0 degree or like 180 degree is in my code, how do you suggest i tackle that problem ?

#include <Stepper.h>


float val; 
int analogPin = 1;
// The current address in the EEPROM (i.e. which byte we're going to write to next)
int address = 0;
int delaylength = 50; 
long startTime, stopTime, myDesiredTime;
byte value;
float realvalue = 0;
float realvalue1 = 0;
float realvalue2;
float realvalue3;
float realvalue4;
float realvalue5;
int average;
const int NumAverages = 6;
 
int storeAnalogValue() {
  unsigned long accumulator = 0;
  for(int i=0; i<NumAverages; i++) {
    accumulator += analogRead(1);
    average= accumulator/NumAverages;
  }
  return average;
}


void enableChannel(int pinA, int pinB) 

{
  digitalWrite(pinA, LOW);  //ENABLE CH A because brake is off so current passes through coil (set low)
  digitalWrite(pinB, HIGH); //DISABLE CH B because brake is on so current passes does not through coil (set high)
  
}

void setPinDirection(int pin, int direction_pin, int pin_level) // i.e. setPinDirection(12, 8, HIGH)
{
  digitalWrite(pin, pin_level);   //Sets direction of CH
  analogWrite(direction_pin, 255);   //Moves CH at speed max speed - 255(-255) spein backwards(100% duty cycle)
}

void setup() {
  Serial.begin(9600);
  startTime = 0;
  stopTime = 0;
  myDesiredTime = 20000; // In milliseconds

  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW 
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW 
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
  
}


 /***
    Write the value to the appropriate byte of the EEPROM.
    these values will remain there when the board is
    turned off.
  ***/
 /*EEPROM.write(address, value);
  // Advance to the next address, when at the end restart at the beginning.
  address = address + 1;
  if (address == EEPROM.length()) {
    address = 0;
  }*/





void loop(){
  //while( below)
  while (stopTime <= myDesiredTime){
    enableChannel(9, 8); // Enable CH A
   setPinDirection(12, 3, HIGH); //Sets direction of CH A and Moves CH A
   delay(delaylength);
   storeAnalogValue();
   Serial.println(average);

    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, LOW); //Sets direction of CH B and Moves CH B
    delay(delaylength);
    storeAnalogValue();
    Serial.println(average);
   
    
    enableChannel(9, 8); // Enable CH A
    setPinDirection(12, 3, LOW); //Sets direction of CH A and Moves CH A
    delay(delaylength);
   storeAnalogValue();
    Serial.println(average);
 
    
    enableChannel(8, 9); // Enable CH B
    setPinDirection(13, 11, HIGH); //Sets direction of CH B and Moves CH B
    delay(delaylength);
    storeAnalogValue(); 
    Serial.println(average);
    stopTime=millis();
}

}

OK, thanks for posting the code, so I can see where you're up to.

You define this at the top of your code: int analogPin = 1; but you never use it anywhere! Also, you should always use the constants like A1 to choose analog pins. While analogRead(1) will do the same thing as analogRead(A1), digitalRead(A1) will do something different to digitalRead(1). Make a little section at the top of the code where you define your pins and then always use your names for the pins. analogRead(IRSensorPin) is much easier to debug one week from now, let alone one year from now.

You changed my function so it's almost unrecognisable. Your calculation of the average is incorrect, you've hardcoded the pin number so it won't work for any other pin and, most importantly, it doesn't do what its name says. You called it storeAnalogValue() but it returns a value. Changing the global variable average is considered to be a "side effect" of the function. Side effects are usually bad because calling a function to obtain a value should not normally change other values.

Your main loop is also weird. The very first line refers to a variable which is updated on the last line. Normally you would update the variable (read the input pin or whatever) then do the calculations and comparisons which use that value. It looks like you're using the value before you read the value.

Because you used a while(), there's no way of stopping this thing. After it does what it does for the time it does, then it's left in the last state - the motor is still driven to the same position. With a stepper, this is not too bad but for a regular motor, it would just continue running in whatever was the last direction. There's no code that allows it to stop. An if() {} else {} would have been better.

OK, to the problem you identified: what position is the motor in? Well, setup() should set some position tracker to zero, possibly even moving the motor itself to either a hard stop or a detector that detects the zero position. Then each time you take a step, with your awkwardly-named step function, add 1 to the counter. For a 200-step motor, stop when you have done 100 steps: that's 180 degrees.

The stepper.h library is pretty old and crappy. A better one might be AccelStepper.h. That can count the steps for you - you just have to ask it what the current motor position is.

I've asked the moderators to merge this with your other thread which seems to be asking the same question.

@uche_ofili7, do not cross-post. Threads merged.

@uche_ofili7, stop cross-posting. Threads merged.