Skocz do zawartości

Regulacja głośności na Arduino Pro Micro


ichigo1987

Pomocna odpowiedź

Witam,

jestem w trakcie budowy sterownika na atmedze pro micro do mojej stacji multimedialnej , ale niestety po wgraniu skryptu nie działa prawidłowo. Mianowicie "przyciski" są wciskane losowo bez mojej ingerencji pomimo podłączenia według schematu. Potencjometr czasem działa ale prawdopodobnie przez wysyłanie ciągłej akcji jest blokowany. Wykonałem wszystko z poniższego tutoriala na płytce do prototypownia. Jako że korzystam przeważnie z gotowych rozwiązań (skryptów) to ciężko mi zdiagnować co jest nie tak ze skryptem i proszę o pomoc.

Poniżej wstawiam cały kod oraz link do filmiku jak został skonstruowany sterownik

Link do filmu jak skonstruować sterownik

//
//USB Rotary Volume Control
//

#include "HID-Project.h"

// Define Arduino pin numbers for buttons and LEDs
#define led 13
int buttonPina = 15;
int buttonPinb = 14;
int buttonPinc = 16;
int buttonPind = 10;
#define ROTARY_A 5
#define ROTARY_B 6
#define ROTARY_C 7

boolean A, a, B, b, C, c;
char line[80];
int n, t;

void setup() {
  Serial.begin(38400);
  Serial.write("Starting...\n");
  Serial.end();
  pinMode(ROTARY_A, INPUT_PULLUP);
  pinMode(ROTARY_B, INPUT_PULLUP);
  pinMode(ROTARY_C, INPUT_PULLUP);
  pinMode(buttonPina, INPUT_PULLUP);
  pinMode(buttonPinb, INPUT_PULLUP);
  pinMode(buttonPinc, INPUT_PULLUP);
  pinMode(buttonPind, INPUT_PULLUP);

  a = b = c = false;
  t = 0;
  n = 0;
  Consumer.begin();
  Keyboard.begin();
  // BootKeyboard.begin();
  //  System.begin(); // For System functions
  //  Gamepad.begin(); // For gamepad functions
  //  Mouse.begin(); // For mouse functions
  //  AbsoluteMouse.begin(); // For the Absolute Mouse

}

void loop() {

  if (millis() + 5 > t) {
    A = digitalRead(ROTARY_A) == LOW;
    B = digitalRead(ROTARY_B) == LOW;
    C = digitalRead(ROTARY_C) == LOW;
    if (A && !a) {
      if (B) {
        n++;
        Consumer.write(MEDIA_VOL_UP);
      }
      else {
        n--;
        Consumer.write(MEDIA_VOL_DOWN);
      }
      //      sprintf(line, "%d\n", n);
      //      Serial.write(line);
    }
    a = A;

    if (C && ! c) {
      Consumer.write(MEDIA_VOL_MUTE);
      delay(500);
      //      sprintf(line, "!\n");
      //      Serial.write(line);
    }

  }

  if (digitalRead(buttonPina))
  {
    Keyboard.write(KEY_F7);
    delay(500);
  }

  if (digitalRead(buttonPinb))
  {
    Keyboard.write(KEY_F8);
    delay(500);
  }

  if (digitalRead(buttonPinc))
  {
    Keyboard.write(KEY_F9);
    delay(500);
  }

  if (digitalRead(buttonPind))
  {
    Keyboard.write(KEY_F10);
    delay(500);
  }

}

 

Link do komentarza
Share on other sites

Dnia 20.12.2019 o 11:28, ichigo1987 napisał:

Mianowicie "przyciski" są wciskane losowo bez mojej ingerencji pomimo podłączenia według schematu

Pokaż ten schemat i swoją zlutowaną wersję to łatwiej będzie pomóc 🙂

Link do komentarza
Share on other sites

W sumie to już sam uporałem się z problemem zmieniłem tylko trochę kod udostępniony na yt i działa bez problemu. Jak kogoś interesuje to zamieszczam kod i link do wideo na yt

// Rotary Encoder
#include "ESPRotary.h";
#define ROTARY_PIN_1 4
#define ROTARY_PIN_2 3
int moves_per_click = 2;
ESPRotary r = ESPRotary(ROTARY_PIN_1, ROTARY_PIN_2, moves_per_click) ;
int old_pos = 0;
int new_pos = 0;

// Keyboard Library
#include "HID-Project.h"

// keys
int keypadPins[9] = {2, 15, 10, 16, 6, 14, 8, 7, 9};
int keypadStatus;  // Used to monitor which buttons are pressed.
int timeout;  // timeout variable used in loop
int UpDownArr, SideArr = 0;
int MainVol = 1;

void setup() {
  Serial.begin(115200);
  // Rotate
  r.setChangedHandler(rotate);
  for (int i = 0; i < 9; i++) {
    pinMode(keypadPins[i], INPUT);  // Set all keypad pins as inputs
    digitalWrite(keypadPins[i], HIGH);  // pull all keypad pins high
  }
  Consumer.begin();
}

