0

I tried to sketch code to make an encoder to generate a number by each step and show it on OLED display. The OLED will connect with I2C, working on in Arduino Nano Every ATmega4809.

I separately made them for both encoder and OLED, they all work in separate code, encoder to generate Number and OLED display the text on screen.

But after combining them together, it seems to work only the OLED display. The encoder is not responding at all. The encoder will work again when I disable OLED.display() in loop section.

Anyone Please help me to them work together?

This is my sketch

//Display
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64 

// กำหนดขาต่อ I2C กับจอ OLED
#define OLED_RESET     -1 
Adafruit_SSD1306 OLED(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//Encoder
#define R_START 0x0
#define R_CW_FINAL 0x1
#define R_CW_BEGIN 0x2
#define R_CW_NEXT 0x3
#define R_CCW_BEGIN 0x4
#define R_CCW_FINAL 0x5
#define R_CCW_NEXT 0x6
#define DIR_NONE 0x0
#define DIR_CW 0x10
#define DIR_CCW 0x20

const byte pin1 = 10;  // DT
const byte pin2 = 11;  // CLK
int state = R_START;
int counter = 0;
int counter_ini = 0;
int potstep = 0;

const unsigned char ttable[7][4] = {
  // R_START
  {R_START,    R_CW_BEGIN,  R_CCW_BEGIN, R_START},
  // R_CW_FINAL
  {R_CW_NEXT,  R_START,     R_CW_FINAL,  R_START | DIR_CW},
  // R_CW_BEGIN
  {R_CW_NEXT,  R_CW_BEGIN,  R_START,     R_START},
  // R_CW_NEXT
  {R_CW_NEXT,  R_CW_BEGIN,  R_CW_FINAL,  R_START},
  // R_CCW_BEGIN
  {R_CCW_NEXT, R_START,     R_CCW_BEGIN, R_START},
  // R_CCW_FINAL
  {R_CCW_NEXT, R_CCW_FINAL, R_START,     R_START | DIR_CCW},
  // R_CCW_NEXT
  {R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START},
};

unsigned char process(){

  // Grab state of input pins.
  unsigned char pinstate = (digitalRead(pin2) << 1) | digitalRead(pin1);

  // Determine new state from the pins and state table.
  state = ttable[state & 0xf][pinstate];

  // Return emit bits, ie the generated event.
  return state & 0x30;
}

void setup(){
  pinMode(pin1, INPUT_PULLUP);
  pinMode(pin2, INPUT_PULLUP);
  Serial.begin(9600);

  //Display
  if (!OLED.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println("SSD1306 allocation failed");
  } else {
    Serial.println("ArdinoAll OLED Start Work !!!");
  }
OLED.clearDisplay();
OLED.setTextSize(2);
OLED.setTextColor(WHITE);
OLED.setCursor(5, 10);
OLED.println("   TEST   ");
OLED.println();
  OLED.display();
  delay(5000);
}

void loop(){

  unsigned char result = process();
  if(result == DIR_CW){
    counter++;
    //Serial.println(counter);
  }
  else if(result == DIR_CCW){
    counter--;
    //Serial.println(counter);
  }
  if(counter > counter_ini){
    potstep++;
    Serial.println(potstep);
    counter_ini=counter;
  }
  else if(counter < counter_ini){
    potstep--;
    Serial.println(potstep);
    counter_ini=counter;
  }
  if(potstep > 255){
    potstep = 255;
    //Serial.println(potstep);
   
  }
  else if(potstep < 0){
    potstep = 0;
    //Serial.println(potstep);
  }
//Display
    OLED.clearDisplay();
    OLED.drawRect(3,3,125,47,WHITE);
    OLED.setTextSize(4);
    OLED.setTextColor(WHITE, BLACK);
    OLED.setCursor(27,12);
    OLED.println(potstep);

    OLED.display();
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.