Sorry, I was busy yesterday. Here is the full sketch. Dont say I didnt warn you
As I said, the problem seems to be in the sap() function.
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 221
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);
//electron paths and variables
int paths[7][69] = {
{ 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, 300, 300 },
{ 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, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 55, 56, 57 },
{ 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, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 55, 56, 57, 300, 300 },
{ 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, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 55, 56, 57, 300, 300 },
{ 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, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 55, 56, 57 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 7, 7, 0, -8, -8, -8, -8, -8, -8, -8, -8, 0, 0, 0, 0 }
};
int dety[36];
int beamline[136] = { 0 };
//tritium paths
int trit[3][14] = {
{ 200, 203, 206, 198, 6, 12, 196, 211, 208, 15, 18, 218, 217, 214 },
{ 201, 204, 207, 8, 5, 10, 13, 197, 210, 16, 19, 219, 216, 213 },
{ 202, 205, 199, 7, 4, 11, 14, 212, 209, 17, 20, 220, 215, 212 }
};
//game variables
int gameboard[5] = { 18, 13, 10, 7, 5 }; //size of the target area
int scoreboard[5] = { 0, 1, 9, 21, 37 };
int difficulty[5] = { 0, 5, 10, 12, 15 }; //game speed
int stage = 0;
//SAP variables
int spectrometer[4][5][21] = {
{ { 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78 },
{ 35, 36, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 54 },
{ 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55 },
{ 35, 36, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 54 },
{ 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135 } },
{ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
{ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1 } }
};
float bdet = 10;
int bcount = 0;
void setup() {
pinMode(3, INPUT);
pixels.begin();
Serial.begin(9600);
delay(1000);
}
void loop() {
//Serial.println("Here I work fine");
detectorwipe(50);
//Serial.println("Here I reset");
if (digitalRead(3) == HIGH) game();
for (int i = 0; i < 3; i++) {
if (digitalRead(3) == HIGH) return;
egun(20);
}
delay(1000);
for (int j = 0; j < 3; j++) {
tritium(80, 20);
if (digitalRead(3) == HIGH) return;
delay(1000);
decay(160);
if (digitalRead(3) == HIGH) return;
delay(1000);
decay(160);
if (digitalRead(3) == HIGH) return;
delay(1000);
for (int i = 0; i < 8; i++) {
if (digitalRead(3) == HIGH) return;
if (random(10) < 8) electron(random(55, 90), random(5));
else electron(-random(60, 80), random(5));
delay(random(500, 1500));
}
}
detectorwipe(50);
if (digitalRead(3) == HIGH) return;
for (int i = 0; i < 200; i++) sap(100, 0);
if (digitalRead(3) == HIGH) return;
for (int i = 0; i < 200; i++) sap(100, 1);
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(00, 00, 00));
pixels.show();
delay(1);
}
delay(1000);
}
void game() {
for (int i = 0; i < 58; i++) {
pixels.setPixelColor(i, pixels.Color(0, 50, 0));
if (i + gameboard[stage] > 58) pixels.setPixelColor(i, pixels.Color(35, 00, 0));
}
pixels.show();
int dir = 1;
int pos = 0;
while (digitalRead(3) == HIGH) delay(100);
while (1) {
for (int i = 0; i < 58; i++) {
pixels.setPixelColor(i, pixels.Color(0, 50, 0));
if (i + gameboard[stage] > 58) pixels.setPixelColor(i, pixels.Color(35, 00, 0));
}
pixels.setPixelColor(pos, pixels.Color(0, 0, 150));
pixels.show();
delay(20 - difficulty[stage]);
if (dir == 1) pos++;
else pos--;
if (pos > 56) dir = 0;
if (pos < 1) dir = 1;
if (digitalRead(3) == HIGH) {
if (pos + gameboard[stage] -1 > 58) {
delay(500);
gamewipe();
electron(95 + pos - 58, random(5));
if (stage == 4) {
win();
stage = 0;
break;
}
stage++;
pos = 0;
dir = 1;
showscoreboard();
delay(500);
} else {
stage = 0;
delay(500);
gamewipe();
if (pos > 25) electron(pos + 10, random(5));
else electron(pos - 40, random(5));
loose();
break;
}
}
}
}
void gamewipe() {
for (int i = 0; i < 58; i++) pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
}
void loose() {
delay(1000);
for (int i = 0; i < 173; i++) {
pixels.setPixelColor(i, pixels.Color(00, 50, 00));
pixels.show();
delay(10);
}
delay(1000);
for (int i = 0; i < NUMPIXELS; i++) pixels.setPixelColor(i, pixels.Color(00, 00, 00));
pixels.show();
delay(1000);
}
void win() {
delay(2000);
for (long i = 0; i < 2 * 65536; i += 256) {
pixels.rainbow(i, 4, 255, 80, false);
pixels.show();
}
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(00, 00, 00));
pixels.show();
delay(10);
}
}
void showscoreboard() {
for (int i = 0; i < 37; i++) {
if (i < scoreboard[stage]) pixels.setPixelColor(i + 136, pixels.Color(0, 0, 100));
else pixels.setPixelColor(i + 136, pixels.Color(0, 0, 0));
}
}
void detectorwipe(int speed) {
for (int i = 37; i > 0; i--) {
pixels.setPixelColor(i + 135, pixels.Color(0, 0, 0));
pixels.show();
dety[i] = 0;
delay(speed);
}
}
void sap(int speed, int sapmode) {
if (random(0, 3) == 0) {
int x = random(0, 5);
int y = random(0, 21);
if (spectrometer[2][x][y] == 0) spectrometer[2][x][y] = 100;
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 21; j++) {
switch (spectrometer[2][i][j]) {
case 100:
if (j == 20) bcount++;
if (sapmode == 1) spectrometer[2][i][constrain(j + spectrometer[3][i][j], 0, 20)] = 100;
else spectrometer[2][i][constrain(j + spectrometer[1][i][j], 0, 20)] = 100;
spectrometer[2][i][j] = 50;
if (spectrometer[3][i][j] == 1 && sapmode == 1) j++;
else if (spectrometer[1][i][j] == 1 && sapmode == 0) j++;
break;
case 50:
spectrometer[2][i][j] = 20;
break;
case 20:
spectrometer[2][i][j] = 0;
break;
default:
break;
}
}
}
if (sapmode == 1)
for (int i = 0; i < 5; i++)
for (int j = 0; j < 21; j++) pixels.setPixelColor(spectrometer[0][i][j], pixels.Color(spectrometer[2][i][j], max(0, 10 * spectrometer[3][i][j]), max(0, -10 * spectrometer[3][i][j])));
else
for (int i = 0; i < 5; i++)
for (int j = 0; j < 21; j++) pixels.setPixelColor(spectrometer[0][i][j], pixels.Color(spectrometer[2][i][j], max(0, 10 * spectrometer[1][i][j]), max(0, -10 * spectrometer[1][i][j])));
pixels.show();
delay(speed);
bdetector();
}
void bdetector() {
bdet += bcount;
bcount = 0;
bdet -= 0.015 * bdet;
for (int i = 0; i < 16; i++) {
if (i < bdet) pixels.setPixelColor(i + 157, pixels.Color(0, 100, 0));
else pixels.setPixelColor(i + 157, pixels.Color(0, 0, 0));
}
pixels.show();
}
void egun(int speed) {
if (digitalRead(3) == HIGH) return;
int pix = random(136, 144);
for (int i = 0; i < 20; i++) {
pixels.setPixelColor(0, pixels.Color(2 * i, 7 * i, 0));
pixels.show();
delay(speed);
}
for (int i = 0; i < 57; i++) {
pixels.setPixelColor(paths[0][i], pixels.Color(20, 80, 0));
pixels.show();
delay(speed);
}
delay(5 * speed);
pixels.setPixelColor(pix, pixels.Color(30, 100, 0));
pixels.show();
delay(speed);
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
pixels.show();
delay(speed);
for (int i = 0; i < 57; i++) {
pixels.setPixelColor(paths[0][i], pixels.Color(0, 0, 0));
pixels.show();
delay(speed);
}
pixels.setPixelColor(pix, pixels.Color(0, 0, 0));
pixels.show();
delay(speed);
}
void tritium(int speed, int length) {
if (digitalRead(3) == HIGH) return;
for (int l = 0; l < length; l++) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 14; j++) pixels.setPixelColor(trit[i][j], pixels.Color(0, 0, 150));
pixels.show();
delay(speed);
for (int j = 0; j < 14; j++) pixels.setPixelColor(trit[i][j], pixels.Color(0, 0, 0));
pixels.show();
}
}
}
void detector() {
int rannum;
for (int i = 0; i < 10; i++) {
rannum = random(36);
if (dety[rannum] == 0) break;
}
dety[rannum] = 80;
pixels.setPixelColor(rannum + 136, pixels.Color(dety[rannum] * 0.8, dety[rannum], 0));
pixels.show();
}
//speed s, spectrometer path p
void electron(int s, int p) {
int ele = random(5, 13);
pixels.setPixelColor(paths[p][ele], pixels.Color(100, 100, 100));
pixels.show();
delay(100 - abs(s));
for (int i = ele; i < 59; i++) {
s -= paths[6][i];
beam(paths[p][i]);
delay(100 - abs(s));
if (s < 0) i -= 2;
if (i < -1) break;
}
for (int i = 0; i < 3; i++) {
beam(0);
delay(100 - abs(s));
}
if (s > 50) {
detector();
}
}
void beam(int led) {
if (led > 0) beamline[led] = 100;
for (int i = 1; i < 136; i++) {
pixels.setPixelColor(i, pixels.Color(beamline[i] * 60 / 100, beamline[i] * 100 / 100, 0));
if (i == 57) pixels.setPixelColor(57, pixels.Color(0, beamline[i] * 150 / 100, 0));
if (i == 1) pixels.setPixelColor(1, pixels.Color(0, beamline[i] * 150 / 100, 0));
if (beamline[i] > 0) beamline[i] /= 3;
if (beamline[i] < 10) beamline[i] = 0;
}
pixels.show();
}
void decay(int speed) {
if (digitalRead(3) == HIGH) return;
for (int i = 173; i < 177; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 150));
pixels.show();
delay(speed);
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
}
for (int i = 0; i < 10; i++) {
pixels.setPixelColor(177, pixels.Color(100, 100, 100));
pixels.show();
delay(speed / 5);
pixels.setPixelColor(177, pixels.Color(0, 0, 0));
pixels.show();
delay(speed / 5);
}
for (int i = 0; i < 48; i++) {
if (i % 6 == 0) {
pixels.setPixelColor(178 + (i / 6) - 1, pixels.Color(0, 0, 0));
pixels.setPixelColor(178 + (i / 6), pixels.Color(0, 120, 0));
}
if (i % 8 == 0) {
pixels.setPixelColor(186 + (i / 8) - 1, pixels.Color(0, 0, 0));
pixels.setPixelColor(186 + (i / 8), pixels.Color(60, 100, 0));
}
if (i % 12 == 0) {
pixels.setPixelColor(192 + (i / 12) - 1, pixels.Color(0, 0, 0));
pixels.setPixelColor(192 + (i / 12), pixels.Color(100, 0, 0));
}
pixels.show();
delay(speed / 12);
}
for (int i = 173; i < 196; i++) pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
delay(speed);
}
void count() {
for (int i = 0; i < NUMPIXELS + 4; i++) {
pixels.setPixelColor(i, pixels.Color(50, 0, 0));
if (i % 10 == 0) pixels.setPixelColor(i, pixels.Color(0, 50, 0));
}
pixels.show();
}