random effects and improved flashing

This commit is contained in:
interfisch 2018-07-15 23:04:20 +02:00
parent 4bee3e6286
commit 761a077162
3 changed files with 67 additions and 32 deletions

View File

@ -24,6 +24,10 @@ long lastCheckspawn=0;
#define CHECKSPAWNDELAY 4000 //delay in ms to check random spawn #define CHECKSPAWNDELAY 4000 //delay in ms to check random spawn
#define SPAWNCHANCE 20 //1 out of x times wagon will spawn #define SPAWNCHANCE 20 //1 out of x times wagon will spawn
#define SPAWNCHANCEDOUBLE 5 //change of spawning a two trains simultaneously #define SPAWNCHANCEDOUBLE 5 //change of spawning a two trains simultaneously
long lastCheckspawnEffect=0;
#define CHECKSPAWNDELAY_EFFECT 2000 //delay in ms to check random effect
#define SPAWNCHANCE_EFFECT_SCANNER 100 //1 out of x times spawn effect
#define SPAWNCHANCE_EFFECT_FLASH 80 //1 out of x times spawn effect
#define BRIGHTNESS_RUN 200 #define BRIGHTNESS_RUN 200
#define BRIGHTNESS_DEBUG 150 #define BRIGHTNESS_DEBUG 150
@ -389,7 +393,7 @@ void checkSerial(){
effect=new FX_Scanner(NUMPIXELS,&strip,height,255,-200,strip.Color(100,0,0)); effect=new FX_Scanner(NUMPIXELS,&strip,height,255,-200,strip.Color(100,0,0));
}else if (serialstring.equals("fx_flash")){ }else if (serialstring.equals("fx_flash")){
Serial.println("Effect Flash"); Serial.println("Effect Flash");
effect=new FX_Flash(NUMPIXELS,&strip,strip.Color(200,200,200)); effect=new FX_Flash(NUMPIXELS,&strip,height,strip.Color(200,200,200));
} }
} }
@ -494,11 +498,18 @@ void loop_achterbahn(){
if (random(0,SPAWNCHANCEDOUBLE)==0){ if (random(0,SPAWNCHANCEDOUBLE)==0){
spawnWagon(); spawnWagon();
} }
}//else{ }
//Serial.println("no spawn"); }
//}
//Check Effect Spawning
if (effect==NULL && lastCheckspawnEffect+CHECKSPAWNDELAY_EFFECT<loopmillis) {
lastCheckspawnEffect=loopmillis;
if (random(0,SPAWNCHANCE_EFFECT_SCANNER)==0){
effect=new FX_Scanner(NUMPIXELS,&strip,height,255,-200,strip.Color(100,0,0));
}else if (random(0,SPAWNCHANCE_EFFECT_FLASH)==0){
effect=new FX_Flash(NUMPIXELS,&strip,height,strip.Color(200,200,200));
}
} }

View File

@ -1,14 +1,15 @@
#include "fx_flash.h" #include "fx_flash.h"
#include "effect.h" #include "effect.h"
#define FLASHTIME 10 //in ms FX_Flash::FX_Flash(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height,uint32_t flashcolor)
FX_Flash::FX_Flash(int numpixels,Adafruit_NeoPixel *strip,uint32_t flashcolor)
{ {
_numpixels=numpixels; _numpixels=numpixels;
_strip=strip; _strip=strip;
_flashcolor=flashcolor; _flashcolor=flashcolor;
_starttime=millis(); _starttime=millis();
_brightness=0;
_flashtime=random(200,3000);
_height=height;
} }
FX_Flash::FX_Flash() FX_Flash::FX_Flash()
@ -17,16 +18,35 @@ FX_Flash::FX_Flash()
void FX_Flash::updateRoutine(float updatedelayms) void FX_Flash::updateRoutine(float updatedelayms)
{ {
float negativelikelihood=pow(( (millis()-_starttime)*1.0/_flashtime ),2); // 0=most likely, 1=unlikely
_brightness*=0.2;
if (random(0,negativelikelihood*20)==0){
_brightness=random(100,255);
}
//_brightness=(1.0-pow((255-_brightness)/255.0,4))*255.0;
} }
void FX_Flash::updateGraphics() void FX_Flash::updateGraphics()
{ {
#define STARTFLASHHEIGHT 100
#define BRIGHTFLASHHEIGHT 150
for(int i=0;i<_numpixels;i++){ for(int i=0;i<_numpixels;i++){
if (_height[i]>=STARTFLASHHEIGHT){
uint8_t heightbrightness=map(_height[i],STARTFLASHHEIGHT,BRIGHTFLASHHEIGHT,0,255);
uint8_t _r = _flashcolor >> 16; uint8_t _r = _flashcolor >> 16;
uint8_t _g = _flashcolor >> 8; uint8_t _g = _flashcolor >> 8;
uint8_t _b = _flashcolor; uint8_t _b = _flashcolor;
_r*=_brightness/255.0;
_g*=_brightness/255.0;
_b*=_brightness/255.0;
_r*=heightbrightness/255.0;
_g*=heightbrightness/255.0;
_b*=heightbrightness/255.0;
uint32_t _pxcolor=_strip->getPixelColor(i); //get current color of that pixel uint32_t _pxcolor=_strip->getPixelColor(i); //get current color of that pixel
uint8_t _pxr = _pxcolor >> 16; uint8_t _pxr = _pxcolor >> 16;
@ -45,13 +65,14 @@ void FX_Flash::updateGraphics()
_tmpb=255; _tmpb=255;
} }
_strip->setPixelColor(i,_tmpr,_tmpg,_tmpb); //draw pixel _strip->setPixelColor(i,_tmpr,_tmpg,_tmpb); //draw pixel
}
} }
} }
bool FX_Flash::active() bool FX_Flash::active()
{ {
if (millis()-_starttime>FLASHTIME){ if (millis()-_starttime>_flashtime){
return false; return false;
} }
return true; return true;

View File

@ -7,7 +7,7 @@
class FX_Flash : public Effect class FX_Flash : public Effect
{ {
public: public:
FX_Flash(int numpixels,Adafruit_NeoPixel *strip,uint32_t flashcolor); FX_Flash(int numpixels,Adafruit_NeoPixel *strip,uint8_t *height,uint32_t flashcolor);
FX_Flash(); FX_Flash();
void updateRoutine(float updatedelayms); void updateRoutine(float updatedelayms);
void updateGraphics(); void updateGraphics();
@ -18,6 +18,9 @@ class FX_Flash : public Effect
Adafruit_NeoPixel *_strip; Adafruit_NeoPixel *_strip;
uint32_t _flashcolor; uint32_t _flashcolor;
long _starttime; long _starttime;
uint8_t _brightness;
long _flashtime;
uint8_t *_height;
}; };
#endif #endif