Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save john212/1420e8eda57d2eb2dbba to your computer and use it in GitHub Desktop.
Save john212/1420e8eda57d2eb2dbba to your computer and use it in GitHub Desktop.
/**************************************************
Udemy Arduino Step-by-Step Course
Section 4 Displays Lecture 45
Using 8x8 LED Matrix - MAX7219 Controller - ADXL335 3-Axis Accelerometer
by J. Cantlin
January 30, 2016
***************************************************/
/**************************************************
Description of Program
An 8x8 LED Matrix driven by a MAX7219 controller
displays an output proportional to the signal
produced when tilting a ADXL335 3-axis accelerometer
made by Analog Devices. It is a +- 3g x,y,z 3-axis
accelerometer. Only the x and y axis signals are
needed for this two-dimensional 8x8 LED Matrrix.
***************************************************/
/**************************************************
Summary of Arduino Analog Pins Used:
A0 = ADXL335 X-axis Sensor
A1 = ADXL335 Y-axis Sensor
A2 =
A3 =
A4 =
A5 =
***************************************************/
/**************************************************
Summary of Arduino Digital Pins Used:
00 =
01 =
02 =
03 =
04 =
05 =
06 =
07 =
08 =
09 =
10 = CS = MAX7219 chip select = device's SPI identifier
11 = DIN Pin on MAX7219 = MOSI = common to all SPI devices
12 = CLK Pin on MAX7219 = SCLK = common to all SPI devices
13 =
***************************************************/
/*********************************************************************
SPI - Serial Peripheral Interconnect Protocol
The 4 signal lines are:
– clock (SCLK), from the bus master to all slaves; all the SPI signals are synchronous to this clock signal
– A data line from the master to the slaves, named MOSI (Master Out-Slave In)
– A data line from the slaves to the master, named MISO (Master In-Slave Out)
– slave select (or chip select) (SSx/CSx) for EACH slave, to select the slave the master communicates with
Arduino hardware supports SPI as follows:
- Uno: MOSI Pin 11, MISO Pin12, SCK Pin 13, SS Pin 10
- Mega: MOSI Pin 51, MISO Pin50,SCK Pin 52, SS Pin 53
- Note: MISO not needed for read only LCD, Only one SPI so SS not required.
The DC pin (data/command) is used to select between command mode (low) and data mode (high).
SPI Libraries:
Must include SPI.h and "begin" the SPI using SPI.begin() in setup. This initializes
the SPI bus by setting SCK, MOSI and SS to outputs, pulling SCK and MOSI LOW and
SS HIGH.
***********************************************************************/
/*********************************************************************
The x,y axes are in the horizontal plane, if you place the breakout
board flat on the table, the x axis will be defined as + to the right
and - to the left. The y axis will be defined as + away from you and
- towards you. The z axis is in the vertical plane and the z axis
will be defined as + up and - down. The g forces versus
measured analog readings for the sensor expected/actual:
+x = +1g = expected 262 analog reading for x-axis use 326+-66. 326=zero g
-x = -1g = expected 392 analog reading
+y = +1g = expected 263 analog reading for y-axis use 325+-66. 326=zero g
-y = -1g = expected 395 analog reading
+z = +1g = expected 286 analog reading for z-axis use 351+-67. 350=zero g
-z = -1g = expected 418 analog reading
Adjust (calibrate) program as needed for these actual values.
actual 0g for x-axis = 268 to 400
actual 0g for y-axis = 269 to 402
***********************************************************************/
/**********************************************************************
The map funtion "maps" one range of values to another:
map(value, fromLow, fromHigh, toLow, toHigh).
The constrain function can be used to limit the range to a specified
minimum and maximum: constrain(x, min, max).
*********************************************************************/
/**************************************************
The 8x8 LED Matrix is driven by a MAX7219 Controller Using
SPI to communicate with the Arduino. Since the LEDs are OUTPUT
only they only get a MOSI signal for data.
Libraries:
SPI.h
for serial communication using SPI.
Adafruit_GFX.h and Max72xxPanel.h
the Max72xxPanel library implements Adafruit's
GFX library for multiple 8x8 LED displays driven by a
MAX7219 or MAX7221, in arbitrary rectangular layouts.
***************************************************/
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
int pinCS = 10; // Attach CS to this pin, DIN to MOSI and CLK to SCK
Max72xxPanel matrix = Max72xxPanel(pinCS, 1, 1);
/**************************************************/
//declare global variables
int x, y;
void setup() {
Serial.begin(9600);
matrix.setIntensity(0);
}
void loop() {
x = analogRead(0); // read analog input pin 0
y = analogRead(1); // read analog input pin 1
Serial.print("accelerations are x, y: ");
Serial.print(x, DEC);
Serial.print(" ");
Serial.println(y, DEC);
int matrix_y = map(y, 268, 400, 7, 0); // Maps x,y values to 7,0
int matrix_x = map(x, 269, 402, 0, 7);
matrix.fillScreen(0); //Clear the screen
matrix.drawPixel(matrix_x, matrix_y, HIGH);
matrix.write(); // Send bitmap to display
delay(50); // wait 50ms for next reading
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment