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{
private:
int pos_x = random(0, 128);
int pos_y = random(-5, -20);
int len = random(3, 7);
int speed = random(6, 13);
public:
void fall()
{
display.drawFastVLine(pos_x, pos_y, len, 1);
pos_y = pos_y + speed;
delay(5);
Serial.println(pos_y);
display.display();
display.clearDisplay();
}
};
Drop* drop = new Drop[50];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
}
void loop() {
drop[1].fall();
}
I will appreciate any suggestion or solution, spent almost entire day doing this "simple thing"..
1 Answer 1
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];
.
-
I dont think so, the messier version without objects worked fine with negative values :/ And thank you for the clarification with random!kuskus– kuskus2018年11月11日 19:28:09 +00:00Commented Nov 11, 2018 at 19:28
-
Okay i just used Global array and it seems that it fixed the problem ;). Thank you!kuskus– kuskus2018年11月12日 17:02:38 +00:00Commented Nov 12, 2018 at 17:02
-
strange. what version of IDE you have. is in Question the same sketch you use?2018年11月12日 17:22:09 +00:00Commented Nov 12, 2018 at 17:22
Adafruit_SSD1306
that just prints to the serial port. It seems to work as expected.