KY-040 liest nicht richtig

Hallo,

dies ist mein erster Post hier und ich bin auch noch neu in der Arduino Welt also bitte seit nachsichtig.

Ich habe auch schon gegoogelt und einen ähnlichen Thread hier gefunden, leider ohne Lösung die mir half.

folgendes Problem:

Ich versuche mit einemArduino Pro Micro, zwei Joysticks (KY-023), einem SSD1306 OLED und einem Encoder (KY-040) eine Art Space Mouse für Fusion 360 zu bauen. Das funktioniert auch wunderbar (zumindest mit einem Joystick der zweite liegt noch bei der Post), zumindest hat es das.
Seit ich zusätzlich zum KY-023 und dem KY-040 das OLED angeschlossen habe funktioniert der Kodierte Drehschalter (KY-040) nurnoch richtig wenn ich ihn ganz langsam drehe und dabei ganz fest halte sodass er nicht von selbst in die nächste Position rutscht (vorher konnte ich drehen wie ich wollte und es ging).

Könnte das an dem OLED liegen??

Hier mein Code der wie gesagt vorher Problemlos funktionierte:

#include <Mouse.h>
#include <Keyboard.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


int horzPin = A0;  // Analog output of horizontal joystick pin
int vertPin = A2;  // Analog output of vertical joystick pin
int selPin = 6;   // In case you want to use the button from the joystick
int lOut = 16;

/*
  int horzPin2 = A1;  // Analog output of horizontal joystick pin
  int vertPin2 = 8;  // Analog output of vertical joystick pin
  // int selPin2 = 9;   // In case you want to use the button from the joystick
  int rOut = 15;
*/





int moved = 0;    // checks if the joystick has moved
// int moved2 = 0;


int vertZero, horzZero;       // Stores the initial value of each axis, usually around 512
int vertValue, horzValue;     // Stores current analog output of each axis
const int sensitivity = 135;  // Higher sensitivity value = slower mouse, should be <= about 500

/*
  int vertZero2, horzZero2;       //Stores the initial value of each axis, usually around 512
  int vertValue2, horzValue2;     //Stores current analog output of each axis
  const int sensitivity2 = 175;  //Higher sensitivity value = slower mouse, should be <= about 500
*/
/*
int pZero;          //Stores the initial value of poti moves
int pValue;  //Stores current analog output of each side of the poti
int rZero;          //Stores the initial value of poti moves
int rValue;  //Stores current analog output of each side of the poti
const int sensitivityP = 175; // Higher sensitivity value = slower mouse, should be <= about 500
*/
// int invertMouse = 1;        // Invert joystick based on orientation
int invertMouse = -1;         // Noninverted joystick based on orientation

/*
  // int invertMouse2 = 1;        // Invert joystick based on orientation
  int invertMouse2 = -1;         // Noninverted joystick based on orientation
*/

// int invertMouseP = 1;        // Invert joystick based on orientation
//int invertMouseP = -1;         // Noninverted joystick based on orientation

// rotations Encoder
int clk = 9; // Analoger Output Drehung (rotation)
int dt = 4; 
int roClick = 5; // Analoger Output bei einem Klick (rotation)
int roOut = 14;

int mro = 0; // Überprüfung ob sich der rotary bewegt hat

int sensRo = 1; // Sensitivität rotary ==> höher gleich stärker

int counter = 0; // rotary Zähler
bool richt; // Richtung des rotary

long letzter;  // letzte Position des rotary
int aktuell;  // aktuelle Position des rotary

void setup()
{

  pinMode(horzPin, INPUT);         // Set both analog pins as inputs
  pinMode(vertPin, INPUT);
  pinMode(selPin, INPUT);          // In case you want to use the button from the joystick
  digitalWrite(selPin, HIGH);      // In case you want to use the button from the joystick
  pinMode(lOut, OUTPUT);
  digitalWrite(lOut, HIGH);
  /*
    pinMode(horzPin2, INPUT);         // Set both analog pins as inputs
    pinMode(vertPin2, INPUT);
    //  pinMode(selPin2, INPUT);          // In case you want to use the button from the joystick
    //  digitalWrite(selPin2, HIGH);      // In case you want to use the button from the joystick
  */
  
  pinMode(clk, INPUT);       // Set analog pin as input
  pinMode(dt, INPUT);
  pinMode(roOut, OUTPUT);
  digitalWrite(roOut, HIGH);

  delay(1000);                     // short delay to let outputs settle
  vertZero = analogRead(vertPin);  // get the initial values
  horzZero = analogRead(horzPin);  // Joystick should be in neutral position when reading these
  letzter = digitalRead(clk);   
  /*
    vertZero2 = analogRead(vertPin2);  // get the initial values
    horzZero2 = analogRead(horzPin2);  // Joystick should be in neutral position when reading these


    pZero = analogRead(pPin); // get the initial values
    rZero = analogRead(rPin);

    digitalWrite(pPin, true); // enable pull-up
    digitalWrite(rPin, true);
  */
  Mouse.begin();      // Init mouse emulation
  Keyboard.begin();   // Init keyboard emulation
//  Serial.begin(9600);
//  Serial.println("Start");

  // ***** DISPLAY *****
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }


   display.setRotation(2);
   display.invertDisplay(true);
   display.clearDisplay();
   display.setTextSize(1); // Draw 2X-scale text
   display.setTextColor(SSD1306_WHITE);
   display.display();
   display.clearDisplay();
}

