Object Oriented Programming and Tabs

I have created the following code to depict a snow scene using an Arduino Nano and an OLED screen.
The code works perfectly but what I'd like to do is to move everything between lines 10 to 47 ( from "Class Snow" to just above "Snow Flake[FLAKE_COUNT];") to a new tab. However, when I do, I start to see errors, for example: "Snow flake[FLAKE_COUNT];" gives:
'snow_test:12:1: error: 'Snow' does not name a type; did you mean 'pow'?
Snow flake[FLAKE_COUNT];'

Snowfall.ino (1.7 KB)

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define FLAKE_COUNT 30

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

class Snow {

public:
  int x, y;
  int radius;
  int speedX;
  int speedY;
  int r = 4;

  Snow() : x(0), y(0), radius(0), speedX(0), speedY(0){}
  
  Snow(int initialX, int initialY, int initialSize, int initialSpeedX, int initialSpeedY)
    : x(initialX+8), y(initialY), radius(initialSize), speedX(initialSpeedX), speedY(initialSpeedY) {} 

  void update() {
  y = y + random(1,4);
  x = x + random(-1, 4);  
  }

  void show() {

  display.drawPixel(x, y, SSD1306_WHITE);
 
  }

  void edges() {
    if (y > SCREEN_HEIGHT) {
      y = 0 + r;
    }
    if (x < -r) {
      x = SCREEN_WIDTH - r;
    }else{
      if (x > SCREEN_WIDTH) {
        x = 0 + r;
      }
    }
  }
};


Snow flake[FLAKE_COUNT];



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

  for (int i = 0; i < FLAKE_COUNT; ++i) {
    flake[i] = Snow(random(SCREEN_WIDTH), (SCREEN_HEIGHT), 4, random(2,5), random(2,5));
  }
}

void loop() {
  display.clearDisplay();
  
  for (int i = 0; i < FLAKE_COUNT; ++i) {
  flake[i].update();
  flake[i].show();
  flake[i].edges();
  }
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(100,64);
  //display.print("THE WINTER");
  display.fillRect(0, 59, 128, 5, WHITE);
  display.fillCircle(100, 54, 10, WHITE);
  display.fillCircle(105, 40, 5, WHITE);
  display.display();
  delay(20);

}

Could anybody point me in the right direction?

Thanks in advance

What name did you give the new tab ?

Hi there!

I called it ‘Classes’

your class is not great as it depends on an externally defined object (the display and its size)


this would probably compile

test.ino

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Snow.h"

const uint8_t SCREEN_WIDTH = 128;
const uint8_t SCREEN_HEIGHT = 64;
const uint8_t FLAKE_COUNT = 30;

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

Snow flake[FLAKE_COUNT];

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(500);
  display.clearDisplay();

  for (int i = 0; i < FLAKE_COUNT; ++i) {
    flake[i] = Snow(random(SCREEN_WIDTH), (SCREEN_HEIGHT), 4, random(2,5), random(2,5));
  }
}

void loop() {
  display.clearDisplay();
  
  for (int i = 0; i < FLAKE_COUNT; ++i) {
  flake[i].update();
  flake[i].show();
  flake[i].edges();
  }
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(100,64);
  //display.print("THE WINTER");
  display.fillRect(0, 59, 128, 5, WHITE);
  display.fillCircle(100, 54, 10, WHITE);
  display.fillCircle(105, 40, 5, WHITE);
  display.display();
  delay(20);

}

Snow.h

#ifndef __SNOW_H__
#define __SNOW_H__
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

extern Adafruit_SSD1306 display;
extern const uint8_t SCREEN_WIDTH;
extern const uint8_t SCREEN_HEIGHT;

class Snow {

  public:
    int x, y;
    int radius;
    int speedX;
    int speedY;
    int r = 4;

    Snow();
    Snow(int initialX, int initialY, int initialSize, int initialSpeedX, int initialSpeedY);
    void update() ;
    void show() ;
    void edges() ;
};

#endif

Snow.cpp

#include "Snow.h"

Snow::Snow() : x(0), y(0), radius(0), speedX(0), speedY(0) {}

Snow::Snow(int initialX, int initialY, int initialSize, int initialSpeedX, int initialSpeedY)
  : x(initialX + 8), y(initialY), radius(initialSize), speedX(initialSpeedX), speedY(initialSpeedY) {}

void Snow::update() {
  y = y + random(1, 4);
  x = x + random(-1, 4);
}

void Snow::show() {
  display.drawPixel(x, y, SSD1306_WHITE);
}

void Snow::edges() {
  if (y > SCREEN_HEIGHT) {
    y = 0 + r;
  }
  if (x < -r) {
    x = SCREEN_WIDTH - r;
  } else {
    if (x > SCREEN_WIDTH) {
      x = 0 + r;
    }
  }
}

1 Like

And the file extension is what ?

That's just brilliant, J-M-L. It's working as intended.
Many thanks to you!!

Just one thing needed to be tweaked:

  1. From 'test.ino', I moved the screen width, height and flake count to the "Snow.h" tab and everything compiled perfectly.

Once again, thank you so much!!!

I have tested your three-tab/file class-based sketch using my NANO-OLED, and it is working perfectly alright producing the same result as the OP's single sketch does.

I am now studying your three-tab/file sketch and will appear soon with queries/questions in order to improve my knowledge on OOP programming.

I hope you like the sketch and, above all, improve on it

this is not such a great OOP approach as your class depends on the display instance to exist... You should pass a reference to the display to your constructor

6 posts were split to a new topic: OOP and Constructors

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