I 'm using Arduino Nano, display 1603 ( 128x32 oled ) and epson printer optical encoder for my project ( 4 pins ). I'm trying to make distance measurement in centimeter whenever the optical disk rotate clockwise , there will be a positive measurement, and the other around counterclockwise , there will be a negative measurement. 1 rotation of the disk = 1.0 cm distance. and there will be a sensor reset, if the sensor touches the switch, the measurement will be 10cm ( reset ).
Can anyone help me to write the code for these project. thanks before.
Can we believe you have the code working to display on the 1603? That is the very first step!!!
Do you have the 4 pins sorted out, so you know what each is connected to? And what Arduino pins are involved?
pin D12 ChA and pinD11 ChB
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define TRIG_PIN 12 // The Arduino Nano pin connected to TRIG pin of ultrasonic sensor
#define ECHO_PIN 11 // The Arduino Nano pin connected to ECHO pin of ultrasonic sensor
#define OLED_WIDTH 128 // OLED display width, in pixels
#define OLED_HEIGHT 32 // OLED display height, in pixels
Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
String distance_str;
void setup() {
Serial.begin(9600);
// initialize OLED display with address 0x3C for 128x32
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(3); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display
distance_str.reserve(10); // to avoid fragmenting memory when using String
}
void loop() {
// Produce a 10-microsecond pulse to the TRIG pin.
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the pulse duration from the ECHO pin
long duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance
float distance_cm = 0.017* duration_us;
distance_str = String(distance_cm, 1); // one decimal places
distance_str += "cm";
oled_display_center(distance_str); // display measurment on OLED
}
void oled_display_center(String text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
// center the display both horizontally and vertically
oled.clearDisplay(); // clear display
oled.setCursor((OLED_WIDTH - width) / 1, (OLED_HEIGHT - height) / 1);
oled.println(text); // text to display
oled.display();
}
I also have an Epson printer, but that doesn't mean I know exactly how its sensors work. For example, how do you understand the direction of rotation? If it's from the sensor itself and the signal is quadratic like rotary encoders, you can try treating it as such with one of the many libraries and using the number of pulses to calculate the distance.
The Arduino Encoder library is used with AB quadrature encoders to keep track of shaft position, and works well.

