arduino:flackerndes_windlicht:programmversion_0.3
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Nächste Überarbeitung | Vorhergehende ÜberarbeitungNächste ÜberarbeitungBeide Seiten der Revision | ||
arduino:flackerndes_windlicht:programmversion_0.3 [08.01.2018 18:14] – angelegt Frickelpiet | arduino:flackerndes_windlicht:programmversion_0.3 [30.01.2018 18:43] – Frickelpiet | ||
---|---|---|---|
Zeile 4: | Zeile 4: | ||
Ab dieser Programmversion kann sich die Flamme über die gesamte Kreisfläche bewegen, die der NeoPixel-Ring umschließt. Die Variable " | Ab dieser Programmversion kann sich die Flamme über die gesamte Kreisfläche bewegen, die der NeoPixel-Ring umschließt. Die Variable " | ||
+ | < | ||
+ | // Flickering and fluttering candle | ||
+ | // This code emulates a candle using a ring of twelve NeoPixels | ||
+ | // Version 0.4.3 | ||
+ | // by Frickelpiet | ||
+ | // Libraries | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // Define output pin | ||
+ | #define PIN 2 | ||
+ | |||
+ | // Define the NeoPixel ring | ||
+ | int numPixels = 12; // Number of NeoPixels | ||
+ | Adafruit_NeoPixel ring = Adafruit_NeoPixel(numPixels, | ||
+ | |||
+ | // Define the light seensor (TSL2561) | ||
+ | Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, | ||
+ | |||
+ | // Variables | ||
+ | int mode = 0; // Mode of the flame' | ||
+ | |||
+ | double deviation = 0; // Value for the flame' | ||
+ | double angle = 0; // Angle of the flame' | ||
+ | double flickering; | ||
+ | int brightness = 255; // Value for the flame' | ||
+ | |||
+ | double pi = 3.14159265358979; | ||
+ | double angleArray[12]; | ||
+ | double deviationArray[12]; | ||
+ | byte flameArray[12]; | ||
+ | |||
+ | double flickerDepth; | ||
+ | double deviationAlteration; | ||
+ | double deviationTreshold; | ||
+ | double angleAlteration; | ||
+ | |||
+ | float sensorValue; | ||
+ | |||
+ | unsigned long previousMillisProgram = 0; // Program | ||
+ | unsigned long previousMillisCandle = 0; // Emulation | ||
+ | unsigned long previousMillisBrightness = 0; // Brightness | ||
+ | |||
+ | unsigned long intervalProgram = 50; // Program | ||
+ | unsigned long intervalCandle = 20; // Emulation | ||
+ | unsigned long intervalBrightness = 200; // Brightness | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(115200); | ||
+ | |||
+ | if(!tsl.begin()) | ||
+ | { | ||
+ | /* There was a problem detecting the TSL2561 ... check your connections */ | ||
+ | Serial.print(" | ||
+ | while(1); | ||
+ | } | ||
+ | sensor_t sensor; | ||
+ | tsl.getSensor(& | ||
+ | Serial.println(" | ||
+ | Serial.print | ||
+ | Serial.print | ||
+ | Serial.print | ||
+ | Serial.print | ||
+ | Serial.print | ||
+ | Serial.print | ||
+ | Serial.println(" | ||
+ | Serial.println("" | ||
+ | delay(500); | ||
+ | | ||
+ | /* You can also manually set the gain or enable auto-gain support */ | ||
+ | tsl.setGain(TSL2561_GAIN_1X); | ||
+ | // tsl.setGain(TSL2561_GAIN_16X); | ||
+ | // tsl.enableAutoRange(true); | ||
+ | | ||
+ | /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */ | ||
+ | tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); | ||
+ | // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); | ||
+ | // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); | ||
+ | |||
+ | /* Update these values depending on what you've set above! */ | ||
+ | Serial.println(" | ||
+ | Serial.print | ||
+ | Serial.print | ||
+ | Serial.println(" | ||
+ | |||
+ | randomSeed(analogRead(6)); | ||
+ | |||
+ | // Calculate the angle for each NeoPixel | ||
+ | angleArray[0] = 0; | ||
+ | for (int i = 0; i < numPixels - 1; i++) { | ||
+ | angleArray[i + 1] = angleArray[i] + pi / (numPixels / 2); | ||
+ | } | ||
+ | for(int i = 0; i < numPixels; i++) { | ||
+ | Serial.println(angleArray[i]); | ||
+ | } | ||
+ | | ||
+ | ring.begin(); | ||
+ | ring.show(); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | void loop() { | ||
+ | // Get current time in microseconds | ||
+ | unsigned long currentMillis = millis(); | ||
+ | |||
+ | // Program modes | ||
+ | if ((unsigned long)(currentMillis - previousMillisProgram) >= intervalProgram) { | ||
+ | |||
+ | mode = random(0, 4); | ||
+ | //mode = 4; | ||
+ | | ||
+ | // Calm burning | ||
+ | if (mode == 0) { | ||
+ | intervalCandle = random(80, 100); | ||
+ | flickerDepth = 940; | ||
+ | deviationAlteration = 50; | ||
+ | deviationTreshold = 0.5; | ||
+ | angleAlteration = 30; | ||
+ | intervalProgram = random(1000, | ||
+ | Serial.print(" | ||
+ | } | ||
+ | | ||
+ | // Gentle flickering | ||
+ | if (mode == 1) { | ||
+ | intervalCandle = random(60, 80); | ||
+ | flickerDepth = 900; | ||
+ | deviationAlteration = 100; | ||
+ | deviationTreshold = 0.6; | ||
+ | angleAlteration = 60; | ||
+ | intervalProgram = random(1000, | ||
+ | Serial.print(" | ||
+ | } | ||
+ | |||
+ | // Moderate flickering | ||
+ | if (mode == 2) { | ||
+ | intervalCandle = random(50, 60); | ||
+ | flickerDepth = 850; | ||
+ | deviationAlteration = 150; | ||
+ | deviationTreshold = 0.7; | ||
+ | angleAlteration = 90; | ||
+ | intervalProgram = random(1000, | ||
+ | Serial.print(" | ||
+ | } | ||
+ | |||
+ | // Noticeabal flickering | ||
+ | if (mode == 3) { | ||
+ | intervalCandle = random(40, 50); | ||
+ | flickerDepth = 800; | ||
+ | deviationAlteration = 200; | ||
+ | deviationTreshold = 0.8; | ||
+ | angleAlteration = 120; | ||
+ | intervalProgram = random(1000, | ||
+ | Serial.print(" | ||
+ | } | ||
+ | |||
+ | // Strong flickering | ||
+ | if (mode == 4) { | ||
+ | intervalCandle = random(30, 50); | ||
+ | flickerDepth = 750; | ||
+ | deviationAlteration = 250; | ||
+ | deviationTreshold = 1.0; | ||
+ | angleAlteration = 240; | ||
+ | intervalProgram = random(1000, | ||
+ | Serial.print(" | ||
+ | } | ||
+ | |||
+ | previousMillisProgram = currentMillis; | ||
+ | } | ||
+ | |||
+ | | ||
+ | // Emulation of the flame' | ||
+ | if ((unsigned long)(currentMillis - previousMillisCandle) >= intervalCandle) { | ||
+ | |||
+ | |||
+ | // Calculation of the flame' | ||
+ | deviation = deviation + double(random(-deviationAlteration, | ||
+ | if (deviation < 0) { | ||
+ | deviation = 0; | ||
+ | } | ||
+ | if (deviation > deviationTreshold) { | ||
+ | deviation = deviationTreshold; | ||
+ | } | ||
+ | | ||
+ | |||
+ | // Calculation of the angle of the flame' | ||
+ | angle = angle + double(random(-angleAlteration, | ||
+ | if (angle < 0) { | ||
+ | angle = angle + 2 * pi; | ||
+ | } | ||
+ | if (angle > 2 * pi) { | ||
+ | angle = angle - 2 * pi; | ||
+ | } | ||
+ | |||
+ | |||
+ | // Calculation of the flame' | ||
+ | flickering = double(random(flickerDepth, | ||
+ | |||
+ | Serial.print(" | ||
+ | Serial.print(" | ||
+ | Serial.print(" | ||
+ | Serial.print(" | ||
+ | Serial.print(" | ||
+ | Serial.print(" | ||
+ | Serial.print(" | ||
+ | | ||
+ | | ||
+ | // Calculation of the d values for the twelve NeoPixel | ||
+ | for (int i = 0; i < numPixels; i++) { | ||
+ | deviationArray[i] = (((cos(angleArray[i] - angle) / (2 / deviation)) + 0.5)); | ||
+ | } | ||
+ | |||
+ | // Calculation of the flickering and brithness fpr the twelve NeoPixel | ||
+ | for (int i = 0; i < numPixels; i++) { | ||
+ | flameArray[i] = byte(deviationArray[i] * flickering * brightness); | ||
+ | } | ||
+ | |||
+ | // Calculation of the colors for the twelve NeoPixel | ||
+ | for (int i = 0; i < numPixels; i++) { | ||
+ | ring.setPixelColor(i, | ||
+ | // | ||
+ | } | ||
+ | | ||
+ | ring.show(); | ||
+ | |||
+ | |||
+ | |||
+ | for(int i = 0; i < numPixels; i++) { | ||
+ | Serial.print(deviationArray[i]); | ||
+ | } | ||
+ | for(int i = 0; i < numPixels; i++) { | ||
+ | Serial.print(byte(flameArray[i] * 255)); Serial.print(" | ||
+ | } | ||
+ | | ||
+ | Serial.println(); | ||
+ | |||
+ | | ||
+ | } | ||
+ | |||
+ | |||
+ | // Brightness control | ||
+ | if ((unsigned long)(currentMillis - previousMillisBrightness) >= intervalBrightness) { | ||
+ | |||
+ | /* Get a new sensor event */ | ||
+ | sensors_event_t event; | ||
+ | tsl.getEvent(& | ||
+ | sensorValue = event.light; | ||
+ | |||
+ | brightness = int(sqrt(sensorValue * 100) * 2); | ||
+ | |||
+ | if (brightness < 192) { | ||
+ | brightness = 128; | ||
+ | } | ||
+ | if (brightness > 255) { | ||
+ | brightness = 255; | ||
+ | } | ||
+ | |||
+ | previousMillisBrightness = currentMillis; | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | |||
+ | </ | ||
Tags: #Arduino #NeoPixel #Windlicht | Tags: #Arduino #NeoPixel #Windlicht |
arduino/flackerndes_windlicht/programmversion_0.3.txt · Zuletzt geändert: 18.05.2023 12:34 von 127.0.0.1