/* 8 rows of 8 LEDs each
* Note
* On the real Arduino Uno not all pins are PWM-capable
*
* Simulator
*/
#include <Adafruit_NeoPixel.h>
uint32_t gibFarbe(byte pos);
uint32_t neueFarbe();
void blitz();
void resetLEDs();
#define NUMPIXELS 8
#define ANZAHLSTICKS 8
Adafruit_NeoPixel* streifen[ANZAHLSTICKS];
// pin[] = {15, 13, 12, 14, 0, 4, 5, 1};
int pin[] = {13, 12, 11, 10, 9, 6, 5, 3};
//Spielfeld
#define X_MAX 8
#define Y_MAX 8
//Ameise
int posX = 3; //Startstellung
int posY = 3;
int lastPosX = posX;
int lastPosY = posY;
int richtungX = 1;
int richtungY = 1;
uint32_t farbe;
//Pause ohne delay()
unsigned long previousMillis = 0;
const long interval = 333;
void setup()
{
for (int z = 0; z < ANZAHLSTICKS; z++)
{
streifen[z] = new Adafruit_NeoPixel(NUMPIXELS, pin[z], NEO_GRB + NEO_KHZ800); //neues Objekt wird erschaffen
streifen[z]->begin();
streifen[z]->setBrightness(255);
/* ??? */ streifen[z]->show();
}
farbe = neueFarbe();
streifen[posX]->setPixelColor(posY, farbe);
streifen[posX]->show();
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
//----------------
// Ameise steuern
//----------------
richtungX = rand() % 3 - 1; //-1...+1
richtungY = rand() % 3 - 1;
posX += richtungX;
posY += richtungY;
if (posX >= X_MAX) //über rechten Rand hinaus
{
posX = lastPosX;
if (richtungY == 0)
{
richtungX = -1; //darf nicht stehen bleiben
richtungY = rand() % 3 - 1; //-1...+1
}
else //richtungY != 0
{
richtungX = rand() % 2 - 1; //-1...0
}
farbe = neueFarbe();
blitz();
}
if (posX < 0) //über linker Rand hinaus
{
posX = lastPosX;
if (richtungY == 0)
{
richtungX = +1; //darf nicht stehen bleiben
richtungY = rand() % 3 - 1; //-1...+1
}
else //richtungY != 0
{
richtungX = rand() % 2; //0...+1
}
farbe = neueFarbe();
blitz();
}
if (posY >= Y_MAX) //über Rand hinaus
{
posY = lastPosY;
if (richtungX == 0)
{
richtungY = -1; //darf nicht stehen bleiben
richtungX = rand() % 3 - 1; //-1...+1
}
else //richtungX != 0
{
richtungY = rand() % 2 - 1; //-1...0
}
farbe = neueFarbe();
blitz();
}
if (posY < 0) //über Rand hinaus
{
posY = lastPosY;
if (richtungX == 0)
{
richtungY = +1; //darf nicht stehen bleiben
richtungX = rand() % 3 - 1; //-1...+1
}
else //richtungX != 0
{
richtungY = rand() % 2; //0...+1
}
farbe = neueFarbe();
blitz();
}
if (lastPosX != posX || lastPosY != posY)
{
streifen[lastPosX]->setPixelColor(lastPosY, 0, 0, 0);
streifen[lastPosX]->show();
}
streifen[posX]->setPixelColor(posY, farbe);
streifen[posX]->show();
lastPosX = posX;
lastPosY = posY;
}
}
//--------------------------------------------------------------------------.-------------.
// | neueFarbe() |
//--------------------------------------------------------------------------'-------------'
uint32_t neueFarbe()
{
byte b = rand() % 256;
return gibFarbe(b);
}
//---------------------------------------------------------------------------.------------.
// | gibFarbe() |
//---------------------------------------------------------------------------'------------'
uint32_t gibFarbe(byte pos)
{
u32 wheelPos = pos % 0xff;
if (wheelPos < 85)
{
return ((255 - wheelPos * 3) << 16) | ((wheelPos * 3) << 8);
}
if (wheelPos < 170)
{
wheelPos -= 85;
return (((255 - wheelPos * 3) << 8) | (wheelPos * 3));
}
wheelPos -= 170;
return ((wheelPos * 3) << 16 | (255 - wheelPos * 3));
}
//------------------------------------------------------------------------------.---------.
// | blitz() |
//------------------------------------------------------------------------------'---------'
void blitz()
{
for (int x = 0; x < ANZAHLSTICKS; x++) //Streifen
{
for (int y = 0; y < NUMPIXELS; y++) //Pixel
{
streifen[x]->setPixelColor(y, farbe);
}
}
for (int x = 0; x < ANZAHLSTICKS; x++)
{
streifen[x]->show();
}
delay(100);
resetLEDs();
}
//--------------------------------------------------------------------------.-------------.
// | resetLEDs() |
//--------------------------------------------------------------------------'-------------'
void resetLEDs()
{
for (int x = 0; x < ANZAHLSTICKS; x++)
{
streifen[x]->clear();
}
for (int x = 0; x < ANZAHLSTICKS; x++)
{
streifen[x]->show();
}
}