Example Code for Arduino-A color filled circle
This article offers a comprehensive guide on using Arduino to create a color-filled circle on a 32x32 RGB LED matrix panel, covering hardware and software preparations, wiring, and including sample code for effective implementation.
Hardware Preparation
- DFRduino UNO R3 x1
- DFR0499 x1
- DuPont cables
Software Preparation
- Arduino IDE Click to Download Arduino IDE from Arduino®
- Adafruit-GFX-Library
- RGB-matrix-Panel
- How to install the library?
Wiring Diagram
Note: It needs an external power supply, the USB is only 5V@500mA, not enough power.
Other Preparation Work
Control signal pin connected to MEGA2560:
R1-> 24 G1-> 25 B1-> 26
R2-> 27 G2-> 28 B2-> 29
HA-> A0 HB-> A1 HC-> A2 HD-> A3 HE-> A4
OE-> 9 LAT-> 10 CLK-> 11
GND-> GND
Power Interface:
-5V -> +5V DC power supply positive
-5V -> +5V DC power supply positive
GND-> DC power supply negative
GND-> DC power supply negative
Sample Code
/***************************************************
*
* For 32x32 RGB LED matrix.
*
* @author lg.gang
* @version V1.0
* @date 2016-10-28
*
* GNU Lesser General Public License.
* See <http://www.gnu.org/licenses/> for details.
* All above must be included in any redistribution
* ****************************************************/
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
// If your 32x32 matrix has the SINGLE HEADER input,
// use this pinout:
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
// If your matrix has the DOUBLE HEADER input, use:
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
//#define LAT 9
//#define OE 10
//#define A A3
//#define B A2
//#define C A1
//#define D A0
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
void setup() {
int x, y, hue;
float dx, dy, d;
uint8_t sat, val;
uint16_t c;
matrix.begin();
for(y=0; y < matrix.width(); y++) {
dy = 15.5 - (float)y;
for(x=0; x < matrix.height(); x++) {
dx = 15.5 - (float)x;
d = dx * dx + dy * dy;
if(d <= (16.5 * 16.5)) { // Inside the circle(ish)?
hue = (int)((atan2(-dy, dx) + PI) * 1536.0 / (PI * 2.0));
d = sqrt(d);
if(d > 15.5) {
// Do a little pseudo anti-aliasing along perimeter
sat = 255;
val = (int)((1.0 - (d - 15.5)) * 255.0 + 0.5);
} else
{
// White at center
sat = (int)(d / 15.5 * 255.0 + 0.5);
val = 255;
}
c = matrix.ColorHSV(hue, sat, val, true);
} else {
c = 0;
}
matrix.drawPixel(x, y, c);
}
}
}
void loop() {
// do nothing
}
Result
A color filled circle
Was this article helpful?