void loop()
{
  // ***** DRAW LOGOS *****
  display.setCursor(8, 30);
  display.println("WALLISER INDUSTRIES");
 // display.drawBitmap(0, 0, autodesk, SCREEN_WIDTH, SCREEN_HEIGHT, BLACK, WHITE);
  display.display();
  
  // ***** LEFT JOYSTICK *****

  vertValue = analogRead(vertPin) - vertZero;  // read vertical offset
  horzValue = analogRead(horzPin) - horzZero;  // read horizontal offset


  if ( (vertValue > 2) || (vertValue < (-2)) )
  { // position two instead of 0 cancel the small fluctuation between readings
    Keyboard.press(KEY_LEFT_SHIFT);
//    Serial.println("Shift wird gedrueckt");
    Mouse.press(MOUSE_MIDDLE);
//    Serial.println("Mittle Maustaste wird gedrueckt");
    Mouse.move(0, (invertMouse * (vertValue / sensitivity)), 0); // move mouse on y axis with Middle Mouse key pressed to rotate view
//    Serial.println("Bewegung vertikal erkannt");
    moved = 1;
  }

  if ( (horzValue > 2) || (horzValue < (-2)) )
  {
    Keyboard.press(KEY_LEFT_SHIFT);
//    Serial.println("Shift wird gedrueckt");
    Mouse.press(MOUSE_MIDDLE);
//    Serial.println("Mittle Maustaste wird gedrueckt");
    Mouse.move((invertMouse * (horzValue / sensitivity)), 0, 0); // move mouse on x axis with Middle Mouse key pressed to rotate view
//    Serial.println("Bewegung horizontal erkannt");
    moved = 1;
  }
  if ( (vertValue <= 2) && (vertValue >= (-2))  &&  (horzValue <= 2) && (horzValue >= (-2))  )
  { // if joystick is in neutral position release all
    Keyboard.releaseAll();
    Mouse.release(MOUSE_MIDDLE);
    if (moved == 1) {
      moved = 0;
    }
  }
  /*
    // ***** RIGHT JOYSTICK *****

    vertValue2 = analogRead(vertPin2) - vertZero2;  // read vertical offset
    horzValue2 = analogRead(horzPin2) - horzZero2;  // read horizontal offset


    if ( (vertValue2 > 2)||(vertValue2 < (-2)) )
    {  // position two instead of 0 cancel the small fluctuation between readings
      Mouse.press(MOUSE_MIDDLE);
      Mouse.move(0, (invertMouse2 * (vertValue2 / sensitivity2)), 0); // move mouse on y axis with Middle Mouse key pressed to rotate view
      moved2=1;
    }

    if ( (horzValue2 > 2)||(horzValue2 < (-2)) )
    {
      Mouse.press(MOUSE_MIDDLE);
      Mouse.move((invertMouse2 * (horzValue2 / sensitivity2)), 0, 0); // move mouse on x axis with Middle Mouse key pressed to rotate view
      moved2=1;
    }

    if ( (vertValue2 <= 2)&&(vertValue2 >= (-2))  &&  (horzValue2 <= 2)&&(horzValue2 >= (-2))  )
    {   // if joystick is in neutral position release all
        Keyboard.releaseAll();
        Mouse.release(MOUSE_MIDDLE);
        if(moved2==1){
           moved2=0;
        }
    }
  */



    // ***** rotations Encoder Zoom *****
  aktuell = digitalRead(clk);

  if (aktuell != letzter)
  {
    if (digitalRead(dt) != aktuell)
    {
      counter ++;
      richt = true;
    }
    else
    {
      richt = false;
      counter --;
    }

    if (richt == true )
    {
      Mouse.move(0,0,counter * sensRo);
      Serial.println("Drechung Rotary im Uhrzeigersinn erkannt und ausgefuehrt");
      Mouse.release();
    }

    if (richt == false)
    {
      Mouse.move(0,0,counter * sensRo);
      Serial.println("Drechung Rotary gegen Uhrzeigersinn erkannt und ausgefuehrt");
      Mouse.release();
    }
  }

  letzter = aktuell; // rotary Nullsetzen
  counter = 0; 
  
  delay(10); //this slows down the moves

}

Der serielle Monitor bestätigt meine Vermutung dass er das irgendwie nicht richtig liest.

für jede Antwort bin ich jetzt schon dankbar!
fl0w1990

edit: Bitte nicht über die teils deutschen und teils englischen Kommentare wundern, der englische Teil stammt aus dem Grundprojekt welches mir die Idee für mein Projekt gab. (Falls es jemanden Interessiert)

Wenn Du das auskommentierst geht es?
Dann benutze das nur, wenn es sich inhaltlich geändert hat.

1 Like

Wenn ich das auskommentiere geht es, ja (natürlich fehlt gerade die Bitmap). Am Verhalten des Encoders ändert das aber nichts oder verstehe ich das gerade falsch?
Danke für den Tipp. Ich weiß noch nicht genau was ich auf dem Display anzeigen will, das sollte erstmal nur sicherstellen dass es funktioniert.

Dann ist die Lösung recht einfach:
Du darfst nicht andauernd das Display beschreiben und Dir damit Reaktionszeit wegnehmen.

Vermutlich.
Der Encoder funktioniert schon.
Du merkst nur nicht das da was geht - solange der Display beschrieben wird.
Und da Du mit jedem Umlauf den Display neu beschreibst....

1 Like

Der Encoder funktioniert ja, ich sehe im seriellen Monitor ja auch was passiert. Das Problem ist dass ein normaler Dreh nach rechts in rechts, links, links, rechts,… endet. Nach links geht es etwas besser aber auch hin und her. Drehe ich ganz langsam geht es normal.

Edit: Ich habe den Teil den Du meintest jetzt hochgeschoben und siehe da es funktioniert viel besser.

// Author Florian Walliser
// February 2022


#include <Mouse.h>
#include <Keyboard.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
/*
  static const unsigned char PROGMEM autodesk[] = {
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xff,0xff,0xdf,0xf7,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xdf,0xff,0xfe,0xff,0xff,0xdf,0xf7,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xdf,0xff,0xfe,0xff,0xff,0xdf,0xf7,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf7,0xfb,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf7,0xfb,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf7,0xfb,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf7,0xfb,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x77,0xfb,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x77,0xfb,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x77,0xff,0xff,0xdf,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x77,0xff,0x7f,0xdf,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x77,0xff,0x7f,0xdf,0xff,0xff
,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0x77,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0x77,0xff,0xef,0xff,0xff,0xff
,0xff,0xff,0xff,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0x77,0xff,0xef,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf7,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf7,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf7,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf7,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xfb,0xfe,0xff,0xfd,0xf7,0xff,0xf7,0xfb,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xfb,0xfe,0xff,0xfd,0xf7,0xff,0xf7,0xfb,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xfe,0x7f,0x7f,0xf7,0x7d,0xf7,0x3c,0x79,0xff,0xef,0xbb,0xef,0xff
,0xff,0xff,0xff,0xfe,0x7f,0x7f,0xf7,0x7d,0xf7,0x3c,0x79,0xff,0xef,0xbb,0xef,0xff
,0xff,0xff,0xff,0xfe,0xff,0x7f,0xf7,0xfd,0xf7,0x9c,0x79,0xff,0xff,0xbf,0xef,0xff
,0xff,0xff,0xff,0xfe,0xff,0x7f,0xf7,0xfd,0xff,0xdc,0x39,0xff,0xf7,0xbf,0xff,0xbf
,0xff,0xff,0xff,0xfe,0xff,0x7f,0xf7,0xfd,0xff,0xdc,0x39,0xff,0xf7,0xbf,0xff,0xbf
,0xff,0xff,0xff,0xfe,0xff,0x7f,0xf7,0xfd,0xcf,0xdd,0xb9,0xff,0xff,0xbf,0xef,0xbf
,0xff,0xff,0xff,0xfe,0xff,0x7f,0xf7,0xfd,0xcf,0xdd,0xb9,0xff,0xef,0xbf,0xff,0xbf
,0xff,0xff,0xff,0xfe,0xff,0x7f,0xf7,0xfd,0xcf,0xdd,0xb9,0xff,0xef,0xbf,0xff,0xbf
,0xff,0xff,0xff,0xfe,0x7f,0x7f,0xf7,0xfd,0xcf,0xdd,0xb9,0xff,0xef,0x3f,0xff,0xbf
,0xff,0xff,0xff,0xfe,0x0f,0x7f,0xfe,0x7d,0xcf,0xdd,0xe9,0xff,0xef,0x3b,0xff,0xbf
,0xff,0xff,0xff,0xfe,0x0f,0x7f,0xfe,0x7d,0xcf,0xdd,0xe9,0xff,0xef,0x3b,0xff,0xbf
,0xff,0xff,0xff,0xfe,0x0f,0x7f,0xfe,0x7d,0xcf,0xdd,0xc9,0xff,0xff,0x3d,0xff,0xbf
,0xff,0xff,0xff,0xfe,0xff,0x7f,0xff,0x9d,0xcf,0xdd,0xc9,0xff,0xf7,0xfc,0xff,0xbf
,0xff,0xff,0xff,0xfe,0xff,0x7f,0xff,0x9d,0xcf,0xdd,0xc9,0xff,0xf7,0xfc,0xff,0xbf
,0xff,0xff,0xff,0xfe,0xff,0x7f,0xff,0x9d,0xcf,0xdd,0xc9,0xff,0xf7,0xbc,0xff,0xbf
,0xff,0xff,0xff,0xfe,0xff,0x7d,0xff,0x9d,0xef,0xdd,0xf1,0xff,0xf7,0xbf,0xef,0xbf
,0xff,0xff,0xff,0xfe,0xff,0x7d,0xff,0x9d,0xef,0xdd,0xf1,0xff,0xf7,0xbf,0xef,0xbf
,0xff,0xff,0xff,0xfe,0xff,0x7d,0xff,0x9d,0xef,0xdd,0xf1,0xff,0xf7,0xbf,0xef,0xbf
,0xff,0xff,0xff,0xfe,0xff,0x91,0xf7,0x7d,0xf7,0x3d,0xf1,0xfe,0x6f,0xdb,0xe2,0x7f
,0xff,0xff,0xff,0xfe,0xff,0x91,0xf7,0x7d,0xf7,0x3d,0xf1,0xfe,0x6f,0xdb,0xe2,0x7f
,0xff,0xff,0xff,0xfe,0xff,0xb1,0xf7,0x7d,0xf7,0x3d,0xf1,0xfe,0x6f,0xdb,0xe2,0x7f
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff

  };
*/
int horzPin = A0;  // Analog output of horizontal joystick pin
int vertPin = A2;  // Analog output of vertical joystick pin
int selPin = 6;   // In case you want to use the button from the joystick
int lOut = 16;

