I'm using an ssd1306 OLED Display, connected up to a Mega2560,
http://docs-europe.electrocomponents.com/webdocs/008d/0900766b8008d78a.pdf
Fritzing, the encoder used in this drwing for demonstration
Currently this is the full code I'm using.
If I move the encoder CW "encoderPos" will display from 0 and counts up, much the same if I start from 0 and move the encoder CCW it will count up but with the - sign next to the number.
If I change "int encoderPos = 0;" to "float encoderPos = 0;"
encoderPos is displayed as 0.0000000000, when I move the encoder CW the number will change from, 0.0000000000 to 1.0000000000 to 2.0000000000 to 3.0000000000 and so on, much the same as if I turn the encoder CCW from 0.0000000000. The number will change from down -0.0000000000 to -1.0000000000 to -2.0000000000 to -3.0000000000 and so on.
How can I display the number like this, starts from 0.000, then moves up like this CW from the encoder to 0.001 to 0.002 ro 0.003 and so on, also the same CWW from 0.000 to -0.001 to 0.002 to 0.003 and so on. Also I would like to scale the counts from the encoder too, I really want to learn how to address the above as I'm very new to this.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_DC 48
#define OLED_CS 52
#define OLED_CLK 46
#define OLED_MOSI 44
#define OLED_RESET 50
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#if (SSD1306_LCDHEIGHT != 32)
#endif
enum PinAssignments {
encoderPinA = 20,
encoderPinB = 21,
zeroButton = 42
};
int encoderPos = 0;
int lastReportedPos = 0;
boolean A_set = false;
boolean B_set = false;
void setup() {
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
pinMode(zeroButton, INPUT);
digitalWrite(encoderPinA, HIGH);
digitalWrite(encoderPinB, HIGH);
digitalWrite(zeroButton, HIGH);
attachInterrupt(2, doEncoderA, CHANGE);
attachInterrupt(3, doEncoderB, CHANGE);
display.begin(SSD1306_SWITCHCAPVCC);
display.display();
delay(1000);
}
void loop(){
if (lastReportedPos != encoderPos) {
lastReportedPos = encoderPos;
}
if (digitalRead(zeroButton) == LOW) {
encoderPos = 0;
}
display.display();
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("Linear");
display.setCursor(0,16);
display.print("Position");
display.setCursor(0,40);
display.setTextSize(3);
display.print(encoderPos, DEC);
display.setTextSize(2);
display.setCursor(0,24);
display.print("__________");
display.setTextSize(2);
display.setCursor(96,47);
display.print("mm");
display.setTextSize(2);
display.setCursor(0,50);
display.print("__________");
}
void doEncoderA(){
A_set = digitalRead(encoderPinA) == HIGH;
encoderPos += (A_set != B_set) ? +1 : -1;
}
void doEncoderB(){
B_set = digitalRead(encoderPinB) == HIGH;
encoderPos += (A_set == B_set) ? +1 : -1;
}