Encoder Arduino code using Arduino Nano
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const byte interrupt = 2;
const byte interruptB = 3;
const int encoder1PinA = 2; // encoder channel A,B (interupt pins)
const int encoder1PinB = 3;
float Pos1deg = 0;
volatile float encoder1Pos = 0; //ticks of encoder will change, initial number is zero
void setup()
{
Wire.setClock(400000);
// Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
//Serial.println(F("SSD1306 allocation failed"));
for (;
; // Don't proceed, loop forever
}
attachInterrupt(0, runEncoder1, RISING ); // encoder pin on interrupt 0 >>(pin 2 on arduino)
}
void loop()
{
float Pos1;
uint8_t oldSREG = SREG;
cli();
Pos1 = encoder1Pos;
SREG = oldSREG;
static int oldPos1;
if (Pos1 != oldPos1) {
oldPos1 = Pos1;
}
Bookmarks