/*
  int horzPin2 = A1;  // Analog output of horizontal joystick pin
  int vertPin2 = 8;  // Analog output of vertical joystick pin
  // int selPin2 = 9;   // In case you want to use the button from the joystick
  int rOut = 15;
*/





int moved = 0;    // checks if the joystick has moved
// int moved2 = 0;


int vertZero, horzZero;       // Stores the initial value of each axis, usually around 512
int vertValue, horzValue;     // Stores current analog output of each axis
const int sensitivity = 135;  // Higher sensitivity value = slower mouse, should be <= about 500

/*
  int vertZero2, horzZero2;       //Stores the initial value of each axis, usually around 512
  int vertValue2, horzValue2;     //Stores current analog output of each axis
  const int sensitivity2 = 175;  //Higher sensitivity value = slower mouse, should be <= about 500
*/
/*
int pZero;          //Stores the initial value of poti moves
int pValue;  //Stores current analog output of each side of the poti
int rZero;          //Stores the initial value of poti moves
int rValue;  //Stores current analog output of each side of the poti
const int sensitivityP = 175; // Higher sensitivity value = slower mouse, should be <= about 500
*/
// int invertMouse = 1;        // Invert joystick based on orientation
int invertMouse = -1;         // Noninverted joystick based on orientation

/*
  // int invertMouse2 = 1;        // Invert joystick based on orientation
  int invertMouse2 = -1;         // Noninverted joystick based on orientation
*/

// int invertMouseP = 1;        // Invert joystick based on orientation
//int invertMouseP = -1;         // Noninverted joystick based on orientation

// rotations Encoder
int clk = 9; // Analoger Output Drehung (rotation)
int dt = 4; 
int roClick = 5; // Analoger Output bei einem Klick (rotation)
int roOut = 14;

int mro = 0; // Überprüfung ob sich der rotary bewegt hat

int sensRo = 1; // Sensitivität rotary ==> höher gleich stärker

int counter = 0; // rotary Zähler
bool richt; // Richtung des rotary

long letzter;  // letzte Position des rotary
int aktuell;  // aktuelle Position des rotary

void setup()
{

  pinMode(horzPin, INPUT);         // Set both analog pins as inputs
  pinMode(vertPin, INPUT);
  pinMode(selPin, INPUT);          // In case you want to use the button from the joystick
  digitalWrite(selPin, HIGH);      // In case you want to use the button from the joystick
  pinMode(lOut, OUTPUT);
  digitalWrite(lOut, HIGH);
  /*
    pinMode(horzPin2, INPUT);         // Set both analog pins as inputs
    pinMode(vertPin2, INPUT);
    //  pinMode(selPin2, INPUT);          // In case you want to use the button from the joystick
    //  digitalWrite(selPin2, HIGH);      // In case you want to use the button from the joystick
  */
  
  pinMode(clk, INPUT);       // Set analog pin as input
  pinMode(dt, INPUT);
  pinMode(roOut, OUTPUT);
  digitalWrite(roOut, HIGH);

  delay(1000);                     // short delay to let outputs settle
  vertZero = analogRead(vertPin);  // get the initial values
  horzZero = analogRead(horzPin);  // Joystick should be in neutral position when reading these
  letzter = digitalRead(clk);   
  /*
    vertZero2 = analogRead(vertPin2);  // get the initial values
    horzZero2 = analogRead(horzPin2);  // Joystick should be in neutral position when reading these


    pZero = analogRead(pPin); // get the initial values
    rZero = analogRead(rPin);

    digitalWrite(pPin, true); // enable pull-up
    digitalWrite(rPin, true);
  */
  Mouse.begin();      // Init mouse emulation
  Keyboard.begin();   // Init keyboard emulation
//  Serial.begin(9600);
//  Serial.println("Start");

  // ***** DISPLAY *****
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

   display.clearDisplay();
   display.setRotation(2);
   display.invertDisplay(true);
   display.setTextSize(1); // Draw 2X-scale text
   display.setTextColor(SSD1306_WHITE);
     // ***** DRAW LOGOS *****
   display.setCursor(8, 30);
   display.println("Test");
 // display.drawBitmap(0, 0, autodesk, SCREEN_WIDTH, SCREEN_HEIGHT, BLACK, WHITE);
   display.display();
}

