init repo"

This commit is contained in:
Niklas 2018-10-27 15:11:56 +02:00
commit 14cef1aa11

434
led_cube.ino Normal file
View File

@ -0,0 +1,434 @@
/*
* Spalten: Layer:
* 15 14 13 12 0
* 11 10 9 8 1
* 7 6 5 4 2
* 3 2 1 0 3
*
*
*
*
* Spalten: Layer:
* 11 14 13 8 0
* 12 9 10 15 1
* 7 6 5 4 2
* 3 2 1 0 3
*
*/
#include "Arduino.h"
#include <Wire.h>
#include "Adafruit_MCP23017.h"
Adafruit_MCP23017 mcp;
int layer1Pin = 15;
int layer2Pin = 16;
int layer3Pin = 17;
int layer4Pin = 14;
int layers[4] = {layer1Pin, layer2Pin, layer3Pin, layer4Pin};
int cols[4][4] = {{3, 2, 1, 0},{7, 6, 5, 4},{12, 9, 10, 15},{11, 14, 13, 8}};
void setup() {
Serial.begin(9600);
mcp.begin();
for (int i=0; i <= 15; i++){
mcp.pinMode(i, OUTPUT);
mcp.digitalWrite(i, HIGH);
delay(10);
}
pinMode(layer1Pin, OUTPUT);
pinMode(layer2Pin, OUTPUT);
pinMode(layer3Pin, OUTPUT);
pinMode(layer4Pin, OUTPUT);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
}
void loop() {
circle();
mixer(4);
full_scan(2);
stamp(2);
ball(60);
rain(25);
randomflicker(250);
}
void clear() {
for (int i=0; i <= 15; i++){
mcp.digitalWrite(i, HIGH);
}
}
void mixer(int rounds) {
digitalWrite(layer1Pin, LOW);
digitalWrite(layer2Pin, LOW);
digitalWrite(layer3Pin, LOW);
digitalWrite(layer4Pin, LOW);
for(int i=0; i<=rounds; i++){
mcp.digitalWrite(cols[3][1], LOW);
mcp.digitalWrite(cols[3][2], LOW);
mcp.digitalWrite(cols[2][1], LOW);
mcp.digitalWrite(cols[2][2], LOW);
mcp.digitalWrite(cols[1][1], LOW);
mcp.digitalWrite(cols[1][2], LOW);
mcp.digitalWrite(cols[0][1], LOW);
mcp.digitalWrite(cols[0][2], LOW);
delay(165); clear();
mcp.digitalWrite(cols[3][0], LOW);
mcp.digitalWrite(cols[2][1], LOW);
mcp.digitalWrite(cols[1][2], LOW);
mcp.digitalWrite(cols[0][3], LOW);
delay(165); clear();
mcp.digitalWrite(cols[2][0], LOW);
mcp.digitalWrite(cols[2][1], LOW);
mcp.digitalWrite(cols[2][2], LOW);
mcp.digitalWrite(cols[2][3], LOW);
mcp.digitalWrite(cols[1][0], LOW);
mcp.digitalWrite(cols[1][2], LOW);
mcp.digitalWrite(cols[0][1], LOW);
mcp.digitalWrite(cols[1][3], LOW);
delay(165); clear();
mcp.digitalWrite(cols[0][0], LOW);
mcp.digitalWrite(cols[1][1], LOW);
mcp.digitalWrite(cols[2][2], LOW);
mcp.digitalWrite(cols[3][3], LOW);
delay(165); clear();
}
}
void full_scan(int rounds) {
for(int i=0; i<=rounds; i++){
digitalWrite(layer1Pin, LOW);
digitalWrite(layer2Pin, LOW);
digitalWrite(layer3Pin, LOW);
digitalWrite(layer4Pin, LOW);
mcp.digitalWrite(cols[3][0], LOW);
mcp.digitalWrite(cols[2][0], LOW);
mcp.digitalWrite(cols[1][0], LOW);
mcp.digitalWrite(cols[0][0], LOW);
delay(100); clear();
mcp.digitalWrite(cols[3][1], LOW);
mcp.digitalWrite(cols[2][1], LOW);
mcp.digitalWrite(cols[1][1], LOW);
mcp.digitalWrite(cols[0][1], LOW);
delay(100); clear();
mcp.digitalWrite(cols[3][2], LOW);
mcp.digitalWrite(cols[2][2], LOW);
mcp.digitalWrite(cols[1][2], LOW);
mcp.digitalWrite(cols[0][2], LOW);
delay(100); clear();
mcp.digitalWrite(cols[3][3], LOW);
mcp.digitalWrite(cols[2][3], LOW);
mcp.digitalWrite(cols[1][3], LOW);
mcp.digitalWrite(cols[0][3], LOW);
delay(100); clear();
mcp.digitalWrite(cols[3][3], LOW);
mcp.digitalWrite(cols[2][3], LOW);
mcp.digitalWrite(cols[1][3], LOW);
mcp.digitalWrite(cols[0][3], LOW);
delay(100); clear();
mcp.digitalWrite(cols[3][2], LOW);
mcp.digitalWrite(cols[2][2], LOW);
mcp.digitalWrite(cols[1][2], LOW);
mcp.digitalWrite(cols[0][2], LOW);
delay(100); clear();
mcp.digitalWrite(cols[3][1], LOW);
mcp.digitalWrite(cols[2][1], LOW);
mcp.digitalWrite(cols[1][1], LOW);
mcp.digitalWrite(cols[0][1], LOW);
delay(100); clear();
mcp.digitalWrite(cols[3][0], LOW);
mcp.digitalWrite(cols[2][0], LOW);
mcp.digitalWrite(cols[1][0], LOW);
mcp.digitalWrite(cols[0][0], LOW);
delay(100); clear();
for (int i=0; i <= 15; i++){
mcp.digitalWrite(i, LOW);
}
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, LOW);
delay(100);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, LOW);
digitalWrite(layer4Pin, HIGH);
delay(100);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, LOW);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
delay(100);
digitalWrite(layer1Pin, LOW);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
delay(100);
digitalWrite(layer1Pin, LOW);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
delay(100);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, LOW);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
delay(100);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, LOW);
digitalWrite(layer4Pin, HIGH);
delay(100);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, LOW);
delay(100);
clear();
}
}
void ball(int rounds) {
// https://electronics.stackexchange.com/a/67140
// size of the space
int height = 4;
int width = 4;
int depth = 4;
// Starting position of the shape
int xpos = 2;
int ypos = 2;
int zpos = 1;
// Speed of the shape along each axis
int xspeed = 1;
int yspeed = 2;
int zspeed = 1;
int choice[2] = {-1, 1};
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
int zdirection = 1; // front or back
for(int i=0; i<=rounds; i++){
clear();
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
// Update the position of the shape
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
zpos = zpos + ( zspeed * zdirection );
if (xpos >= width || xpos < 0) {
xpos = constrain(xpos, 0, width-1);
xdirection *= -1;
}
if (ypos >= height || ypos < 0) {
ypos = constrain(ypos, 0, height-1);
ydirection *= -1;
}
if (zpos >= depth || zpos < 0) {
zpos = constrain(zpos, 0, depth-1);
zdirection *= -1;
}
if(random(0, 6) == 1) {
xdirection *= -1;
} else if(random(0, 6) == 1) {
ydirection *= -1;
} else if(random(0, 6) == 1) {
zdirection *= -1;
}
digitalWrite(layers[ypos], LOW);
mcp.digitalWrite(cols[xpos][3-zpos], LOW);
delay(150);
}
}
void randomflicker(int rounds) {
for(int i=0; i<=rounds; i++){
clear();
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
int randomLayer = random(0, 4);
int randomLed = random(0, 16);
digitalWrite(layers[randomLayer], LOW);
mcp.digitalWrite(randomLed, LOW);
delay(10);
}
}
void stamp(int rounds) {
for(int i=0; i<=rounds; i++){
digitalWrite(layer1Pin, LOW);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
clear();
for (int i=0; i <= 15; i++){
mcp.digitalWrite(i, LOW);
}
delay(1500);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, LOW);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
delay(50);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, LOW);
digitalWrite(layer4Pin, HIGH);
delay(50);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, LOW);
delay(1000);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, LOW);
digitalWrite(layer4Pin, HIGH);
delay(400);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, LOW);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
delay(400);
}
}
void rain(int rounds) {
for(int i=0; i<=rounds; i++){
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
clear();
mcp.digitalWrite(random(0, 15), LOW);
digitalWrite(layer1Pin, LOW);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
delay(85);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, LOW);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
delay(85);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, LOW);
digitalWrite(layer4Pin, HIGH);
delay(85);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, LOW);
delay(85);
digitalWrite(layer1Pin, HIGH);
digitalWrite(layer2Pin, HIGH);
digitalWrite(layer3Pin, HIGH);
digitalWrite(layer4Pin, HIGH);
delay(150);
}
}
void circle() {
clear();
digitalWrite(layer1Pin, LOW);
digitalWrite(layer2Pin, LOW);
digitalWrite(layer3Pin, LOW);
digitalWrite(layer4Pin, LOW);
mcp.digitalWrite(cols[0][3], LOW); delay(150);
mcp.digitalWrite(cols[0][2], LOW); delay(150);
mcp.digitalWrite(cols[0][1], LOW); delay(150);
mcp.digitalWrite(cols[0][0], LOW); delay(150);
mcp.digitalWrite(cols[1][0], LOW); delay(150);
mcp.digitalWrite(cols[2][0], LOW); delay(150);
mcp.digitalWrite(cols[3][0], LOW); delay(150);
mcp.digitalWrite(cols[3][1], LOW); delay(150);
mcp.digitalWrite(cols[3][2], LOW); delay(150);
mcp.digitalWrite(cols[3][3], LOW); delay(150);
mcp.digitalWrite(cols[2][3], LOW); delay(150);
mcp.digitalWrite(cols[1][3], LOW); delay(150);
mcp.digitalWrite(cols[1][2], LOW); delay(150);
mcp.digitalWrite(cols[1][1], LOW); delay(150);
mcp.digitalWrite(cols[2][1], LOW); delay(150);
mcp.digitalWrite(cols[2][2], LOW); delay(150);
delay(1500);
mcp.digitalWrite(cols[2][2], HIGH); delay(150);
mcp.digitalWrite(cols[2][1], HIGH); delay(150);
mcp.digitalWrite(cols[1][1], HIGH); delay(150);
mcp.digitalWrite(cols[1][2], HIGH); delay(150);
mcp.digitalWrite(cols[1][3], HIGH); delay(150);
mcp.digitalWrite(cols[2][3], HIGH); delay(150);
mcp.digitalWrite(cols[3][3], HIGH); delay(150);
mcp.digitalWrite(cols[3][2], HIGH); delay(150);
mcp.digitalWrite(cols[3][1], HIGH); delay(150);
mcp.digitalWrite(cols[3][0], HIGH); delay(150);
mcp.digitalWrite(cols[2][0], HIGH); delay(150);
mcp.digitalWrite(cols[1][0], HIGH); delay(150);
mcp.digitalWrite(cols[0][0], HIGH); delay(150);
mcp.digitalWrite(cols[0][1], HIGH); delay(150);
mcp.digitalWrite(cols[0][2], HIGH); delay(150);
mcp.digitalWrite(cols[0][3], HIGH); delay(150);
delay(1500);
}