I just got an Idea of trying to code rain simulation with arduino and Adafruit OLED display. When I finished it was quite messy code so I thought That maybe doing it Object Oriented would help. This code is just a sketch that MAYBE works. The thing is that I need to create an array of objects(drops) and then display them. I have tried to display just one drop and it does not seem to do anything, even Serial wont print the pos_y. I have never wrote an Arduino code with objects so I just made a quick research and tried. here is the code:

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

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

class Drop{
  int pos_x = random(0, 128);
  int pos_y = random(-5, -20);
  int len = random(3, 7); 
  int speed = random(6, 13);
 void fall()
 display.drawFastVLine(pos_x, pos_y, len, 1);
 pos_y = pos_y + speed;


Drop* drop = new Drop[50];

void setup() {
// put your setup code here, to run once:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

void loop() {

I will appreciate any suggestion or solution, spent almost entire day doing this "simple thing"..

  • 2
    I tried your code using a dummy implementation of Adafruit_SSD1306 that just prints to the serial port. It seems to work as expected. – Edgar Bonet Nov 11 '18 at 12:24
  • can the display show negative y position? becose it start with -5 (you don't initialize random) – Juraj Nov 11 '18 at 15:41
  • Well, cutting out all the display code the program appears to work fine for me. – Majenko Nov 11 '18 at 15:56
  • It may well be then that the display driver hates a negative Y value. Not using that library ever I can't say... Personally I'd never use an Arduino for anything to do with a display... – Majenko Nov 11 '18 at 16:03
  • do you have 9600 baud set in Serial Monitor? add some prints to see where the code hangs – Juraj Nov 12 '18 at 6:24

The code works without the display commands. A problem could be that you start with negative pos_y and use it as parameter for drawFastVLine.

Function random returns first parameter if it is a larger value then the second parameter. random(-5, -20) always returns -5.

To have more random random values, you should call randomSeed in setup().

And you can use global array Drop drop[50];.

| improve this answer | |
  • I dont think so, the messier version without objects worked fine with negative values :/ And thank you for the clarification with random! – kuskus Nov 11 '18 at 19:28
  • Okay i just used Global array and it seems that it fixed the problem ;). Thank you! – kuskus Nov 12 '18 at 17:02
  • strange. what version of IDE you have. is in Question the same sketch you use? – Juraj Nov 12 '18 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.