void loop()
{ 
  // ***** LEFT JOYSTICK *****

  vertValue = analogRead(vertPin) - vertZero;  // read vertical offset
  horzValue = analogRead(horzPin) - horzZero;  // read horizontal offset


  if ( (vertValue > 2) || (vertValue < (-2)) )
  { // position two instead of 0 cancel the small fluctuation between readings
    Keyboard.press(KEY_LEFT_SHIFT);
//    Serial.println("Shift wird gedrueckt");
    Mouse.press(MOUSE_MIDDLE);
//    Serial.println("Mittle Maustaste wird gedrueckt");
    Mouse.move(0, (invertMouse * (vertValue / sensitivity)), 0); // move mouse on y axis with Middle Mouse key pressed to rotate view
//    Serial.println("Bewegung vertikal erkannt");
    moved = 1;
  }

  if ( (horzValue > 2) || (horzValue < (-2)) )
  {
    Keyboard.press(KEY_LEFT_SHIFT);
//    Serial.println("Shift wird gedrueckt");
    Mouse.press(MOUSE_MIDDLE);
//    Serial.println("Mittle Maustaste wird gedrueckt");
    Mouse.move((invertMouse * (horzValue / sensitivity)), 0, 0); // move mouse on x axis with Middle Mouse key pressed to rotate view
//    Serial.println("Bewegung horizontal erkannt");
    moved = 1;
  }
  if ( (vertValue <= 2) && (vertValue >= (-2))  &&  (horzValue <= 2) && (horzValue >= (-2))  )
  { // if joystick is in neutral position release all
    Keyboard.releaseAll();
    Mouse.release(MOUSE_MIDDLE);
    if (moved == 1) {
      moved = 0;
    }
  }
  /*
    // ***** RIGHT JOYSTICK *****

    vertValue2 = analogRead(vertPin2) - vertZero2;  // read vertical offset
    horzValue2 = analogRead(horzPin2) - horzZero2;  // read horizontal offset


    if ( (vertValue2 > 2)||(vertValue2 < (-2)) )
    {  // position two instead of 0 cancel the small fluctuation between readings
      Mouse.press(MOUSE_MIDDLE);
      Mouse.move(0, (invertMouse2 * (vertValue2 / sensitivity2)), 0); // move mouse on y axis with Middle Mouse key pressed to rotate view
      moved2=1;
    }

    if ( (horzValue2 > 2)||(horzValue2 < (-2)) )
    {
      Mouse.press(MOUSE_MIDDLE);
      Mouse.move((invertMouse2 * (horzValue2 / sensitivity2)), 0, 0); // move mouse on x axis with Middle Mouse key pressed to rotate view
      moved2=1;
    }

    if ( (vertValue2 <= 2)&&(vertValue2 >= (-2))  &&  (horzValue2 <= 2)&&(horzValue2 >= (-2))  )
    {   // if joystick is in neutral position release all
        Keyboard.releaseAll();
        Mouse.release(MOUSE_MIDDLE);
        if(moved2==1){
           moved2=0;
        }
    }
  */



    // ***** rotations Encoder Zoom *****
  aktuell = digitalRead(clk);

  if (aktuell != letzter)
  {
    if (digitalRead(dt) != aktuell)
    {
      counter ++;
      richt = true;
    }
    else
    {
      richt = false;
      counter --;
    }

    if (richt == true )
    {
      Mouse.move(0,0,counter * sensRo);
      Serial.println("Drechung Rotary im Uhrzeigersinn erkannt und ausgefuehrt");
      Mouse.release();
    }

    if (richt == false)
    {
      Mouse.move(0,0,counter * sensRo);
      Serial.println("Drechung Rotary gegen Uhrzeigersinn erkannt und ausgefuehrt");
      Mouse.release();
    }
  }

  letzter = aktuell; // rotary Nullsetzen
  counter = 0; 
  
  delay(10); //this slows down the moves

}

Allerdings passiert es immernoch, wenn ich ganz schnell drehe, dass sich falsche Bewegungen einschleicht, könnte das vielleicht einfach am billigen Encoder liegen (wobei er garnicht so billig war)?