void loop() {
  r.loop();
  keypadStatus = getKeypadStatus();  // read which buttons are pressed
  if (keypadStatus != 0) {
    sendKeyPress(keypadStatus);  // send the button over USB
    timeout = 2000;  // top of the repeat delay
    while ((keypadStatus == getKeypadStatus()) && (--timeout))  // Decrement timeout and check if key is being held down
      delayMicroseconds(400);
    while (getKeypadStatus() == keypadStatus)  // while the same button is held down
    {
      sendKeyPress(keypadStatus);  // continue to send the button over USB
      delay(250);
    }
  }
}

void rotate(ESPRotary & r) {
  new_pos = r.getPosition();
  if ( old_pos > new_pos) {
    old_pos = new_pos ;
    if ( MainVol == 1) {
      Serial.println("MEDIA_VOLUME_UP");
      Consumer.write(MEDIA_VOLUME_UP);
    }
    else if ( SideArr == 1) {
      Serial.println("KEY_RIGHT_ARROW");
      Keyboard.press(KEY_RIGHT_ARROW);
      Keyboard.releaseAll();
    }
    else if ( UpDownArr == 1) {
      Serial.println("KEY_DOWN_ARROW");
      Keyboard.press(KEY_DOWN_ARROW);
      Keyboard.releaseAll();
    }
  } else {
    old_pos = new_pos ;
    if ( MainVol == 1) {
      Serial.println("MEDIA_VOLUME_DOWN");
      Consumer.write(MEDIA_VOLUME_DOWN);
    }
    else if ( SideArr == 1) {
      Serial.println("KEY_LEFT_ARROW");
      Keyboard.press(KEY_LEFT_ARROW);
      Keyboard.releaseAll();
    }
    else if ( UpDownArr == 1) {
      Serial.println("KEY_UP_ARROW");
      Keyboard.press(KEY_UP_ARROW);
      Keyboard.releaseAll();
    }
  }
}

void sendKeyPress(int key) {
  switch (key) {
    case 1:  // 0x001
      Serial.println("MEDIA_VOLUME_MUTE");
      Consumer.write(MEDIA_VOLUME_MUTE);
      break;
    case 2:  // 0x002
      Serial.println("KEY_F7");
      Keyboard.press(KEY_F7);
      Keyboard.releaseAll();
      delay(75);
      break;
    case 4:  // 0x004
      Serial.println("KEY_F10");
      Keyboard.press(KEY_F10);
      Keyboard.releaseAll();
      delay(75);
      break;
    case 8:  // 0x008
      Serial.println("KEY_F9");
      Keyboard.press(KEY_F9);
      Keyboard.releaseAll();
      delay(75);
      break;
    case 16:  // 0x010
      Serial.println("KEY_LEFT_ARROW");
      Keyboard.press(KEY_LEFT_ARROW);
      Keyboard.releaseAll();
      delay(75);
      break;
    case 32:  // 0x020
      Serial.println("KEY_F8");
      Keyboard.press(KEY_F8);
      Keyboard.releaseAll();
      delay(75);
      break;
    case 64:  // 0x040
      Serial.println("KEY_RIGHT_ARROW");
      Keyboard.press(KEY_RIGHT_ARROW);
      Keyboard.releaseAll();
      delay(75);
      break;
    case 128:  // 0x080
      Serial.println("KEY_UP_ARROW");
      Keyboard.press(KEY_UP_ARROW);
      Keyboard.releaseAll();
      delay(75);
      break;
    case 256:  // 0x100
      Serial.println(key);
      if ( MainVol == 1 ) {
        UpDownArr = 1;
        SideArr = 0;
        MainVol = 0;
      }
      else if ( UpDownArr == 1 ) {
        UpDownArr = 0;
        SideArr = 1;
        MainVol = 0;
      }
      else {
        UpDownArr = 0;
        SideArr = 0;
        MainVol = 1;
      }
      break;
  }
}

int getKeypadStatus() {
  int keypadStatus = 0;  // this will be what's returned

  /* initialize all pins, inputs w/ pull-ups */
  for (int i = 0; i < 9; i++) {
    if (!digitalRead(keypadPins[i])) {
      keypadStatus |= 1 << i; // set the status bit of the keypad return value
    }
  }
  return keypadStatus;
}

 

Link do komentarza
Share on other sites

Bądź aktywny - zaloguj się lub utwórz konto!

Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony

Utwórz konto w ~20 sekund!

Zarejestruj nowe konto, to proste!

Zarejestruj się »

Zaloguj się

Posiadasz własne konto? Użyj go!

Zaloguj się »
×
×
  • Utwórz nowe...

Ważne informacje

Ta strona używa ciasteczek (cookies), dzięki którym może działać lepiej. Więcej na ten temat znajdziesz w Polityce Prywatności.