Arduino improve articles 14- rocker button operation OLED

OLED joystick key operation

In many applications the rocker is used instead of touch keys, the only easy to operate, and a plurality of alternate rocker buttons, which makes the circuit design is simplified. Benpian demonstrates how four directions as four twisted rocker buttons to operate the OLED display.

1. Experimental Materials

  • Uno R3 Development Board
  • Supporting USB data cable
  • Bread plate and supporting cables
  • The biaxially module rocker keys
  • OLED display

2. Experimental Procedure

1. The schematic circuit diagram of a building.

VCC OLED screen, GND are connected to 3.3V development board, GND, SDA OLED screen SCL and A4 and A5 are connected to the development board. The biaxially rocker key module VCC, GND 5V development board are connected, GND, X-axis output module, Y-axis output boards are connected to the analog pins A0, A1.

Principle is shown below:

Experimental schematics

Physical connection is shown below:

Physical connection diagram

2. Create a new sketch, the following code replacement copies of the automatically generated code and save it.

/*
   OLED_Key
   摇杆按键操作OLED
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET     4
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);

#define pinX  A0
#define pinY  A1

int valueX = 0;
int valueY = 0;
unsigned char keyValue = 0;
int value = 0;


void menu(unsigned char index)
{
  display.clearDisplay();//清屏
  display.setCursor(20, 2);//设置显示位置
  display.println("--KEY--");
  display.setCursor(2, 35);//设置显示位置

  if (index == 1)
  {
    display.print("R:  [");
    value = value - 1;
  } else if (index == 2)
  {
    display.print("L:  [");
    value = value + 1;
  } else if (index == 3)
  {
    display.print("D:  [");
    value = value - 10;
  }
  else if (index == 4)
  {
    display.print("U:  [");
    value = value + 10;
  }

  if (value >= 100)
  {
    value = 100;
  }
  if (value <= 0)
  {
    value = 0;
  }
  display.print(value);
  display.print("]");
  display.display(); // 开显示

}

//按键扫描函数
unsigned char keyScan(void)
{
  static unsigned char keyUp = 1;

  valueX = analogRead(pinX);
  valueY = analogRead(pinY);

  if (keyUp && ((valueX <= 10) || (valueX >= 1010) || (valueY <= 10) || (valueY >= 1010)))
  {
    delay(10);
    keyUp = 0;
    if (valueX <= 10)return 1;
    else if (valueX >= 1010)return 2;
    else if (valueY <= 10)return 3;
    else if (valueY >= 1010)return 4;
  } else if ((valueX > 10) && (valueX < 1010) && (valueY > 10) && (valueY < 1010))keyUp = 1;
  return 0;
}

void setup()
{
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.setTextColor(WHITE);//开像素点发光
  display.clearDisplay();//清屏
  display.setTextSize(2); //设置字体大小
  display.setCursor(20, 2);//设置显示位置
  display.println("--KEY--");
  display.display(); // 开显示
}

void loop()
{
  keyValue = keyScan();

  if (keyValue != 0)
  {
    menu(keyValue);
  }
}

3. Development Board connector, and set the port number corresponding to the type of boards, for download.

Download

3. experimental results

Torsional rocker corresponding to four display marked L, R, U, D corresponding to the four directions and subtraction digital display.

Experimental phenomena

4. Program Analysis

The main part of the program key scanning function, we were collected X-axis and Y-axis input analog joystick select analog values ​​AD 1010 and 10 as two critical points, data collection twisting back when the rocker less than 10 or greater than 1010, this was considered an axial key actuation.

KeyValue static variable save button pressed, only when less than 10 re-enters the analog or greater than 1010 will trigger once again return key.

Focus on micro-channel public number: TonyCode
the Arduino learning exchange group: 868 283 450

More, I welcome the attention of the public number. Sweep the micro-channel to follow the Fanger Wei code:
Micro channel scan code added public number: TonyCode

Published 63 original articles · won praise 250 · Views 230,000 +

Guess you like

Origin blog.csdn.net/TonyIOT/article/details/103519899