QWIIC Joystick with Arduino, Grove strip driver and .96" OLED

I read a similar post regarding the use of OLED and QWIIC Joystick. I am not sure if the issue is exactly the same.

I first started with Sparkfun Blackboard (with QWIIC), Grove Shield, and DYI .96" OLED

Updates all the Libraries. I2C scanner did not show any conflicts.

I had no problems with the working as a team.

I then added the QWIIC Joystick. That is when the problems started.

I can successfully accomplish a very limited use of the Joystick. 3 Arrays for each color (RGB) elements 0-3 works but if I add any additional elements The code begins to break down -for example I may get one joystick movement (vertical) but no horizontal.

//** CODE FOLLOWS

Edited by moderator to add code tags.

//Using gitHub library
//The following code is working as of: 10/09/2020:1601
//
/***************************************************************************/
/*
 *  Changed the display to #define SSD1306_128_64  in Adafruit_SSD1306.h  Library 
 * After initializing the display, the above sketch then draws a pixel at each extreme of the display by using drawPixel() to place a pixel in each corner of the screen. The first parameter passed to drawPixel() is the screen X coordinate and the second parameter is the screen Y coordinate.
Screen dimensions are 128 by 64 pixels and pixel coordinates start at 0, 0 for the top left pixel. This means that the X coordinates for the screen are from 0 to 127 (not 1 to 128) left to right; and Y coordinates are from 0 to 63 (not 1 to 64) top to bottom.
As can be seen in the sketch and on the display, the corner pixels are at the following coordinates:

Top left pixel – x = 0, y = 0.
Top right pixel – x = 127, y = 0.
Bottom left pixel – x = 0, y = 63.
Bottom right pixel – x = 127, y = 63.
  xMap = map(joystick.getHorizontal(), 0,1023, 0, 7);
  yMap = map(joystick.getVertical(),0,1023,7,0);
Position of Joystick:
Longside toward me, power light Right hand side
(7,7)      (7,4)      (7,0)         |s
                                    |h
                                    |o
(3,7)      (3,4)      (3,0)         |r  
                                    |t
                                    | 
(0,7)       (0,4)     (0,0)         |s
Long side---------------------------|
*/

#include <Wire.h>
#include "RGBdriver.h"

//#include <SPI.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "SparkFun_Qwiic_Joystick_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_joystick
JOYSTICK joystick; //Create instance of this object

// OLED display TWI address
#define OLED_ADDR   0x3C
//I2C scanner double checked addresses;
//I2C device found at address 0x20 - joystick
//I2C device found at address 0x3C - OLED

// reset pin not used on 4-pin OLED module
Adafruit_SSD1306 display(-1);  // -1 = no reset pin

// 128 x 64 pixel display
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif


#define CLK 5   //0  2  pins definitions for the driver        
#define DIO 6   //1  /3
RGBdriver Driver(CLK,DIO);
int k = 16;
int ixCurrent;
int iyCurrent;
int ixOld;
int iyOld;
int iButtonCur;
int iButtonOld;
int indxArray = 0;
bool bButton;
String cString, cColor;    //Conversion for Display
int xMap, yMap; 
int xValue = 0;
int yValue = 0;
int iRed, iBlue, iGreen;
String sColors[4] = {"ORANGE", "LIGHT BROWN", "HARVEST GOLD", "BLUE"};  // 0, 1, 2, 3
int aRed[4] = {255, 196, 230, 0};  //Orange. Light Brown, Harvest GOld, Blue
int aGreen[4] = {165, 98, 153, 0}; //Orange. Light Brown, Harvest GOld, Blue
int aBlue[4] = {0, 16, 0, 255};    //Orange. Light Brown, Harvest GOld, Blue
//If I increase the arrays to 8 no values are displayed on the OLED   10/10/2020:0952

