• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Wykorzystanie encodera z myszki do budowy prostej kierownicy do gier
#1
Photo 
Witam,
A wiec tak wpadlem na taki pomysł by wykorzystać encoder z rolki do przewijania tekstu z myszki jako element sterujacy do gier np. ets i samochodówki (kierownica do gier). No i o ile poradziłem sobie od strony mechanicznej to już gorzej z kodem. Znalazłem taki kod służący do sterowania głośnością góra-dół, który robi prawie to co chce tylko nie wiem czym zastąpić Vol up/down tak żeby ta kierownica skręcała. Próbowałem keykode arow left/raight kompilowało sie to bez błędów ale nie reagowało kierownica nie skręcała. W TrinketHidCombo są jeszcze jakieś funkcje sterujące myszką no ale nie wiem jak ich użyć. 
Tak mam to podłączone do digisparka

[Obrazek: F944APDIH6O5C98.LARGE.jpg?auto=webp&frame=1&fit=bounds]
A taki kod znalazłem na necie, który trzeba zmodyfikować
Kod:
// see tutorial at http://learn.adafruit.com/trinket-usb-volume-knob

#include "TrinketHidCombo.h"

#define PIN_ENCODER_A 0
#define PIN_ENCODER_B 2
#define TRINKET_PINx  PINB

static uint8_t enc_prev_pos = 0;
static uint8_t enc_flags    = 0;

void setup()
{
  // set pins as input with internal pull-up resistors enabled
  pinMode(PIN_ENCODER_A, INPUT);
  pinMode(PIN_ENCODER_B, INPUT);
  digitalWrite(PIN_ENCODER_A, HIGH);
  digitalWrite(PIN_ENCODER_B, HIGH);

  TrinketHidCombo.begin(); // start the USB device engine and enumerate

  // get an initial reading on the encoder pins
  if (digitalRead(PIN_ENCODER_A) == LOW) {
    enc_prev_pos |= (1 << 0);
  }
  if (digitalRead(PIN_ENCODER_B) == LOW) {
    enc_prev_pos |= (1 << 1);
  }
}

void loop()
{
  int8_t enc_action = 0; // 1 or -1 if moved, sign is direction

  // note: for better performance, the code will now use
  // direct port access techniques
  // http://www.arduino.cc/en/Reference/PortManipulation
  uint8_t enc_cur_pos = 0;
  // read in the encoder state first
  if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_A)) {
    enc_cur_pos |= (1 << 0);
  }
  if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_B)) {
    enc_cur_pos |= (1 << 1);
  }

  // if any rotation at all
  if (enc_cur_pos != enc_prev_pos)
  {
    if (enc_prev_pos == 0x00)
    {
      // this is the first edge
      if (enc_cur_pos == 0x01) {
        enc_flags |= (1 << 0);
      }
      else if (enc_cur_pos == 0x02) {
        enc_flags |= (1 << 1);
      }
    }

    if (enc_cur_pos == 0x03)
    {
      // this is when the encoder is in the middle of a "step"
      enc_flags |= (1 << 4);
    }
    else if (enc_cur_pos == 0x00)
    {
      // this is the final edge
      if (enc_prev_pos == 0x02) {
        enc_flags |= (1 << 2);
      }
      else if (enc_prev_pos == 0x01) {
        enc_flags |= (1 << 3);
      }

      // check the first and last edge
      // or maybe one edge is missing, if missing then require the middle state
      // this will reject bounces and false movements
      if (bit_is_set(enc_flags, 0) && (bit_is_set(enc_flags, 2) || bit_is_set(enc_flags, 4))) {
        enc_action = 1;
      }
      else if (bit_is_set(enc_flags, 2) && (bit_is_set(enc_flags, 0) || bit_is_set(enc_flags, 4))) {
        enc_action = 1;
      }
      else if (bit_is_set(enc_flags, 1) && (bit_is_set(enc_flags, 3) || bit_is_set(enc_flags, 4))) {
        enc_action = -1;
      }
      else if (bit_is_set(enc_flags, 3) && (bit_is_set(enc_flags, 1) || bit_is_set(enc_flags, 4))) {
        enc_action = -1;
      }

      enc_flags = 0; // reset for next time
    }
  }

  enc_prev_pos = enc_cur_pos;

  if (enc_action > 0) {
    TrinketHidCombo.pressMultimediaKey(MMKEY_VOL_UP);
  }
  else if (enc_action < 0) {
    TrinketHidCombo.pressMultimediaKey(MMKEY_VOL_DOWN);
  }
  else {
    TrinketHidCombo.poll(); // do nothing, check if USB needs anything done
  }
}

Próbowałem też czegoś takiego zmodyfikowałem kod wysyłający pozycje enkodera do serial no ale wciąż nie reaguje domyślam się że problem z odczytem pozycji enkodera jest jakiś inny typ czy coś ale kompiluje się to:

#include <DigiMouse.h>
int val;
int encoder0PinA = 0;
int encoder0PinB = 0;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

void setup() {
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
digitalWrite(encoder0PinA, HIGH);
digitalWrite(encoder0PinB, HIGH);
DigiMouse.begin();

}

void loop() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos--;
} else {
encoder0Pos++;
}

DigiMouse.moveY(encoder0Pos);
}
encoder0PinALast = n;
}
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości