Pete I hope this helps
Note I only use one of the phases of the encoder i.e. pin 3
/16/3/17
Moving the carraige a set distance
about line 164 while (counter <= 2052); //205.2/mm
/
//*****************
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SSD1306_LCDHEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//**********************************
volatile unsigned int counter = 0;
volatile unsigned int counter1 = 0;
volatile unsigned int counterEnd = 0;
const int encoder0PinA = 11;
const int encoder0PinB = 12;
const int encoder1PinA = 2;
const int encoder1PinB = 3;
const int FSTOP = 5; //Front Stop
const int AIN1 = 9; //rotate Low Forward
const int AIN2 = 8; //Motor Engaged High dis-engauge
const int START = 7; //Start Button
int speed = 10;
int encoder0PinALast = LOW;
int n = LOW;
int i;
int rotate; // 1 = forward 3 Backward
int xstart = 2000;
int xrun = xstart;
int pausetime = 0;
int errocode;
unsigned long time1;
unsigned long time;
void stop()
{
digitalWrite(AIN2, HIGH);// Motor dis-engauged
//delayMicroseconds (50);
// Serial.println("stopped");
}
void accelerate () {
// Serial.println ("accelerate");
i = xstart ;
while (i< xrun)
{
i = i +100;
tone(speed,i);
//delayMicroseconds(4);
//Serial.println (i);
}
}
//*******************************
void backward()
{
//Serial.println("backward");
rotate = 3;
digitalWrite(AIN1, HIGH);// backward
digitalWrite(AIN2, LOW);// Motor engauged
accelerate();
}
//*******************************
void forward()
{
// Serial.println("forward");
rotate = 1;
digitalWrite(AIN1,LOW); //forward
digitalWrite(AIN2, LOW);// Motor engauged
accelerate();
}
//*******************************
void error()
{
//Serial.print(" Erro Code = ");
//Serial.println(errocode);
}
//*******************************
void readspeed(){
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
xrun = xrun - 100;
}
else
{
xrun = xrun + 100;
}
}
if( xrun < 500){
xrun = 500;
}
encoder0PinALast = n;
}
//*******************************
void setup()
{
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
// init done
display.display();// to displace the Adfruit logo that is embeded
display.clearDisplay();
Serial.begin(9600); // initialize serial communication at 9600 bits per second:
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(START, INPUT);
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
pinMode(speed,OUTPUT);
pinMode(encoder1PinA, INPUT); // set pin to input
pinMode(encoder1PinB, INPUT); // set pin to input
digitalWrite(encoder1PinA, HIGH); // turn on pullup resistors
digitalWrite(encoder1PinB, HIGH); // turn on pullup resistors
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr encoder1PinA on moust Arduino.
// attachInterrupt(1, ai0, RISING );
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr encoder1PinB on moust Arduino.
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr encoder1PinB on moust Arduino.
attachInterrupt(digitalPinToInterrupt(encoder1PinB), ai1, RISING );// pin 3
displayscreen();
}
void loop() {
// Serial.print ("Forward = ");
// Serial.println (counter1);
// Serial.print ("Backwards = ");
//Serial.println (counter);
//Serial.println (counter1 - counter);
counter = 0;
counter1 = 0;
digitalWrite(AIN2, HIGH);// Motor dis-engauged
Serial.println("Ready PRESS START ");
while (digitalRead(START) == LOW )//pin 7
{
readspeed();
displayscreen();
}
Serial.println("Starting");
Serial.println (counter);
pausetime = 500;
pause();//to stop bounce
do
{
forward();
// backward();
tone(speed,xrun);
}
while (counter <= 61200); //102.6/mm
stop();
Serial.println (counter);
counter1 = counter ;
counter = 0;
//Serial.println (counter1);
while (digitalRead(START) == LOW )//pin 7
pausetime = 500;
pause();//to stop bounce
backward();
tone(speed,xrun);
do
{
//Nothing
}
while (digitalRead(FSTOP) == LOW);//pin 7
stop();
// pausetime = 400;
//pause();//to stop bounce
}
//end of loop
void ai1() {
//Serial.println ("ai1");
// ai0 is activated if DigitalPin nr encoder1PinB is going from LOW to HIGH
// Check with pin encoder1PinA to determine the rotate
counter = ++counter;
//Serial.println (counter);
}
void pause(){
time1 = millis();
do{
// nothing
} while (millis() - time1 < pausetime);
}
void displayscreen(){
display.clearDisplay();
display.setTextSize(2);
display.setCursor(6,15);
display.setTextColor( WHITE);
display.println(xrun);
display.display();
//Serial.println (xrun);
}