void setup()  
{ 

Driver.begin(); // begin
  //Serial.begin(9600); 
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  // initialize and clear display
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.display();
  delay(500);

  if(joystick.begin() == false)   //This must be here
  {
    Serial.println("Joystick does not appear to be connected. Please check wiring. Freezing...");
    while(1);
  }
  
}    // End setup 



void loop()  
{                        //use the values of the joyStick to change the RGB Strip LEDs
  //int xMap, yMap, xValue, yValue;
  //int iRed, iBlue, iGreen;
  //String sColors(4) = {"ORANGE", "LIGHT BROWN", "HARVEST GOLD", "BLUE"};  // 0, 1, 2, 3
  //int cRed(4) = {255, 196, 230, 0};  //Orange. Light Brown, Harvest GOld, Blue
  //int cGreen(4) = {165, 98, 153, 0}; //Orange. Light Brown, Harvest GOld, Blue
  //int cBlue(4) = {0, 16, 0, 255};    //Orange. Light Brown, Harvest GOld, Blue
  
 
 xValue = joystick.getHorizontal();
 //delay(500); 
 xMap = map(joystick.getHorizontal(),0,1023, 0, 7);  //Now works 10/10/20:1718  
 
 delay(700); 
 yValue = joystick.getVertical();  //having problems
 yMap = map(joystick.getVertical(),0,1023,7,0);
 //if (yValu > 1000) {
 delay(500);  
 bButton = joystick.checkButton();
 delay(500);          
// }
  
 if (bButton) {
    display.setCursor(0,56); 
    display.print("T");   //Then do something
    display.display();
    delay(700);   
    indxArray = 0;
    iRed = aRed[indxArray];
    iBlue = aBlue[indxArray];
    iGreen = aGreen[indxArray];
    cColor = sColors[indxArray];
    changeLEDcolor(iRed, iGreen, iBlue, xMap, yMap, cColor);   
   
    
    indxArray = indxArray + 1;
 }
 else {
  xyJoystick(xMap, yMap, " ");
 }
// if (indxArray > 3) {   //As soon as I put this in one of the x-y values disappears
//    indxArray = 0;         // As steup above, the xMap value disappears
// }
 delay(500); 
// xValue = analogRead(joyX);  NOPE
//xMap = map(joystick.getHorizontal(), 0,1023, 0, 7);  NOPE

//    for(i = 0;i < 256;i ++)
//    for(i = 255;i > 0;i --)
//  unsigned int i;
//  unsigned int j;

}   // End Main LOOP

void changeLEDcolor(int iR, int iG, int iB, int iX, int iY, String sT) 
{                   //(int RED, GREEN, BLUE)
// 
  String sX, sY, sPrntX, sPrntY;
  sX = String(iX);
  sY = String(iY);
  sPrntX = "X= " + sX;  //joystick Horizontal value
  sPrntY = "Y= " + sY;  //joystick Vertical value  
  //Get Joystick values
  //Default values: X: 508508 Y: 518 Button: 1
  //https://rgbcolorcode.com/color/FFAA00
  //Amber: rgb(255,126,0)
  //Light Brown: rgb(196,98,16)
  //Jarvest Gold  rgb(230, 153,0)
  //Carrot Orange: rgb(255,140,25)
  //Pastel Orange: rgb(255,195,77)
  //Saffron: rgb(255,187,51)
  //Chrome Yellow: rgb(255,170,0)
  //RGB setting example (Chrome Yellow): rgb(255,170,0)
  //But,  stripdriver format:                 G   R   B 
 
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,16);
    display.print(sT); 
//    delay(600);
    display.setCursor(0,36);
    display.print(sPrntX);  
    
//        display.display();
//        delay(600);
    display.setCursor(0,46);
    display.print(sPrntY);
//    delay(600);
    display.display();  
    delay(700);
    Driver.begin(); // begin
    Driver.SetColor(iG,iR,iB); //GREEN. First node data. SetColor(R,G,B)
    Driver.end();
    delay(5000);
    display.clearDisplay();
    delay(500);
}


