Adafruit_HT1632 Matrix Displays and sine wave graphic

HI,

Trying to draw a nice sinewave on this display:

Got one pixel bouncing along, but when I try to get a full waveform drawn rather than just a single pixel, but can't seem to get there! HALP, it must be simple, right?

#include "Adafruit_HT1632.h"

#define HT_DATA 2
#define HT_WR   3
#define HT_CS   4
#define HT_CS2  5


int angle = 0;

// use this line for single matrix
Adafruit_HT1632LEDMatrix matrix = Adafruit_HT1632LEDMatrix(HT_DATA, HT_WR, HT_CS);
// use this line for two matrices!
//Adafruit_HT1632LEDMatrix matrix = Adafruit_HT1632LEDMatrix(HT_DATA, HT_WR, HT_CS, HT_CS2);

void setup() {
  Serial.begin(9600);
  matrix.begin(ADA_HT1632_COMMON_16NMOS);
  matrix.fillScreen();
  delay(500);
  matrix.clearScreen();
}

void loop() {


      for (int x = 23; x >= 0; x--)
      {
        int y = 8 * sin(angle * 3.141 / 180);
        y = 8 - y;
        matrix.setPixel(x, y);
        matrix.writeScreen();
        delay(3);
        //matrix.clrPixel(x, y);


        
        matrix.clearScreen();
        angle += 360 / 24;
      }

}
int angle = 0;

int?

      for (int x = 23; x >= 0; x--)
      {

Why are you doing it backwards?

        y = 8 - y;

Why is this necessary?

Why don't you draw all the pixels, in setup(), to see that your calculations are right? You do know what makes the pixels un-draw, right?

Thanks for replying..

If not int, what's a better data type? Byte caps at 255... Float? Sorry, clueless...

Backwards? In a previous attempt, I had it going 'forwards' and it does the same thing but forwards... I don't see why this matters?

y = 8 - y; - This centres the wave on the y-axis. y += 8 is better? Or is there a better way to do this?

Drawing the pixels in setup shows me a nice wave, and as a static wave it looks ok...

To lear pixels iss - matrix.clrPixel(x, y); right? If I use that, I still get a single pixel animating across the wave.

I've just tried wrapping the for loop in another which adds to the x-axis to try and'move' the wave across the screen, but it just gradually disappears off screen...

Apologies, but I'm mighty confused!

If not int, what's a better data type? Byte caps at 255... Float? Sorry, clueless...

I'd expect to see angles expressed as floats. YMMV.

Backwards? In a previous attempt, I had it going 'forwards' and it does the same thing but forwards... I don't see why this matters?

It probably doesn't. But, I read left to right. I expect to light pixels the same way, in the absence of compelling reasons to do otherwise.

y = 8 - y; - This centres the wave on the y-axis. y += 8 is better? Or is there a better way to do this?

   int y = 8 - (8 * sin(angle * 3.141 / 180));

Drawing the pixels in setup shows me a nice wave, and as a static wave it looks ok...

OK. That means that the calculation is correct.

I've just tried wrapping the for loop in another which adds to the x-axis to try and'move' the wave across the screen, but it just gradually disappears off screen...

When it gets to the end, make it start at x=0 again...

OK, well this is the code with the extra for loop as described, and the other improvements you've suggested... Not sure how to make it start at x=0 again... I thought that was taken care of by the for loop, when slide > 23, it loops back to 0, or am I really being dense?

This is the closest I've got so far... The display is till very'flickery', I mean, you can see the re-draw very clearly?

void setup() {
  Serial.begin(9600);
  matrix.begin(ADA_HT1632_COMMON_16NMOS);

}

void loop() {

  sine();

}

void sine() {

  for (int slide = 0; slide < 23; slide++) {
    for (int x = 0; x <= 23; x++)
    {
      int y = 8 - (8 * sin(angle * 3.141 / 180));

      matrix.setPixel(x + slide, y);
      matrix.writeScreen();

      matrix.clrPixel(x + slide, y);
      angle += 360 / 24;


    }
  }
}
      matrix.setPixel(x + slide, y);

This statement uses values from both loops, so the X location increases from 0 to 44. Is that what you expect?

I don't like magic numbers, like 23. Surely that is supposed to relate to some physical characteristic of the matrix, in which case there should be a const byte or #define statement that gives the value a name, and the name should be used in the code.

I've removed that magic number and used matrix.width().

      matrix.setPixel(x + slide, y);

Well, it's what I expect, I guess it's not what I want!

The following is closer to what I expect and want, but, although the wave 'phase'(?) is moving across the screen, the re-draw is still very obvious, it's not a smooth animation...

I'm using clearScreen() rather than clrPixel(), as clrPixel() just gives me one pixel animating rather than the full waveform... Any advice?

Thanks!

#include "Adafruit_HT1632.h"
#include "math.h"
#define HT_DATA 2
#define HT_WR   3
#define HT_CS   4
#define HT_CS2  5

#define PI 3.1415926535897932384626433832795


float angle = 0;

// use this line for single matrix
Adafruit_HT1632LEDMatrix matrix = Adafruit_HT1632LEDMatrix(HT_DATA, HT_WR, HT_CS);

void setup() {
  Serial.begin(9600);
  matrix.begin(ADA_HT1632_COMMON_16NMOS);

}

void loop() {

  sine();
}

void sine() {
  for (int x = 0; x <= matrix.width(); x++)

  {
    int y = 8 - (8 * sin(angle * PI / 180));

    matrix.setPixel(x, y);
    matrix.writeScreen();
    angle += 360 / 24;
  }
  
  matrix.clearScreen();
}