Hi. I'm pretty new to all of this. Here's the story:
I'm building an Arduino driven pickup winder. The idea of the design is to sync a stepper to a separate spinning driveshaft.
I have a DC motor turning shaft with faceplate on the end of it. I'm using an IR LED and a photo transistor that reads a painted black and white cross to monitor the speed and number of turns of the driveshaft. I have an attachInterrupt that tells the stepper to advance 1 increment each time the white and black transitions. The average working speed of the DC motor will be about 1000rpm
Originally, I built the machine using an Easy Driver, and it worked. But the stepper wasn't quite strong enough to reliably turn the gearbox I built for the machine. So I got a 2A stepper and a Big Easy Driver.
What happened was really strange - now the optical transistor is seems to be sensing each transition from both light to dark and dark to light firing the attachInterrupt twice as often as it should! When I disconnect the Big Easy Driver, my optical sensor works perfectly.
Anyway here's my current code I managed to cobble together. The only difference between the two is that I added the MS3 pin for 1/16 steps.
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
const int buttonPin = 2;
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
#define step_pin 9 // Pin 9 connected to Steps pin on EasyDriver
#define dir_pin 8 // Pin 8 connected to Direction pin
#define MS1 10 // Pin 10 connected to MS1 pin
#define MS2 11 // Pin 11 connected to MS2 pin
#define MS3 13
#define SLEEP 12 // Pin 12 connected to SLEEP pin
//int ledPin = 13;
volatile byte windState = 0; // varible that will hopefully advance the stepper
volatile byte quarters; // Counts pulses for the tach
volatile long sampleCount = 0; // Raw count data
unsigned int windCount = 0; // the actual count
long previousCount_Millis = 0; // Stores the last time Wind Count was updated
long count_interval = 25; // Interval at which the Wind Count is updated
long previousRPM_Millis = 0; // Stores the last time RPM was updated
long rpm_interval = 400; // Interval at which the RPM is updated
long previousLCD_Millis = 0; // Stores the last time LCD was updated
long lcd_interval = 20; // Interval at which the LCD is updated
long previousCount = 0; // Stores the old count for the easydriver
long stepperInterval = 10;
void sample()
{
quarters++;
sampleCount++;
}
void setup()
{
attachInterrupt(0, winder, FALLING);
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(MS3, OUTPUT);
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(SLEEP, OUTPUT);
digitalWrite(SLEEP, HIGH);
delay(5);
/* Configure type of Steps on EasyDriver:
// MS1 MS2
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/
digitalWrite(MS1, HIGH);
digitalWrite(MS2, LOW);
digitalWrite(MS3, LOW);
Serial.begin(9600);
display.begin();
// init done
// you can change the contrast around to adapt the display
// for the best viewing!
display.setContrast(40);
display.clearDisplay();// clears the screen and buffer
display.display();
}
void loop()
{
quarters = 0;
unsigned int rpm_temp = 0;
unsigned int rpm = 0;
unsigned long timeOld = 0;
while (1) {
unsigned long current_Millis = millis();
if (quarters >= 37) {
// Update rpm_temp every 37 counts
rpm_temp = 60000 * quarters / (4 * (current_Millis - timeOld));
timeOld = current_Millis;
quarters = 0;
}
// compare current time with the last time the RPM was updated
if (current_Millis - previousRPM_Millis > rpm_interval) {
rpm = rpm_temp;
previousRPM_Millis = current_Millis;
}
// compare current time with the last time the Wind Count was updated
if (current_Millis - previousCount_Millis > count_interval) {
windCount = (sampleCount / 4);
previousCount_Millis = current_Millis;
}
// compare current time with the last time the LCD was updated
if (current_Millis - previousLCD_Millis > lcd_interval) {
display.clearDisplay();
display.setTextColor(BLACK);
display.setCursor(0, 5);
display.setTextSize(1);
display.print("RPM: ");
display.print(rpm);
display.setCursor(0, 20);
display.setTextSize(1);
display.print("WIND COUNT:");
display.setCursor(0, 30);
display.setTextSize(2);
display.print(windCount);
display.display();
}
}
}
void winder() {
windState = digitalRead(buttonPin);
digitalWrite(dir_pin, LOW); // HIGH = anti-clockwise / LOW = clockwise
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
sample();
}


