Skip to content

Instantly share code, notes, and snippets.

@dropofwill
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dropofwill/2596fab1d9236557d862 to your computer and use it in GitHub Desktop.
Save dropofwill/2596fab1d9236557d862 to your computer and use it in GitHub Desktop.
Arduino LED Matrix Code
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to http://unlicense.org
#include <math.h>
// ^ For atan2() functionality
// anodes
int row[] = {3,5,6,9};
// cathodes
int col[] = {10,11,12,13};
int pot_pin = A0;
int pot_val = 0;
int x = 1;
int y = 1;
int px = 0;
int py = 0;
const int LT = 0;
const int UP_LT = 1;
const int UP = 2;
const int UP_RT = 3;
const int RT = 4;
const int DN_RT = 5;
const int DN = 6;
const int DN_LT = 7;
const int RGT = 0;
const int F_R = 1;
const int FWD = 2;
const int F_L = 3;
const int LFT = 4;
//const float PI = 3.14159;
// starting bar sizes
int bar_sizes[] = {1, 1, 1, 1};
void setup() {
Serial.begin(9600);
led_setup();
lightPin(x,y);
}
void loop() {
// Uncomment whatever you want to run
// Feel free to adjust the values
// lightTest();
// lightIterateTest();
// increment_bars(25);
// boid_steer(300, false, true);
// boid_steer(250, true, false);
// boid_steer(1000, true);
}
// Steer the boid like a car
void boid_steer(int frame_rate, boolean bounce, boolean fine_tune) {
/*
Boid's relative movements
1 2 3
0 ^ 4
LED Layout
| Uno Here |
|__________|
0,0 0,1 0,2 0,3
1,0 1,1 1,2 1,3
2,0 2,1 2,2 2,3
3,0 3,1 3,2 3,3
____/_|___
| ∆
___|_/|___
| |
*/
delay(frame_rate);
int fwd_x = x - px;
int fwd_y = y - py;
pot_val = map(analogRead(pot_pin), 0, 1023, 0, 4);
Serial.println(analogRead(pot_pin));
//Serial.println(pot_val);
double heading = -1 * atan2(-fwd_y, fwd_x);
if (fine_tune) {
switch(pot_val) {
case RGT: heading += PI/2; break;
case F_R: heading += PI/4; break;
case F_L: heading -= PI/4; break;
case LFT: heading -= PI/2; break;
default: break;
}
}
else {
switch(pot_val) {
case RGT: heading += PI/2; break;
case F_R: heading += PI/2; break;
case F_L: heading -= PI/2; break;
case LFT: heading -= PI/2; break;
default: break;
}
}
fwd_x = (int)ceil(cos(heading));
fwd_y = (int)ceil(sin(heading));
Serial.println(heading);
Serial.print(fwd_x);
Serial.print(", ");
Serial.print(fwd_y);
Serial.println("");
if (bounce) {
if (x + fwd_x > 3 || x + fwd_x < 0) {
fwd_x = -1 * fwd_x;
}
if (y + fwd_y > 3 || y + fwd_y < 0) {
fwd_y = -1 * fwd_y;
}
px = x;
py = y;
x = fwd_x + x;
y = fwd_y + y;
}
else {
if (x + fwd_x > 3) {
x = 0;
px = -1;
}
else if (x + fwd_x < 0) {
x = 3;
px = 4;
}
else {
px = x;
x = fwd_x + x;
}
if (y + fwd_y > 3) {
y = 0;
py = -1;
}
else if (y + fwd_y < 0) {
y = 3;
py = 4;
}
else {
py = y;
y = fwd_y + y;
}
}
allOff();
lightPin(x, y);
}
// Steer the boid by pointing it in the pot's absolute direction
void boid_abs() {
/*
Boid's absolute movements
1 2 3
0 ^ 4
7 6 5
LED Layout
| Uno Here |
|__________|
0,0 0,1 0,2 0,3 0 1023
1,0 1,1 1,2 1,3 \/
2,0 2,1 2,2 2,3 pot
3,0 3,1 3,2 3,3
*/
pot_val = map(analogRead(pot_pin), 0, 1023, 0, 7);
Serial.println(pot_val);
switch(pot_val) {
case UP_LT: x--; y++; break;
case UP: y++; break;
case UP_RT: x++; y++; break;
case LT: x--; break;
case RT: x++; break;
case DN_LT: x--; y--; break;
case DN: y--; break;
case DN_RT: x++; y--; break;
}
if (x < 0) {
x = 3;
}
else if (x > 3) {
x = 0;
}
if (y < 0) {
y = 3;
}
else if (y > 3) {
y = 0;
}
Serial.println("Pos:");
Serial.println(x);
Serial.println(y);
allOff();
lightPin(x, y);
}
// Prime all the LED's
// To be run in setup
void led_setup() {
for (int i = 0; i < 4; i++) {
pinMode(row[i], OUTPUT);
}
for (int j = 0; j < 4; j++) {
pinMode(col[j], OUTPUT);
}
allOff();
}
// Variable height LED bars on the matrix
// Increment by ±1
void increment_bars(int iter_delay) {
for (int i = 0; i < 4; i++) {
bar_sizes[i] = new_bar_size(bar_sizes[i], 1, 5);
lightBars(i, bar_sizes[i], 1);
}
for (int i = 0; i < iter_delay; i++) {
lightBars(i%4, bar_sizes[i%4], 1);
}
}
// increase or decrease an old bar size by 1 or -1 based
//on a random number, checking for going to high or low.
int new_bar_size(int old_size, int min_val, int max_val) {
long seed = random(-1, 2);
int new_size = old_size;
Serial.println(seed);
if (seed > 0) {
if (new_size < max_val) {
new_size++;
}
}
else {
if (new_size > min_val) {
new_size--;
}
}
return new_size;
}
void lightBars(int row_index, int bar_size, int delay_time) {
// Serial.print(row_index);
allOff();
//rowOff(row_index);
for (int i = 0; i < bar_size; i++) {
lightPin(row_index, i);
delay(delay_time);
}
}
// Light everything up one at a time
void lightIterateTest() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
delay(500);
allOff();
lightPin(i, j);
}
}
}
// Light everything up, will be dull
void lightTest() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
allOff();
lightPin(i, j);
}
}
}
// Light an individual pin
// Needs to be turned off with allOff()
// or by incompleting the circuit the
// opposite way that it was turned on here
void lightPin(int i, int j) {
digitalWrite(row[i], HIGH);
digitalWrite(col[j], LOW);
}
// Turn off a given row
void rowOff(int row_index) {
digitalWrite(row[row_index], LOW);
for (int i = 0; i < 4; i++) {
digitalWrite(col[i], HIGH);
}
}
// Shortcut to turn off all the LEDs
void allOff() {
for (int i = 0; i < 4; i++) {
digitalWrite(row[i], LOW);
}
for (int i = 0; i < 4; i++) {
digitalWrite(col[i], HIGH);
}
}
@dropofwill
Copy link
Author

Explanatory post available on my blog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment