Skip to content

Instantly share code, notes, and snippets.

@soffer
Created September 3, 2012 10:51
Show Gist options
  • Select an option

  • Save soffer/3608510 to your computer and use it in GitHub Desktop.

Select an option

Save soffer/3608510 to your computer and use it in GitHub Desktop.
making progress 31.8.12
#include <AccelStepper.h>
#include <OneButton.h>
//encoder/motor/driver setup
int easyDriverMicroSteps = 1; //I think this is 4 or 8
int rotaryEncoderSteps = 75; //setps per rev on your encoder
int motorStepsPerRev = 200; //this is right - steps per rev on the motor
int MinPulseWidth = 50; //too low and the motor will stall, too high and it will slow it down
//Encoder pins (cannot be changed 2/3 are special pins)
int encoderPin1 = 2;
int encoderPin2 = 3;
//Easy Driver pins
int easyDriverStepPin = 4;
int easyDriverDirPin = 5;
//Encoder Values
volatile int lastEncoded = 0;
volatile int encoderValue = 0;
int lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
//1 motor driver with the library
AccelStepper stepper(1, easyDriverStepPin, easyDriverDirPin);
//LEDs
#define realTimeLED 10 //Real Time LED
#define playLED 11 //PLay LED
#define inLED 12 //In LED
#define outLED 13 //Out LED
// Setup a new OneButton on pin A1.
OneButton realTimebutton(A1, true);
OneButton playButton (A2, true);
OneButton inButton (A3, true);
OneButton outButton (A4, true);
//Blink without delay
int ledState = LOW;
long previousMillis = 0;
long ledInterval = 75;
//Modes
int mode;
#define RealTime 1
#define STOP 2
//Values for focus points
int inPoint;
int outPoint;
boolean clickedOnce = false;
void setup(){
Serial.begin(115200);
Serial.flush();
stepper.setMinPulseWidth(MinPulseWidth);
stepper.setMaxSpeed(50000);
stepper.setAcceleration(1000000000);
stepper.setSpeed(50000);
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
//Attach Click to Buttons
realTimebutton.attachClick(Click);
playButton.attachClick(ClickPlay);
inButton.attachClick(ClickIn);
outButton.attachClick(ClickOut);
//Attach DoubleClick to Real Time
realTimebutton.attachDoubleClick(doubleclick);
pinMode (realTimeLED, OUTPUT);
pinMode (playLED, OUTPUT);
pinMode (inLED, OUTPUT);
pinMode (outLED, OUTPUT);
digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
//call updateEncoder() when any high/low changed seen
//on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
void loop(){
// keep watching the push buttons:
realTimebutton.tick();
playButton.tick();
inButton.tick();
outButton.tick();
// Defining Cases
switch (mode)
{
case RealTime:
{
digitalWrite(realTimeLED, HIGH);
int stepsPerRotaryStep = (motorStepsPerRev * easyDriverMicroSteps) / rotaryEncoderSteps;
stepper.moveTo(encoderValue * stepsPerRotaryStep);
stepper.run();
if(encoderValue != lastencoderValue)
{
lastencoderValue = encoderValue;
Serial.println(encoderValue);
}
}
break;
case STOP:
digitalWrite(realTimeLED, LOW);
clickedOnce = !clickedOnce;
Serial.println ("Stop");
}
}
//Functions
void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
//Limiting value range
if (encoderValue>2000){
encoderValue = 2000;
}
if (encoderValue< -2000){
encoderValue = -2000;
}
lastEncoded = encoded; //store this value for next time
}
//4 Buttons Click Functions
void Click() {
if (clickedOnce == false)
{
clickedOnce = !clickedOnce;
mode = RealTime;
}
else
mode = STOP;
}
void ClickPlay () {
Serial.println ("Play Click");
}
void ClickIn () {
Serial.print ("In Value = ");
inPoint = encoderValue;
Serial.println (inPoint);
static int b = HIGH;
for (int i=0;i<6;i++)
{
digitalWrite(inLED, b);
delay(75);
b=!b;
}
}
void ClickOut() {
Serial.print ("Out Value = ");
outPoint = encoderValue;
Serial.println (outPoint);
static int b = HIGH;
for (int i=0;i<6;i++)
{
digitalWrite(outLED, b);
delay(75);
b=!b;
}
}
// Doubleclick Function
void doubleclick() {
Serial.println ("realTime doubleClick");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment