Загрузка данных


// ==============================
// 8 магнитных датчиков
// ==============================

const int sensorPins[] = {
  2, 3, 4, 5, 6, 7, 8, 9
};

const int sensorCount = 8;


// ==============================
// Джойстик
// ==============================

#define VRX A0
#define VRY A1
#define SW 10


// ==============================
// Предыдущие состояния
// ==============================

bool lastSensorState[8] = {
  false, false, false, false,
  false, false, false, false
};

bool lastLeft = false;
bool lastRight = false;
bool lastUp = false;
bool lastDown = false;
bool lastPress = false;


// ==============================
// SETUP
// ==============================

void setup() {

  Serial.begin(9600);


  // Магнитные датчики
  for (int i = 0; i < sensorCount; i++) {

    pinMode(sensorPins[i], INPUT);

  }


  // Кнопка джойстика
  pinMode(SW, INPUT_PULLUP);

}


// ==============================
// LOOP
// ==============================

void loop() {


  // =====================================
  // 8 МАГНИТНЫХ ДАТЧИКОВ
  // =====================================

  for (int i = 0; i < sensorCount; i++) {

    bool sensorState =
      digitalRead(sensorPins[i]) == HIGH;


    // Магнит появился
    if (sensorState && !lastSensorState[i]) {

      Serial.print("сигнал_");
      Serial.println(sensorPins[i]);

    }


    lastSensorState[i] = sensorState;

  }



  // =====================================
  // ДЖОЙСТИК
  // =====================================

  int x = analogRead(VRX);
  int y = analogRead(VRY);


  bool left =
    x < 300;

  bool right =
    x > 700;


  bool up =
    y > 700;

  bool down =
    y < 300;


  bool press =
    digitalRead(SW) == LOW;



  // =====================================
  // ВЛЕВО
  // =====================================

  if (left && !lastLeft) {

    Serial.println("RIGHT");

  }



  // =====================================
  // ВПРАВО
  // =====================================

  if (right && !lastRight) {

    Serial.println("LEFT_ON");

  }


  if (!right && lastRight) {

    Serial.println("LEFT_OFF");

  }



  // =====================================
  // ВВЕРХ
  // =====================================

  if (up && !lastUp) {

    Serial.println("UP_ON");

  }


  if (!up && lastUp) {

    Serial.println("UP_OFF");

  }



  // =====================================
  // ВНИЗ
  // =====================================

  if (down && !lastDown) {

    Serial.println("DOWN");

  }



  // =====================================
  // НАЖАТИЕ ДЖОЙСТИКА
  // =====================================

  if (press && !lastPress) {

    Serial.println("PRESS");

  }



  // =====================================
  // Сохраняем состояния
  // =====================================

  lastLeft = left;
  lastRight = right;
  lastUp = up;
  lastDown = down;
  lastPress = press;


  // Небольшая задержка
  delay(20);

}