Could anyone help with making a function in which the snake gets longer? The FastLED.clear(); function clears it, and I've tried making a bool function to tell it not to clear when a food is ate, but I feel I don't have the skill level to or don't know about a function to make it add one led to the snake.
#include "FastLED.h"
#define NUM_LEDS 256
#define DATA_PIN 8
CRGB leds[NUM_LEDS];
int led = 0;
int foodLed = 0;
int dir = 0;
int trail = 1;
int pause = 200;
int szechuan(int);
int columns(int);
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
randomSeed(analogRead(0));
foodLed = food();
}
void loop() {
leds[trail] = CRGB::Blue;
if (led == foodLed) {
foodLed = food();
int morty = szechuan(led);
FastLED.show();
leds[morty] = CRGB::Green;
trail++;
}
int valx = analogRead(1);
int valy = analogRead(2);
leds[szechuan(foodLed)] = CRGB::Red;
if (valx < 354) {
dir = 1;
}
if (valx > 700) {
dir = -1;
}
if (valy > 700) {
dir = 16;
}
if (valy < 354) {
dir = -16;
}
if (abs(dir) == 1) {
if ((led + dir) / 16 == led / 16) {
led = led + dir;
}
}
if (abs(dir) == 16) {
if (led + dir > 0 && led + dir < 256) {
led = led + dir;
}
}
delay(pause);
led = led % 256;
int morty = szechuan(led);
leds[morty] = CRGB::Green;
int rick = leds[morty];
FastLED.setBrightness(15);
FastLED.show();
FastLED.clear();
}
int szechuan(int n) {
int output;
if ((n / 16) % 2 == 1) {
output = (2 * (n / 16) + 1) * 16 - 1 - n;
}
else {
output = n;
}
return output;
}
int columns(int n) {
int output = (16 * led) % 255;
return output;
}
int food() {
int x = random(256);
return x;
}