Go Down

Topic: Weird behavior - SSD1306 wont work if an array is declared (Read 57 times) previous topic - next topic

EasyGoing1

So this is kinda weird ...

When I say that the display doesn't work, what I mean by that, is that the code in the setup routine that clears the display, shows the Adafruit logo, then clears the display and prints "Hello World" ... simply does not work ... in other words, the display does not clear, it does not display the logo etc.

Heres the entire sketch, but the only portions that matter are setup and loop ... so please don't wander off on tangents asking me why I did this or why I did that ... unless you believe that the tangent will get us to an answer of my question...

Code: [Select]
#include <Arduino.h>
#include <util/atomic.h>
#include <CiscoCharterRemote.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <Wire.h>

CiscoCharterRemote ccr;


#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)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#define CLR(x,y) (x&=(~(1<<y)))
#define SET(x,y) (x|=(1<<y))

#define SCL 2
#define SDO 3

const int RED = 4;
const int GREEN = 5;
const int BLUE = 6;

// Touchpad value
volatile uint16_t touchVal;  // var for ISR access
uint16_t touchValc;          // copy var for main loop

int shiftMode = 1;

void ledRed() {
    digitalWrite(RED,LOW);
    digitalWrite(GREEN,HIGH);
    digitalWrite(BLUE,HIGH);
}
void ledGreen() {
    digitalWrite(RED,HIGH);
    digitalWrite(GREEN,LOW);
    digitalWrite(BLUE,HIGH);
}
void ledBlue() {
    digitalWrite(RED,HIGH);
    digitalWrite(GREEN,HIGH);
    digitalWrite(BLUE,LOW);
}

int checkNumberPad() {
    int padNumber = 0;
    ATOMIC_BLOCK(ATOMIC_FORCEON) {
        touchValc = touchVal;
    }
    if (touchValc) {
        for (byte b=0; b<=15; b++) {
            if ((touchValc >> b) & 1) {
                padNumber = b+1;
            }
        }
    }
    return padNumber;
}

void setup() {
    Serial.begin(9600);
    if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
        Serial.println(F("SSD1306 allocation failed"));
    }
    display.display();
    delay(3500);
    display.clearDisplay();
    display.drawPixel(10, 10, WHITE);
    display.setTextColor(WHITE);
    display.println("Hello There, I'm ME");
    // Show the display buffer on the screen. You MUST call display() after
    // drawing commands to make them visible on screen!
    display.display();
    delay(2000);

    pinMode(SCL, OUTPUT);
    pinMode(SDO, INPUT);
    // set up INT1 on digital pin 3
    EICRA = (1 << ISC11) | (1 << ISC10);  // external INT1 on rising edge
    EIMSK = (1 << INT1);  // External Interrupt Request 1 Enable
    sei();
}
void loop() {

   
    const int COMMANDS[4][17] = {    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                     {-10,0,1,2,4,2,-1,6,14,0,8,5,9,1,3,7},
                                     {-10,10,11,12,13,16,17,18,19,20},
                                     {-10,21,22,23,24,25,26,27,28,29,30}
    } ;

    int padNumber = checkNumberPad();
    if (padNumber == 16) {
        shiftMode++;
        if(shiftMode > 3) shiftMode = 1;
        if(shiftMode == 1) ledBlue();
        if(shiftMode == 2) ledGreen();
        if(shiftMode == 3) ledRed();
    }
    if (padNumber > 0 && padNumber < 16) {
        int device = CISCO;
        if(shiftMode == 1 && padNumber < 4) device = TV;
        ccr.sendCommand(COMMANDS[shiftMode][padNumber],device);
    }
}

ISR(INT1_vect) {
    touchVal = 0;
    delayMicroseconds(100);
    for (byte i=0; i<=15; i++) {
        SET(PORTD, SCL);
        delayMicroseconds(50);
        touchVal |= (digitalRead(SDO) << i);
        CLR(PORTD, SCL);
        delayMicroseconds(50);
    }
}


When I upload that sketch to my UNO, the display DOES NOT WORK....

HOWEVER, if I REMOVE the array, by commenting out the reference to it in loop like so:
Code: [Select]

//  ccr.sendCommand(COMMANDS[shiftMode][padNumber],device); //Array no longer being referenced, in fact it's not even being compiled into the program since it's never used in code execution.


Then I compile / upload the sketch to the UNO, the display works perfectly.

Now you MIGHT be tempted to blame my Cisco remote library ... after all, commenting out that line also removes references to that library ...But when I remove the comments from the function call and then manually supply two integers into the function call instead of using integers that are in the array like this:

Code: [Select]
ccr.sendCommand(COMMANDS[1][9],device); ... the display still works perfectly!

How is the existence of an array and the act of accessing it .... preventing my SSD1306 display from working?

Go Up