Was erwartest Du hier?
Du bist für 10ms blind.
Alles was Du danach auf den Encoderpins auswertest ist reines raten.

1 Like

Perfekt! Das war die Lösung, ich danke Dir.
Das kam ursprünglich aus der Version mit nur einem Joystick aus dem Code den ich anfangs kopiert hatte.

Danke, Danke, Danke!

Na bitte - manchmal finde ich auch ein Korn... :wink:

Stellst den bitte noch auf gelöst? Dann kann in der Suchfunktion das als Merkmal von der Nachwelt genutzt werden.

1 Like

Jetzt muss ich das hier doch nochmal aufmachen...

Nachdem ich ein neues Gehäuse gedruckt habe musste ich die PIN-Leisten an den Joysticks ablöten und habe dabei auch die Kabel etwas anders angebracht. Plötzlich triggert der zweite Joystick unendlich in die vertikale Richtung (A3 am Arduino). Es wäre super wenn jemand über meinen Code schauen könnte um diesen ausschliessen zu können.


// February 2022


#include <Mouse.h>
#include <Keyboard.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const unsigned char itsame [] PROGMEM = {
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xfe, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xe0, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xfc, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xf8, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xf0, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xe0, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xc0, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0x80, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0x80, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xc0, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xc0, 0x00, 0x07, 0xf8, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xe0, 0x00, 0x1f, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xf0, 0x00, 0x7f, 0xff, 0x80, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xf8, 0x00, 0xff, 0xff, 0xe0, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xfc, 0x01, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xfe, 0x01, 0xff, 0xff, 0xf8, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0x01, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0x83, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xc3, 0xff, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xe7, 0xff, 0x00, 0x7f, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xf7, 0xfe, 0x0f, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xf8, 0xff, 0x1f, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xfb, 0xff, 0x87, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xfe, 0x7f, 0xc7, 0xff, 0xc1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xfc, 0xff, 0xef, 0xff, 0xc1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xfc, 0xff, 0xff, 0xff, 0xd3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xc1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xc3, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0x8f, 0xff, 0xff, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0x83, 0xff, 0xff, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0x00, 0xff, 0xff, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xfe, 0x00, 0x03, 0xff, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xfc, 0x70, 0x01, 0xff, 0x36, 0xdf, 0xe3, 0x31, 0x1f, 0xcd, 0x46, 0xb3, 0x11, 0x8c, 0xe4, 
  0xff, 0xfc, 0x7c, 0x01, 0xff, 0x34, 0xdf, 0xf6, 0xf7, 0x6f, 0xfd, 0x5a, 0xaf, 0xb6, 0xdf, 0xdc, 
  0xff, 0xfc, 0xef, 0x91, 0xe3, 0x1c, 0xdf, 0xf7, 0x71, 0x1f, 0xfc, 0x5a, 0xb7, 0xb1, 0xdc, 0xcc, 
  0xff, 0xf8, 0xff, 0xfd, 0xc7, 0x08, 0x5f, 0xf7, 0x97, 0x1f, 0xfc, 0x5a, 0xb9, 0xb1, 0xdd, 0xf4, 
  0xff, 0xfb, 0xff, 0xff, 0xc7, 0x2b, 0x5e, 0xf7, 0xd7, 0x4f, 0xcd, 0x52, 0xbd, 0xb4, 0xdd, 0xf4, 
  0xff, 0xff, 0xff, 0xff, 0xc2, 0x7b, 0x46, 0x63, 0x31, 0xef, 0xcf, 0x6f, 0x73, 0xbe, 0x8c, 0xcc, 
  0xff, 0xff, 0xff, 0xff, 0x40, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0x83, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0x83, 0xf8, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xc3, 0xf0, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xfe, 0xa7, 0xe0, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xfe, 0x7f, 0xe0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xfe, 0x30, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0x30, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0x80, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xf0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xff, 0xf0, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc
};

int horzPin = A0;  // Analog output of horizontal joystick pin
int vertPin = A2;  // Analog output of vertical joystick pin
int selPin = 6;   // In case you want to use the button from the joystick
int lOut = 16;

int selPinClick;  // Button links ZUSTAND

int horzPin2 = 7;  // Analog output of horizontal joystick pin
int vertPin2 = A3;  // Analog output of vertical joystick pin
int selPin2 = A1;   // In case you want to use the button from the joystick
int rOut = 15;

int selPin2Click; // Button rechts Zustand






int moved = 0;    // checks if the joystick has moved
 int moved2 = 0;


int vertZero, horzZero;       // Stores the initial value of each axis, usually around 512
int vertValue, horzValue;     // Stores current analog output of each axis
const int sensitivity = 200;  // Higher sensitivity value = slower mouse, should be <= about 500


int vertZero2, horzZero2;       //Stores the initial value of each axis, usually around 512
int vertValue2, horzValue2;     //Stores current analog output of each axis
const int sensitivity2 = 200;  //Higher sensitivity value = slower mouse, should be <= about 500


// int invertMouse = 1;        // Invert joystick based on orientation
int invertMouse = -1;         // Noninverted joystick based on orientation


//int invertMouse2 = 1;        // Invert joystick based on orientation
int invertMouse2 = -1;         // Noninverted joystick based on orientation


// rotations Encoder
int clk = 9; // Analoger Output Drehung (rotation)
int dt = 4; 
int roClick = 5; // Analoger Output bei einem Klick (rotation)
int roOut = 14;

int mro = 0; // Überprüfung ob sich der rotary bewegt hat

int sensRo = 1; // Sensitivität rotary ==> höher gleich stärker

int counter = 0; // rotary Zähler
bool richt; // Richtung des rotary

int letzter;  // letzte Position des rotary
int aktuell;  // aktuelle Position des rotary

int roClickClick; // Button Rotary Zustand

int modeL = 0; // Für die Switch Cases
//int modeR = 0;
int modeRo = 0;

void setup()
{

  pinMode(horzPin, INPUT);         // Set both analog pins as inputs
  pinMode(vertPin, INPUT);
  pinMode(selPin, INPUT_PULLUP);          // In case you want to use the button from the joystick
  digitalWrite(selPin, HIGH);      // In case you want to use the button from the joystick
  pinMode(lOut, OUTPUT);
  digitalWrite(lOut, HIGH);
  
  pinMode(horzPin2, INPUT);         // Set both analog pins as inputs
  pinMode(vertPin2, INPUT_PULLUP);
  pinMode(selPin2, INPUT_PULLUP);          // In case you want to use the button from the joystick
  digitalWrite(selPin2, HIGH);      // In case you want to use the button from the joystick
  pinMode(rOut, OUTPUT);
  digitalWrite(rOut, HIGH);
  
  
  pinMode(clk, INPUT);       // Set analog pin as input
  pinMode(dt, INPUT);
  pinMode(roClick, INPUT_PULLUP);
  digitalWrite(roClick, HIGH);
  pinMode(roOut, OUTPUT);
  digitalWrite(roOut, HIGH);

  delay(1000);                     // short delay to let outputs settle
  vertZero = analogRead(vertPin);  // get the initial values
  horzZero = analogRead(horzPin);  // Joystick should be in neutral position when reading these
  letzter = digitalRead(clk);   
 
  vertZero2 = analogRead(vertPin2);  // get the initial values
  horzZero2 = analogRead(horzPin2);  // Joystick should be in neutral position when reading these

  
  Mouse.begin();      // Init mouse emulation
  Keyboard.begin();   // Init keyboard emulation
//  Serial.begin(9600);
//  Serial.println("Start");

  // ***** DISPLAY *****
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

// ***** LOGO BEIM START *****
   display.clearDisplay();
//   display.setRotation(1);
   display.invertDisplay(true);
   display.setTextSize(1); // Draw 2X-scale text
   display.setTextColor(SSD1306_WHITE);
     // ***** DRAW LOGOS *****
   display.setCursor(0, 0);
   display.drawBitmap(0, 0, itsame, SCREEN_WIDTH, SCREEN_HEIGHT, BLACK, WHITE);
   display.display();
}

void loop()
{ 

  vertValue = analogRead(vertPin) - vertZero;  // read vertical offset
  horzValue = analogRead(horzPin) - horzZero;  // read horizontal offset

  
  vertValue2 = analogRead(vertPin2) - vertZero2;  // read vertical offset
  horzValue2 = analogRead(horzPin2) - horzZero2;  // read horizontal offset
/* 
  vertValue2 = analogRead(vertPin2) - vertZero2;  // read vertical offset
  horzValue2 = analogRead(horzPin2) - horzZero2;  // read horizontal offset
*/
/*
//***** BUTTON LINKS *****
  selPinClick = digitalRead(selPin);
  
  if ((selPinClick == LOW) && (modeL == 0))
  {
    display.clearDisplay();
    display.setCursor(0, 30);
    display.println("Autodesk Fusion 360");
    display.display();
    modeL = 1;
//    Serial.println("Mode = 1");
    delay(180);
  }
  else if ((selPinClick == LOW) && (modeL == 1))
  {
    display.clearDisplay();
    display.setCursor(0, 30);
    display.println("Ultimaker Cura");
    display.display();
    modeL = 0;
//    Serial.println("Mode = 0");
    delay(180);
  }
*/
//***** BUTTON RECHTS *****
/*
  selPin2Click = digitalRead(selPin2);
  
  if ((selPin2Click == LOW) && (modeR == 0))
  {
    modeR = 1;
//    Serial.println("Mode = 1");
    delay(120);
  }
  else if ((selPin2Click == LOW) && (modeR == 1))
  {
    modeR = 0;
//    Serial.println("Mode = 0");
    delay(120);
  }
*/
/*
//***** BUTTON ROTARY *****
  roClickClick = digitalRead(roClick);
  
  if ((roClickClick == LOW) && (modeRo == 0))
  {
    modeRo = 1;
//    Serial.println("Mode = 1");
    delay(120);
  }
  else if ((roClickClick == LOW) && (modeRo == 1))
  {
    modeRo = 0;
//    Serial.println("Mode = 0");
    delay(120);
  }
*/
  
    // ***** LEFT JOYSTICK ROTATE*****
  if ( (vertValue > 2) || (vertValue < (-2)) )
  { // position two instead of 0 cancel the small fluctuation between readings
    Keyboard.press(KEY_LEFT_SHIFT);
//    Serial.println("Shift wird gedrueckt");
    Mouse.press(MOUSE_MIDDLE);
//    Serial.println("Mittle Maustaste wird gedrueckt");
    Mouse.move(0, (invertMouse * (vertValue / sensitivity)), 0); // move mouse on y axis with Middle Mouse key pressed to rotate view
//    Serial.println("Bewegung vertikal erkannt");
    moved = 1;
  }

  if ( (horzValue > 2) || (horzValue < (-2)) )
  {
    Keyboard.press(KEY_LEFT_SHIFT);
//    Serial.println("Shift wird gedrueckt");
    Mouse.press(MOUSE_MIDDLE);
//    Serial.println("Mittle Maustaste wird gedrueckt");
    Mouse.move((invertMouse * (horzValue / sensitivity)), 0, 0); // move mouse on x axis with Middle Mouse key pressed to rotate view
//    Serial.println("Bewegung horizontal erkannt");
    moved = 1;
  }
  if ( (vertValue <= 2) && (vertValue >= (-2))  &&  (horzValue <= 2) && (horzValue >= (-2)) && (moved == 1) )
  { // if joystick is in neutral position release all
    Keyboard.releaseAll();
    Mouse.release(MOUSE_MIDDLE);
    moved = 0;
  }

    // ***** RIGHT JOYSTICK *****

  if ( (vertValue2 > 2) || (vertValue2 < (-2)) )
  { // position two instead of 0 cancel the small fluctuation between readings
    Mouse.press(MOUSE_MIDDLE);
//    Serial.println("Mittle Maustaste wird gedrueckt");
    Mouse.move(0, (invertMouse2 * (vertValue2 / sensitivity2)), 0); // move mouse on y axis with Middle Mouse key pressed to rotate view
//    Serial.println("7");
    moved2 = 1;
  }

  if ( (horzValue2 > 2) || (horzValue2 < (-2)) )
  {
    Mouse.press(MOUSE_MIDDLE);
//    Serial.println("Mittle Maustaste wird gedrueckt");
    Mouse.move((invertMouse2 * (horzValue2 / sensitivity2)), 0, 0); // move mouse on x axis with Middle Mouse key pressed to rotate view
    Serial.println("8");
    moved2 = 1;
  }
  if ( (vertValue2 <= 2) && (vertValue2 >= (-2))  &&  (horzValue2 <= 2) && (horzValue2 >= (-2)) && (moved2 == 1) )
  { // if joystick is in neutral position release all
    Mouse.release(MOUSE_MIDDLE);
    moved2 = 0;
  }
 

    // ***** rotations Encoder Zoom *****
  aktuell = digitalRead(clk);

  if (aktuell != letzter)
  {
    if (digitalRead(dt) != aktuell)
    {
      counter ++;
      richt = true;
    }
    else
    {
      richt = false;
      counter --;
    }

    if (richt == true )
    {
      Mouse.move(0,0,counter * sensRo);
//      Serial.println("Drechung Rotary im Uhrzeigersinn erkannt und ausgefuehrt");
      mro = 1;
      Mouse.release();
    }
    if (richt == false)
    {
      Mouse.move(0,0,counter * sensRo);
//      Serial.println("Drechung Rotary gegen Uhrzeigersinn erkannt und ausgefuehrt");
      mro = 1;
      Mouse.release();
    }
  }

  if(mro != 0)
  {
  letzter = aktuell; // rotary Nullsetzen
  counter = 0;
  mro = 0;
  }
}

Ich habe bereits alles Verbindungen überprüft und zur Sicherheit nochmal ein neues Kabel eingelötet, daran kann es eigentlich nicht liegen. Denkt Ihr es könnte sein dass ich den Joystick zerstört habe?

Liebe Grüße

Hallo
Du könntest mit einem Ohmmeter das Poti durchmessen, oder mit einem Voltmeter die Spannung. Klar kann es sein das Du das Poti gekillt hast. Ein Ende und Schleifer vertauscht, dann mit dem Knüppel Richtung Ende , und schon hast du einen Kurzschluss bei dem das Poti verbrennt.

Heinz

Ok danke, dh am Code siehst du aber nichts das diesen Fehler verursachen könnte? Vertauscht habe ich sicher nichts aber es könnte sein dass ich beim entölten mit der Entlötlitze was zerstört hab. Ich werde dann gleich mal die Spannung messen.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.