void xyJoystick(int iX, int iY, String sT) 
{                   
// 
  String sX, sY, sPrntX, sPrntY;
  sX = String(iX);
  sY = String(iY);
  sPrntX = "X= " + sX;  //joystick Horizontal value
  sPrntY = "Y= " + sY;  //joystick Vertical value  
  //Get Joystick values
  //Default values: X: 508508 Y: 518 Button: 1
 
    display.setTextSize(1);
    display.setTextColor(WHITE);

    display.setCursor(0,36);
    display.print(sPrntX);  
    
//        display.display();
//        delay(600);
    display.setCursor(0,46);
    display.print(sPrntY);
//    delay(600);
    display.display();  
    delay(5000);
    display.clearDisplay();;
}

void headerOLED() {
    display.setTextSize(2);
    display.setTextColor(WHITE);  
    display.setCursor(0,0);
    display.print("COLORS!");  
    display.display();   
    delay(1000);     ///Somehow the timing was messed up.  Added more delay
}

I still do not know what exactly why items were not displayed. However, I did discover how to force it. I had to change the order of displaying. Cursor position at (0,46) had to be printed first. The following code was selected and “copied for forum”. It is my progress so far.

//Using gitHub library
//The following code is working as of: 10/09/2020:1601
//
/***************************************************************************/
/*
 *  Changed the display to #define SSD1306_128_64  in Adafruit_SSD1306.h  Library 
 * After initializing the display, the above sketch then draws a pixel at each extreme of the display by using drawPixel() to place a pixel in each corner of the screen. The first parameter passed to drawPixel() is the screen X coordinate and the second parameter is the screen Y coordinate.
Screen dimensions are 128 by 64 pixels and pixel coordinates start at 0, 0 for the top left pixel. This means that the X coordinates for the screen are from 0 to 127 (not 1 to 128) left to right; and Y coordinates are from 0 to 63 (not 1 to 64) top to bottom.
As can be seen in the sketch and on the display, the corner pixels are at the following coordinates:

Top left pixel – x = 0, y = 0.
Top right pixel – x = 127, y = 0.
Bottom left pixel – x = 0, y = 63.
Bottom right pixel – x = 127, y = 63.
  xMap = map(joystick.getHorizontal(), 0,1023, 0, 7);
  yMap = map(joystick.getVertical(),0,1023,7,0);
Position of Joystick:
Longside toward me, power light Right hand side
Regular values (non-mapped):
(1023,0)    (1023,518)    (1023,1023)|s
                                     |h
                                     |o
(508,0)     (508,518)     (508,1023) |r  
                                     |t 
                                     |     power led
(0,0)       (0,518)       (0,1023)   |s
Long side----------------------------|
                                          
MAP                                       
Longside toward me, power light Right hand side
(7,7)       (7,4)         (7,0)      |s
 5                                   |h
 4                                   |o
(3,7)  6  5 (3,4) 3  2  1 (3,0)      |r  
 2                                   |t
 1                                   |     power led    
(0,7)       (0,4)         (0,0)      |s
Long side----------------------------|

positions: (1) x<=1, y>=6
           (2) 2 <= x<=4; y>=6
           (3) 5<=x <=7;  y>=6
           (4) 5<=x <=7; 3 <= y<=5
           (5) 2 <= x<=4; 3 <= y<=5
           (6) x<=1;   3 <= y<=5
           (7) x<=1, y<=2
           (8) 2 <= x<=4; y<=2
           (9) 5<=x <=7;  y<=2         
*/

#include <Wire.h>
#include "RGBdriver.h"

//#include <SPI.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "SparkFun_Qwiic_Joystick_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_joystick
JOYSTICK joystick; //Create instance of this object

// OLED display TWI address
#define OLED_ADDR   0x3C
//I2C scanner double checked addresses;
//I2C device found at address 0x20 - joystick
//I2C device found at address 0x3C - OLED

// reset pin not used on 4-pin OLED module
Adafruit_SSD1306 display(-1);  // -1 = no reset pin

// 128 x 64 pixel display
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif


#define CLK 5   //0  2  pins definitions for the driver        
#define DIO 6   //1  /3
RGBdriver Driver(CLK,DIO);
int k = 16;
int ixCurrent;
int iyCurrent;
int ixOld;
int iyOld;
int iButtonCur;
int iButtonOld;
int indxArray = 0;
bool bButton;
String cString, cColor;    //Conversion for Display
int xMap, yMap; 
int xValue = 0;
int yValue = 0;
int iRed, iBlue, iGreen;
String sColors[4] = {"ORANGE", "LIGHT BROWN", "HARVEST GOLD", "BLUE" };  // 0, 1, 2, 3
int aRed[4] = {255, 196, 230, 0};  //Orange. Light Brown, Harvest GOld, Blue
int aGreen[4] = {165, 98, 153, 0}; //Orange. Light Brown, Harvest GOld, Blue
int aBlue[4] = {0, 16, 0, 255};    //Orange. Light Brown, Harvest GOld, Blue

//String sColors[5] = {"ORANGE", "LIGHT BROWN", "HARVEST GOLD", "BLUE", "ORANGE" };  // 0, 1, 2, 3
//int aRed[5] = {255, 196, 230, 0, 255};  //Orange. Light Brown, Harvest GOld, Blue
//int aGreen[5] = {165, 98, 153, 0, 165}; //Orange. Light Brown, Harvest GOld, Blue
//int aBlue[5] = {0, 16, 0, 255, 0};    //Orange. Light Brown, Harvest GOld, Blue

//String sColors[8] = {"ORANGE", "LIGHT BROWN", "HARVEST GOLD", "BLUE", "ORANGE", "LIGHT BROWN", "HARVEST GOLD", "BLUE" };  // 0, 1, 2, 3
//int aRed[8] = {255, 196, 230, 0, 255, 196, 230, 0};  //Orange. Light Brown, Harvest GOld, Blue
//int aGreen[8] = {165, 98, 153, 0, 165, 98, 153, 0}; //Orange. Light Brown, Harvest GOld, Blue
//int aBlue[8] = {0, 16, 0, 255, 0, 16, 0, 255};    //Orange. Light Brown, Harvest GOld, Blue
   //If I increase the arrays to >= 5 no values are displayed on the OLED   10/10/2020:0952

void setup()  
{ 

Driver.begin(); // begin
  //Serial.begin(9600); 
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  // initialize and clear display
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.display();
  delay(500);

  if(joystick.begin() == false)   //This must be here
  {
    Serial.println("Joystick does not appear to be connected. Please check wiring. Freezing...");
    while(1);
  }
  
}    // End setup 



void loop()  
{                        //use the values of the joyStick to change the RGB Strip LEDs
  //int xMap, yMap, xValue, yValue;
  //int iRed, iBlue, iGreen;
  //String sColors(4) = {"ORANGE", "LIGHT BROWN", "HARVEST GOLD", "BLUE"};  // 0, 1, 2, 3
  //int cRed(4) = {255, 196, 230, 0};  //Orange. Light Brown, Harvest GOld, Blue
  //int cGreen(4) = {165, 98, 153, 0}; //Orange. Light Brown, Harvest GOld, Blue
  //int cBlue(4) = {0, 16, 0, 255};    //Orange. Light Brown, Harvest GOld, Blue
  
 
   xValue = joystick.getHorizontal();
   //delay(500); 
   xMap = map(joystick.getHorizontal(),0,1023, 0, 7);  //Now works 10/10/20:1718  
   int mapX;
   mapX = xMap;
   display.setCursor(0,36);
   display.print("MX= " + String(mapX));  //"MX= " + String(xMap); + String(xValue)  
   delay(700); 
   yValue = joystick.getVertical();  //having problems
   yMap = map(joystick.getVertical(),0,1023,7,0);

 // xyRJoystick(xValue, yValue, " ");
    
   delay(500);  
   bButton = joystick.checkButton();
   delay(500);       
   xyMJoystick(xMap, yMap, " ");
   indxArray = iPosition(xMap, yMap);    
    iRed = aRed[indxArray];
    iBlue = aBlue[indxArray];
    iGreen = aGreen[indxArray];
    cColor = sColors[indxArray];
    changeLEDcolor(iRed, iGreen, iBlue, xMap, yMap, cColor);
  String sX, sY, sPrntX, sPrntY;
  sX = String(xMap);
  sY = String(yMap);
  sPrntX = "MX= " + sX;  //joystick Horizontal value
  sPrntY = "MY= " + sY;  //joystick Vertical value 

    delay(500);
    display.setTextSize(1);
    display.setTextColor(WHITE);

    //delay(700);

    delay(600);
    display.setCursor(0,56); 
    display.print("iPOS= " + String(indxArray));   //Then do something

    delay(600);
    display.setCursor(0,46);
    display.print(sPrntY);
    //delay(600);    
    //display.setCursor(0,36);
    //display.print(sPrntX);

      
    delay(600);
    display.setCursor(0,0); 
    display.print(cColor);   //Then do something
    delay(700);
    display.display();  
 
        
 if (bButton) {
    //display.setCursor(0,56); 
    //display.print("T");   //Then do something
    //display.display();
    //delay(700);   
    indxArray = 0;
    iRed = aRed[indxArray];
    iBlue = aBlue[indxArray];
    iGreen = aGreen[indxArray];
    cColor = sColors[indxArray];
    //     changeLEDcolor(iRed, iGreen, iBlue, xMap, yMap, cColor);   
   
    
    //    indxArray = indxArray + 1;
 }

 
    //
    delay(5000);
    display.clearDisplay();
}   // End Main LOOP



void changeLEDcolor(int iR, int iG, int iB, int iX, int iY, String sT) 
{                   //(int RED, GREEN, BLUE)
// 
 
  //Get Joystick values
  //Default values: X: 508508 Y: 518 Button: 1
  //https://rgbcolorcode.com/color/FFAA00
  //Amber: rgb(255,126,0)
  //Light Brown: rgb(196,98,16)
  //Jarvest Gold  rgb(230, 153,0)
  //Carrot Orange: rgb(255,140,25)
  //Pastel Orange: rgb(255,195,77)
  //Saffron: rgb(255,187,51)
  //Chrome Yellow: rgb(255,170,0)
  //RGB setting example (Chrome Yellow): rgb(255,170,0)
  //But,  stripdriver format:                 G   R   B 
 

    Driver.begin(); // begin
    Driver.SetColor(iG,iR,iB); //GREEN. First node data. SetColor(R,G,B)
    Driver.end();
    delay(5000);

}

int iPosition(int iX, int iY) {
int iPos;
  
if (iX==0 && iY ==7){
  iPos = 0;
 
}
else if (iX==3 && iY == 7) {
  iPos = 1;
}
else if (iX ==7 && iY == 7) {
  iPos = 2;
}
else if (iX == 3 && iY == 4) {
  iPos = 3;
}
else if (iX ==7 && iY == 4) {
  iPos = 4;
}
else if (iX == 7 && iY == 0) {
  iPos = 1;
}
else if (iX == 3 && iY == 0) {
  iPos = 2;
}
else if (iX == 0 && iY == 0) {
  iPos = 3;
}
else if (iX == 0 && iY == 4) {
  iPos = 4;
}

else {
  //oops!
 
 // iPos = 0;
}  
return iPos;
}

void xyRJoystick(int iX, int iY, String sT) 
{                   
// 
  String sX, sY, sPrntX, sPrntY;
  sX = String(iX);
  sY = String(iY);
  sPrntX = "X= " + sX;  //joystick Horizontal value
  sPrntY = "Y= " + sY;  //joystick Vertical value  
  //Get Joystick values
  //Default values: X: 508508 Y: 518 Button: 1
 
    //display.setTextSize(1);
    //display.setTextColor(WHITE);

    //display.setCursor(0,16);
    //display.print(sPrntX);  
    
//        display.display();
//        delay(600);
    //display.setCursor(0,26);
    //display.print(sPrntY);
//    delay(600);
    //display.display();  
    //delay(5000);
    //display.clearDisplay();;
}

void xyMJoystick(int iX, int iY, String sT) 
{                   
// 
  String sX, sY, sPrntX, sPrntY;
  sX = String(iX);
  sY = String(iY);
  sPrntX = "MX= " + sX;  //joystick Horizontal value
  sPrntY = "MY= " + sY;  //joystick Vertical value  
  //Get Joystick values
  //Default values: X: 508508 Y: 518 Button: 1
 
    //display.setTextSize(1);
    //display.setTextColor(WHITE);

    //display.setCursor(0,36);
    //display.print(sPrntX);  
    
     //   display.display();
     //   delay(600);
    //display.setCursor(0,46);
    //display.print(sPrntY);
//    delay(600);
    //display.display();  
    //delay(5000);
    //display.clearDisplay();;
}

void headerOLED() {
    display.setTextSize(2);
    display.setTextColor(WHITE);  
    display.setCursor(0,0);
    display.print("COLORS!");  
    display.display();   
    delay(1000);     ///Somehow the timing was messed up.  Added more delay
}

void comparit() {
  int iX;
  int iY;
  int iPos;
 if (iX<=1 && iY >=6){
  iPos = 0;
 
}
else if (2<= iX <=4 && iY >=6) {
  iPos = 1;
}
else if (5<= iX <=7 && iY >=6) {
  iPos = 2;
}
else if (5<=iX <=7 && 3 <= iY<=5) {
  iPos = 3;
}
else if (2 <= iX<=4 && 3 <= iY<=5) {
  iPos = 4;
}
else if (iX<=1 && 3 <= iY<=5) {
  iPos = 5;
}
else if (iX<=1 && iY<=2) {
  iPos = 6;
}
else if (2 <= iX<=4 && iY<=2) {
  iPos = 7;
}
else if (5<=iX <=7 && iY<=2) {
  iPos = 8;
}
else if (iX==3 && iY==4) {
  iPos = 8;

}
else {
  //oops!
 
  iPos = 0;
} 
}
void textSpacing(void) {
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("123456789A");  
  display.setTextSize(1);
  display.setCursor(0,16);
  display.print("123456789A123456789B1");
  display.setCursor(0,26);
  display.print("ABCDEFGHIJKLMNOPQRSTU");
  display.setCursor(0,36);
  display.print("123456789A123456789B1");
  display.setCursor(0,46);
  display.print("ABCDEFGHIJKLMNOPQRSTU");  
  display.setCursor(0,56);
  display.print("123456789A12345678");
 
  // update display with all of the above graphics
  display.display();  
}

void testdrawstyles(void) {
  display.clearDisplay();

  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(F("Hello, world!"));
  display.setCursor(0,1);
  display.println(F("123456789A12345678"));
  display.setCursor(0,2);
  display.println(F("ABCDEFGHIJKLMNOP"));
  display.println(F("                "));
  //display.setCursor(0,3);
  //display.println(F("Red Blue Green"));
  
  //display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  //display.println(3.141592);

 // display.setTextSize(2);             // Draw 2X-scale text
 // display.setTextColor(SSD1306_WHITE);
 // display.print(F("0x")); display.println(0xDEADBEEF, HEX);

//  display.display();
//  delay(2000);
}