Candle-lamp made with Teensy LC

Status
Not open for further replies.
If someone is interested:
Code:
#include <IRremote.h>
#include <IRremoteInt.h>

const unsigned int DACPIN = A12;
const double MIN = 380.0;
const double MAX = 1900.0;
const double MINSTEP = 0.00002;
const unsigned int COEFFMAXMAX = 1000;
const unsigned int COEFFMAXMIN = 200;
const unsigned int DELTACOEFF_FASTMODE = 200;
const unsigned int MAXRUNTIME = 1000*60*60*6;
const unsigned int RNDTIME1MIN = 500;
const unsigned int RNDTIME1MAX = 10000;
const unsigned int RNDTIME2MIN = 1000;
const unsigned int RNDTIME2MAX = 2000;

void setup()
{
  analogWriteResolution(12);
  randomSeed(analogRead(A1));
  delay(1);
}

void loop()
{
unsigned int coeffMin, coeffMax, on = 1, RECV_PIN = 14, waitUntil1 = 0, waitUntil2 = 0,
             onTime = 0, currMillis, prevMillis = 0;
double x1 = 0.0, x2 = 0.0, x3 = 0.0, step1, step2, step3;
IRrecv irrecv(RECV_PIN);
decode_results results;

  irrecv.enableIRIn();
  while(1)
  {
    waitUntil2 = micros();
    if (waitUntil2 > 0xffffffff-5000)
    {
      delayMicroseconds(5000);
      waitUntil2 = micros();
    }
    waitUntil2 += 1000;
    currMillis = millis();
    if (currMillis > 0xffffffff-RNDTIME1MAX)
    {
      delay(RNDTIME1MAX);
      currMillis = millis();
    }
    analogWrite(DACPIN, (MIN+MAX)/2.0 + (sin(x1) + sin(x2) + sin(x3)) / 3.0 * (MAX-MIN)/2.0);
    if (currMillis > waitUntil1)
    {
      if (random(10))
      {
        coeffMin = 0;
        coeffMax = random(COEFFMAXMIN, COEFFMAXMAX);
        waitUntil1 = currMillis + random(RNDTIME1MIN, RNDTIME1MAX);
      }
      else
      {
        coeffMin = COEFFMAXMAX-DELTACOEFF_FASTMODE;
        coeffMax = random(coeffMin, COEFFMAXMAX);
        waitUntil1 = currMillis + random(RNDTIME2MIN, RNDTIME2MAX);
      }
      step1 = random(coeffMin, coeffMax) * MINSTEP;
      step2 = random(coeffMin, coeffMax) * MINSTEP;
      step2 = random(coeffMin, coeffMax) * MINSTEP;
    }
    x1 += step1;
    x2 += step2;
    x3 += step3;
    if (x1 > TWO_PI) x1 -= TWO_PI;
    if (x2 > TWO_PI) x2 -= TWO_PI;
    if (x3 > TWO_PI) x3 -= TWO_PI;
    if (currMillis != prevMillis)
    {
      if (++onTime > MAXRUNTIME)
      {
        analogWrite(DACPIN, 0);
        on = 0;
        onTime = 0;
        waitUntil2 = 0;
      }
      prevMillis = currMillis;
    }
    while (micros() < waitUntil2 || !on)
    {
      if (irrecv.decode(&results))
      {
        if (results.value == 0x807F38C7)
        {
          if (on)
          {
            analogWrite(DACPIN, 0);
            on = 0;
            onTime = 0;
            waitUntil2 = 0;
          }
          else
          {
            irrecv.resume();
            waitUntil1 = millis() + 200;
            int buttonPressCnt = 0;
            while (millis() < waitUntil1)
            {
              if (irrecv.decode(&results))
              {
                if (results.value == 0xffffffff)
                {
                  buttonPressCnt++;
                  waitUntil1 = millis() + 200;
                }
                irrecv.resume();
              }
            }
            if (buttonPressCnt * 200 < 2000-200) on = 1;
          }
        }
        irrecv.resume();
      }
    }
  }
}
 
Status
Not open for further replies.
Back
Top