#include "FastLED.h"
#include <VariableTimedAction.h>
// Matrix size
#define NUM_ROWS 16
#define NUM_COLS 16
// Define pins
#define DATA_PIN 3
#define BUTTON_0 9
#define BUTTON_1 10
#define BUTTON_2 11
int x; // Ball x coordinate
int y; //Ball y coordinate
int xDir; // Ball x coordinate
int yDir; //Ball y coordinate
int locY1;
int locY2;
// LED brightness
#define BRIGHTNESS 255
int speed = 50;
// Define the array of leds
#define NUM_LEDS NUM_ROWS * NUM_COLS
CRGB leds[NUM_LEDS];
const uint8_t kMatrixWidth = 16;
const uint8_t kMatrixHeight = 16;
int16_t maxPoints = 11;
// Animation controls
int startFlag = 1;
int sensorValue1 = analogRead(0);
int sensorValue2 = analogRead(5);
int prevVal0 = 0;
int prevVal5 = 0;
#define LAST_VISIBLE_LED 255
uint8_t XY (uint8_t x, uint8_t y) {
// any out of bounds address maps to the first hidden pixel
if ( (x >= kMatrixWidth) | (y >= kMatrixHeight) ) {
return (LAST_VISIBLE_LED + 1);
}
const uint8_t XYTable[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
};
uint8_t i = (y * kMatrixWidth) + x;
uint8_t j = XYTable[i];
return j;
}
class Counter : public VariableTimedAction {
private:
int count = 0;
//this method will be called at your specified interval
unsigned long run() {
//increase the timer
count++;
//return code of 0 indicates no change to the interval
//if the interval must be changed, then return the new interval
return 0;
}
public:
int getCount() {
return count;
}
int setCount(int value) {
count = value;
}
};
Counter secondCounter;
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
Serial.begin(9600);
secondCounter.start(speed);
}
void loop() {
VariableTimedAction::updateActions();
if (secondCounter.getCount() == 10){
secondCounter.stop();
loose(startFlag);
startFlag = 0;
leds[XY(x, y)] = CRGB::Black;
x = x + xDir;
y = y + yDir;
Serial.print(',');
leds[XY(x, y)] = CRGB::Blue;
FastLED.show();
ballMove(x, y, locY1, locY2, xDir, yDir);
Serial.print(secondCounter.getCount());
secondCounter.setCount(0);
secondCounter.start(speed);
}
// void displayBall() {
// leds[XY(x, y)] = CRGB::Blue; //init 2,4
// }
// int playerLoc(int player) {
// //returned value should be 0 to 1024 between (0 and 5v)
// int sensorValue;
// if (player == 1) {
// sensorValue = analogRead(0);
// } else {
// sensorValue = analogRead(5);
// }
// //convert potentiometer values to x value betwen 0 and 6
// int xval = map(sensorValue, 0, 1023, 0, 13);
// xval = constrain (xval, 0, 13);
// return xval;
// }
slides();
// Serial.print(xval);
// const int direction[] = {-1, 1}; //global variable
// int randDir = direction[random(0, 1)]; //where you want a delay
// leds[XY(7, 7)] = CRGB::CRGB::Black;
// leds[XY(7 + randDir, 7)] = CRGB::CRGB::Blue;
prevVal0 = analogRead(0);
prevVal5 = analogRead(5);
}
void slides(){
if (analogRead(0) > 0 & analogRead(0) <= 73 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 0)] = CRGB::Red;
leds[XY(0, 1)] = CRGB::Red;
leds[XY(0, 2)] = CRGB::Red;
FastLED.show();
locY1 = 1;
}
if (analogRead(0) > 73 & analogRead(0) <= 146 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 1)] = CRGB::Red;
leds[XY(0, 2)] = CRGB::Red;
leds[XY(0, 3)] = CRGB::Red;
FastLED.show();
locY1 = 2;
}
if (analogRead(0) > 146 & analogRead(0) <= 219 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 2)] = CRGB::Red;
leds[XY(0, 3)] = CRGB::Red;
leds[XY(0, 4)] = CRGB::Red;
FastLED.show();
locY1 = 3;
}
if (analogRead(0) > 219 & analogRead(0) <= 292 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 3)] = CRGB::Red;
leds[XY(0, 4)] = CRGB::Red;
leds[XY(0, 5)] = CRGB::Red;
FastLED.show();
locY1 = 4;
}
if (analogRead(0) > 292 & analogRead(0) <= 365 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 4)] = CRGB::Red;
leds[XY(0, 5)] = CRGB::Red;
leds[XY(0, 6)] = CRGB::Red;
FastLED.show();
locY1 = 5;
}
if (analogRead(0) > 365 & analogRead(0) <= 438 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 5)] = CRGB::Red;
leds[XY(0, 6)] = CRGB::Red;
leds[XY(0, 7)] = CRGB::Red;
FastLED.show();
locY1 = 6;
}
if (analogRead(0) > 438 & analogRead(0) <= 511 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 6)] = CRGB::Red;
leds[XY(0, 7)] = CRGB::Red;
leds[XY(0, 8)] = CRGB::Red;
FastLED.show();
locY1 = 7;
}
if (analogRead(0) > 511 & analogRead(0) <= 584 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 7)] = CRGB::Red;
leds[XY(0, 8)] = CRGB::Red;
leds[XY(0, 9)] = CRGB::Red;
FastLED.show();
locY1 = 8;
}
if (analogRead(0) > 584 & analogRead(0) <= 657 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 8)] = CRGB::Red;
leds[XY(0, 9)] = CRGB::Red;
leds[XY(0, 10)] = CRGB::Red;
FastLED.show();
locY1 = 9;
}
if (analogRead(0) > 657 & analogRead(0) <= 730 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 9)] = CRGB::Red;
leds[XY(0, 10)] = CRGB::Red;
leds[XY(0, 11)] = CRGB::Red;
FastLED.show();
locY1 = 10;
}
if (analogRead(0) > 730 & analogRead(0) <= 803 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 10)] = CRGB::Red;
leds[XY(0, 11)] = CRGB::Red;
leds[XY(0, 12)] = CRGB::Red;
FastLED.show();
locY1 = 11;
}
if (analogRead(0) > 803 & analogRead(0) <= 876 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
continue;
}
leds[XY(0, 11)] = CRGB::Red;
leds[XY(0, 12)] = CRGB::Red;
leds[XY(0, 13)] = CRGB::Red;
FastLED.show();
locY1 = 12;
}
if (analogRead(0) > 876 & analogRead(0) <= 949 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 12)] = CRGB::Red;
leds[XY(0, 13)] = CRGB::Red;
leds[XY(0, 14)] = CRGB::Red;
FastLED.show();
locY1 = 13;
}
if (analogRead(0) > 949 & analogRead(0) <= 1023 & analogRead(0) != prevVal0){
for (int i = 0; i < 16; i++){
leds[XY(0, i)] = CRGB::Black;
}
leds[XY(0, 13)] = CRGB::Red;
leds[XY(0, 14)] = CRGB::Red;
leds[XY(0, 15)] = CRGB::Red;
FastLED.show();
locY1 = 14;
}
if (analogRead(5) > 0 & analogRead(5) <= 73 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 0)] = CRGB::Red;
leds[XY(15, 1)] = CRGB::Red;
leds[XY(15, 2)] = CRGB::Red;
FastLED.show();
locY2 = 1;
}
if (analogRead(5) > 73 & analogRead(5) <= 146 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 1)] = CRGB::Red;
leds[XY(15, 2)] = CRGB::Red;
leds[XY(15, 3)] = CRGB::Red;
FastLED.show();
locY2 = 2;
}
if (analogRead(5) > 146 & analogRead(5) <= 219 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 2)] = CRGB::Red;
leds[XY(15, 3)] = CRGB::Red;
leds[XY(15, 4)] = CRGB::Red;
FastLED.show();
locY2 = 3;
}
if (analogRead(5) > 219 & analogRead(5) <= 292 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 3)] = CRGB::Red;
leds[XY(15, 4)] = CRGB::Red;
leds[XY(15, 5)] = CRGB::Red;
FastLED.show();
locY2 = 4;
}
if (analogRead(5) > 292 & analogRead(5) <= 365 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 4)] = CRGB::Red;
leds[XY(15, 5)] = CRGB::Red;
leds[XY(15, 6)] = CRGB::Red;
FastLED.show();
locY2 = 5;
}
if (analogRead(5) > 365 & analogRead(5) <= 438 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 5)] = CRGB::Red;
leds[XY(15, 6)] = CRGB::Red;
leds[XY(15, 7)] = CRGB::Red;
FastLED.show();
locY2 = 6;
}
if (analogRead(5) > 438 & analogRead(5) <= 511 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 6)] = CRGB::Red;
leds[XY(15, 7)] = CRGB::Red;
leds[XY(15, 8)] = CRGB::Red;
FastLED.show();
locY2 = 7;
}
if (analogRead(5) > 511 & analogRead(5) <= 584 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 7)] = CRGB::Red;
leds[XY(15, 8)] = CRGB::Red;
leds[XY(15, 9)] = CRGB::Red;
FastLED.show();
locY2 = 8;
}
if (analogRead(5) > 584 & analogRead(5) <= 657 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 8)] = CRGB::Red;
leds[XY(15, 9)] = CRGB::Red;
leds[XY(15, 10)] = CRGB::Red;
FastLED.show();
locY2 = 9;
}
if (analogRead(5) > 657 & analogRead(5) <= 730 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 9)] = CRGB::Red;
leds[XY(15, 10)] = CRGB::Red;
leds[XY(15, 11)] = CRGB::Red;
FastLED.show();
locY2 = 10;
}
if (analogRead(5) > 730 & analogRead(5) <= 803 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 10)] = CRGB::Red;
leds[XY(15, 11)] = CRGB::Red;
leds[XY(15, 12)] = CRGB::Red;
FastLED.show();
locY2 = 11;
}
if (analogRead(5) > 803 & analogRead(5) <= 876 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 11)] = CRGB::Red;
leds[XY(15, 12)] = CRGB::Red;
leds[XY(15, 13)] = CRGB::Red;
FastLED.show();
locY2 = 12;
}
if (analogRead(5) > 876 & analogRead(5) <= 949 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 12)] = CRGB::Red;
leds[XY(15, 13)] = CRGB::Red;
leds[XY(15, 14)] = CRGB::Red;
FastLED.show();
locY2 = 13;
}
if (analogRead(5) > 949 & analogRead(5) <= 1023 & analogRead(5) != prevVal5){
for (int i = 0; i < 16; i++){
leds[XY(15, i)] = CRGB::Black;
}
leds[XY(15, 13)] = CRGB::Red;
leds[XY(15, 14)] = CRGB::Red;
leds[XY(15, 15)] = CRGB::Red;
FastLED.show();
locY2 = 14;
}
FastLED.show();
}
void ballPos(int x, int y, int xDir, int yDir){
// leds[XY(x, y)] = CRGB::Black;
// x = x + xDir;
// y = y + yDir;
// Serial.print(',');
// Serial.print(x);
// Serial.print(',');
// Serial.print(y);
// leds[XY(x, y)] = CRGB::Blue;
// FastLED.show();
}
void ballMove(int x, int y, int locY1, int locY2, int &xDir, int &yDir){
// Serial.println("I am here");
if (y == 15 | y == 0) {
yDir = -yDir;
}
if (x == 14 & (y + yDir == locY2 - 1 | y + yDir == locY2 | y + yDir == locY2 + 1)) {
xDir = -xDir;
}
if (x == 1 & (y + yDir == locY1 - 1 | y + yDir == locY1 | y + yDir == locY1 + 1)) {
xDir = -xDir;
}
// if ((x == 1 | x == 14) & (y == 0 || y == 15) &
if (x == 0 | x == 15) {
FastLED.clear();
delay(1000);
slides();
loose(1);
slides();
}
}
void loose(int startFlag){
if (startFlag == 1){
slides();
delay(1000);
speed = speed - 1;
x = 7;
y = random(2, 13);
// leds[XY(7, 7)] = CRGB::CRGB::Blue;
startFlag = 0;
xDir = random(0, 4);
yDir = random(0, 4);
Serial.println(xDir);
Serial.println(yDir);
if (xDir == 1) {
xDir = 1;
}
else{
xDir = -1;
}
if (yDir == 1) {
yDir = 1;
}
else{
yDir = -1;
}
leds[XY(x, y)] = CRGB::Black;
x = x + xDir;
y = y + yDir;
leds[XY(x, y)] = CRGB::Blue;
FastLED.show();
// int xval = map(sensorValue1, 0, 1023, 0, 15);
// xval = constrain (xval, 0, 16);
// Serial.print(xval);
}
}