Skip to content

Instantly share code, notes, and snippets.

@iDevPro
Last active November 6, 2017 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iDevPro/db27d5dc1c24b36c8b442bf5f718637a to your computer and use it in GitHub Desktop.
Save iDevPro/db27d5dc1c24b36c8b442bf5f718637a to your computer and use it in GitHub Desktop.
/*
Работа по:
созданию "мозгов" для джостика X52
Стадия:
подключены и готовы к эксплуатации 14 кнопок
Необходимо:
прописать оси
Фомин Юрий Александрович
04-11-2017
—----------------------------------------------------------------—
*/
#include <Joystick.h>
void setup() {
// инициализация пинов
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
/* pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(17, INPUT_PULLUP);
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);*/
// инициализация библиотеки джостика
Joystick.begin();
}
// Constant that maps the phyical pin to the joystick button.
// Константа, которая сопоставляет физический контакт с джойстика.
const int pinToButtonMap = 0;
// Last state of the button
// Последнее состояние кнопки
int lastButtonState[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
void loop() {
int analogR = analogRead(A0);
int analogR1 = analogRead(A1);
int analogM = map(analogR, 0, 1023, 0, 255);
int analogM1 = map(analogR1, 0, 1023, 0, 255);
analogM -= 160;
if (analogM < 0)
analogM = map(max(analogM, -120), -120, 0, -127, 0);
else if (analogM > 0)
analogM = map(analogM, 0, 94, 0, 127);
Joystick.setYAxis(analogM);
Joystick.setXAxis(map(analogM1 - 122, -123, 125, -127, 125));
// Read pin values
// Чтение значения пинов
for (int index = 0; index < 13; index++)
{
int currentButtonState = !digitalRead(index + pinToButtonMap);
if (currentButtonState != lastButtonState[index])
{
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}
// DEBUG INFO
// read the input on analog pin 0:
//int sensorValue = analogRead(A0);
//int sensorValue1 = analogRead(A1);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
//float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
//Serial.println(voltage);
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment