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)