Bipolar Stepper motor continuous rotation and still count steps

First post here, I am trying to have my bipolar stepper motor to run continuously while still knowing how many steps it has taken. My issue is that I need the arduino to do other things while the motor is running.

Quick overview of my project:
-Turn stepper motor on
-Every 25 steps turn RGB color sensor on
-When IR sensor go off, activate solenoid to open gate
-There are a total of 11 IR sensors (1 before and 1 after each gate) and 5 solenoids

I have just recieved the stepper motor, motor driver, and RGB color sensor.

So far ive written a for loop… for(stepCount=0; stepCount<25; stepCount++) …then i activated the RGB. This was just to test that everything works.

Ive attached a state machine diagram that i thought might work for my project, as you can tell… I need the stepper motor to run continuously. Any ideas on how to make it work? Arduino’s stepper library doesnt have a command that will work for this project.

state machine 1 color.pdf (506 KB)

If you are using a stepper driver that takes step and direction signals then have a look at the second example in this Simple Stepper Code. It could easily be adapted for continuous running. Every time you ask it to take a step just increment a variable to count the number of steps.

Or just use the Stepper library to make 1 step each time.


This is the program that i am running now

#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <Stepper.h>

const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

#define redpin 3
#define greenpin 5
#define bluepin 6

#define commonAnode true

byte gammatable[256];

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

int led_on_RGB = 52;

void setup() 
  {                                           // put your setup code here, to run once:

    if (tcs.begin()) 
      Serial.println("Found sensor");
      Serial.println("No TCS34725 found ... check your connections");
      while (1);                            // halt!

    pinMode(led_on_RGB, OUTPUT);
    pinMode(redpin, OUTPUT);
    pinMode(greenpin, OUTPUT);
    pinMode(bluepin, OUTPUT);

    for (int i=0; i<256; i++) 
        float x = i;
        x /= 255;
        x = pow(x, 2.5);
        x *= 255;
      if (commonAnode) 
          gammatable[i] = 255 - x;
          gammatable[i] = x;      

void loop() 
    unsigned char Step_Count;
    for( Step_Count=0; Step_Count<25; Step_Count++)
        Serial.println((int)Step_Count, DEC);
    uint16_t clear, red, green, blue;

    digitalWrite(led_on_RGB, HIGH);


    tcs.getRawData(&red, &green, &blue, &clear);

    digitalWrite(led_on_RGB, LOW);

    Serial.print("C:\t"); Serial.print(clear);
    Serial.print("\tR:\t"); Serial.print(red);
    Serial.print("\tG:\t"); Serial.print(green);
    Serial.print("\tB:\t"); Serial.print(blue);

    uint32_t sum = clear;
    float r, g, b;
    r = red; r /= sum;
    g = green; g /= sum;
    b = blue; b /= sum;
    r *= 256; g *= 256; b *= 256;
    Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);

    analogWrite(redpin, gammatable[(int)r]);
    analogWrite(greenpin, gammatable[(int)g]);
    analogWrite(bluepin, gammatable[(int)b]);

This is the program that i am running now

and ... ?

Does it do what you want?
How does it compare to the suggestions I have made?


What is the stepper driver you have and can you post a link to its spec/data please?

Thanks.. Tom.. :slight_smile: