Adafruit i2c OLED Display Inconsistencies

Hi! Thanks for the response. I'm sorry if I seem obtuse, I'm trying to understand.
Here's what my code looks like if I try static variables:

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ArduinoSTL.h>

std::vector<int> trail{0, 0, 8, 0, 16, 0, 24, 0}; // list of coordinates of the snake's tail

Adafruit_SSD1306 display(128, 64, &Wire, 4);

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // initialse display
    Serial.println("Fail");
    for(;;);
  }
  display.clearDisplay();
  display.display();
}

void loop() {
  static int posx = 0; // position of head of snake
  static int posy = 0;
  
  static int joyx = 0; // joystick position
  static int joyy = 0;
  
  static int fx = random(0, 17); // fruit position
  static int fy = random(0, 17);
  
  static int facing = 2; // snake movement direction
  static int timer = 0; // snake movement timer
  
  joyx = analogRead(A0);
  joyy = analogRead(A1);
  if (joyx > 800 && abs(joyy - 512) < 200 && facing != 0) { // if right
    facing = 2;
  }
  if (joyy < 200 && abs(joyx - 512) < 200 && facing != 3) { // if down
    facing = 1;
  }
  if (joyx < 200 && abs(joyy - 512) < 200 && facing != 2) { // if left
    facing = 0;
  }
  if (joyy > 800 && abs(joyx - 512) < 200 && facing != 1) { // if up
    facing = 3;
  }
  timer++;
  if (timer == 20) { // every 200ms ish
    timer = 0;
    if (facing == 0 || facing == 2) { // change snake head position
      posx = posx + ((facing - 1) * 8);
    } else {
      posy = posy + ((facing - 2) * 8);
    }
    trail.push_back(posx); // move snake forward
    trail.push_back(posy);
    trail.erase(trail.begin()); // remove last square of snake
    trail.erase(trail.begin());
    display.clearDisplay();
    for (int i = 0; i < trail.size(); i = i + 2) { // display all snake segments
      display.fillRect(trail[i], trail[i + 1], 8, 8, SSD1306_WHITE);
    }
    display.drawCircle((fx * 8) + 4, (fy * 8) + 4, 4, SSD1306_WHITE); // draw the fruit
    display.drawRect(0, 0, 128, 64, SSD1306_WHITE); // draw the game bounding box
    display.display(); // refresh display
  }
  delay(10);
}

This still causes the serial monitor to print "fail".

You don't seem to know what code that you say failed to post. It is a bit depressing you can't seem to be able to cooperate with us.

So I have modified the code from your first original post, which you said works to include the suggestion that was made to you about where to put the random initialisation of the fruit position. And also to halt the code when the OLDE fails to initialise.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ArduinoSTL.h>

int posx = 0; // position of head of snake
int posy = 0;

int joyx = 0; // joystick position
int joyy = 0;

int fx = 2; // fruit position
int fy = 0;

int facing = 2; // snake movement direction
int timer = 0; // snake movement timer

std::vector<int> trail{0, 0, 8, 0, 16, 0, 24, 0}; // list of coordinates of the snake's tail

Adafruit_SSD1306 display(128, 64, &Wire, 4);

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  fx = random(0, 16);
  fy = random(0, 16);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // initialse display
    Serial.println("Fail");
    Serial.println(fx);
    while(1) { } ; // do not go on from this point
  }
  display.clearDisplay();
  display.display();
}

void loop() {
  joyx = analogRead(A0);
  joyy = analogRead(A1);
  if (joyx > 800 && abs(joyy - 512) < 200 && facing != 0) { // if right
    facing = 2;
  }
  if (joyy < 200 && abs(joyx - 512) < 200 && facing != 3) { // if down
    facing = 1;
  }
  if (joyx < 200 && abs(joyy - 512) < 200 && facing != 2) { // if left
    facing = 0;
  }
  if (joyy > 800 && abs(joyx - 512) < 200 && facing != 1) { // if up
    facing = 3;
  }
  timer++;
  if (timer == 20) { // every 200ms ish
    timer = 0;
    if (facing == 0 || facing == 2) { // change snake head position
      posx = posx + ((facing - 1) * 8);
    } else {
      posy = posy + ((facing - 2) * 8);
    }
    trail.push_back(posx); // move snake forward
    trail.push_back(posy);
    trail.erase(trail.begin()); // remove last square of snake
    trail.erase(trail.begin());
    display.clearDisplay();
    for (int i = 0; i < trail.size(); i = i + 2) { // display all snake segments
      display.fillRect(trail[i], trail[i + 1], 8, 8, SSD1306_WHITE);
    }
    display.drawCircle((fx * 8) + 4, (fy * 8) + 4, 4, SSD1306_WHITE); // draw the fruit
    display.drawRect(0, 0, 128, 64, SSD1306_WHITE); // draw the game bounding box
    display.display(); // refresh display
  }
  delay(10);
}

Hi! I don't quite think that's called for, I am trying my best here. I directly copied that code in and uploaded it and it still didn't work, printing "Fail. 7" to the serial monitor. However, I am getting a new error:

...\Arduino\libraries\ArduinoSTL-master\src\limits:24:2: warning: #warning limits header is nowhere complete or accurate [-Wcpp]
 #warning limits header is nowhere complete or accurate
  ^~~~~~~

If you could shed some light on what this means that would be appreciated :slight_smile:

Go on. You have noted that the message is "Fail".

The only way to get this is via

The reason for begin() to fail is shortage of SRAM. Adafruit_SSD1306 library allocates 1024 byte buffer at runtime.

I suspect that your STL methods are gobbling up SRAM memory.

I tried to build both of your sketches from #19 in the Web Editor.
Both failed to compile with this message.

new.cpp.o (symbol from plugin): In function `operator new(unsigned int)':

(.text+0x0): multiple definition of `std::nothrow'

/tmp/arduino-build-B26EF3A139B5BEECA4541E00A9823999/libraries/arduinostl_1_3_3/new_handler.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2: error: ld returned 1 exit status

Error during build: exit status 1

which implies that there is something wrong with the STL library code.

David.

Tysm! I replaced my stl library with another one specifically for containers like vectors (arxcontainer) and now it works perfectly! Thank you all for your help :)

Which STL library did you use ?

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