#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST);
// Joystick
bool joystickPress = false;
// Options
bool useBuzzer = true;
void setup()
{
// Joystick
pinMode(2, INPUT);
digitalWrite(2, HIGH);
Serial.begin(115200);
// Buzzer
if (useBuzzer)
Serial.begin(9600);
// Screen
u8g.setFont(u8g_font_tpssb);
u8g.setColorIndex(1);
}
void loop()
{
// Joystick
float joystickX = getJoystickX();
float joystickY = getJoystickY();
bool joystickPressed = getJoystickPressed();
// Screen
u8g.firstPage();
do
{
u8g.setPrintPos(5, 15);
u8g.print("Pressed: ");
u8g.print(joystickPressed ? "Yes" : "No");
u8g.setPrintPos(5, 30);
u8g.print("X: ");
u8g.print(joystickX);
u8g.setPrintPos(5, 45);
u8g.print("Y: ");
u8g.print(joystickY);
}
while ( u8g.nextPage() );
// Click
if (joystickPress != joystickPressed)
{
if (joystickPress == false)
click();
joystickPress = joystickPressed;
}
}
void click()
{
// Buzzer
if (useBuzzer)
tone(11, 20, 100);
}
float getJoystickX() { return 0 - (analogRead(0) / 1023.0 * 2 - 1); }
float getJoystickY() { return analogRead(1) / 1023.0 * 2 - 1; }
bool getJoystickPressed() { return digitalRead(2) == 0 ? true : false; }