ATTiny85 candle mimic problem

After discovering a video by author Tim Keeley on Youtube, I breadboarded components and loaded his code onto an ATTiny85 running at 8Mhz with an Arduino Uno as an ISP. Worked well; a very nice effect using two warm white leds. My project goes a bit further. Besides the candle I added a LDR, two led arrays, and a PIR detector. Using the LDR as the sensor, the candle leds and the led arrays are enabled only under a ‘dark’ condition. A second condition for the led arrays is that motion has been detected.

Scenario dark: Candle flickers indefinitely giving off a low level light. When motion is detected led arrays illuminate for 30 seconds.

Scenario light: Neither candle or led arrays illuminate.

The project is somewhat successful in that when respective conditions are met, it works as expected except for the candle leds. The effect is no longer smooth. Evidently this is because I’ve added 3 new functions to the original candle code. When I comment out the calls to those functions the candle effect is again pleasing. All 3 functions are pretty simple, I am surprised that a relatively small amount of code would slow the loop iterations and cause the problem, which I suspect what is happening. Code below.

#include <Arduino.h>
#include <WProgram.h>

byte photocell_read_pin = 1;//analog input of photocell-resistor voltage divider
byte detection_pin = 3;//logic input from PIR
byte light_control_pin = 4;//to gate of MOSFET
boolean light_on = false;
boolean detected = false;
boolean dark = true;
unsigned long light_start = 0;
unsigned long start_detect_time = 0;
int photocell_read = 0;
int threshold_read = 0;

* randomly flickering LEDs

int ledPin[] ={
  0, 1,};         // pwm pins only
int ledState[2];               // last state of each led
long randNumber;

void setup() {
  pinMode(light_control_pin, OUTPUT);
  pinMode(detection_pin, INPUT);
  for (int i=0; i<=3; i++){      // set each led pin as an output
    pinMode(ledPin[i], OUTPUT);   
  randomSeed(analogRead(0));     // seed the rnd generator with noise from unused pin
  for (int i=0; i<=3; i++){      // init. each led with a random value
    ledState[i] = random(20, 201);

void loop(){ 
    for (int i=0; i<2; i++){                  // for each led   
      analogWrite(ledPin[i], ledState[i]);     // set the pwm value of that pin determined previously
      randNumber = random(-40, 41);            // generate new random number and add that to the current value
      ledState[i] += randNumber;               // that range can be tweaked to change the intensity of the flickering
      if (ledState[i] > 200) {                 // clamp the limits of the pwm values so it remains within
        ledState[i] = 200;                     // a pleasing range as well as the pwm range
      if (ledState[i] < 10) {
        ledState[i] = 10;
    delay(100);    // the delay between changes

The other functions are in other tabs as follows:

void Dark_Detect(){
  photocell_read = analogRead(photocell_read_pin);
  if (photocell_read > 500){
    dark = true;
    dark = false;


void Detect(){
  if(digitalRead(detection_pin) == HIGH && detected == false){
    detected = true;
    start_detect_time = millis();
  if (millis()>(start_detect_time + 5000)){
    detected = false;


void Light_Control(){
  if (dark && detected && !light_on){
    digitalWrite (light_control_pin, HIGH);
    light_start = millis();
    light_on = true;
  if((light_start + 30000) < millis()){
    digitalWrite (light_control_pin, LOW);
    light_on = false;

Please give your opinions as to what is happening and if possible (considering the current hardware) what a possible solution might be.

Thanks - Scotty (schematic below)

#include <WProgram.h>

What version of the IDE are you using? Hello, the stone ages are over.


  randomSeed(analogRead(0));     // seed the rnd generator with noise from unused pin

Is worthless. Don’t bother.

byte detection_pin = 3;//logic input from PIR
  pinMode(detection_pin, INPUT);
  for (int i=0; i<=3; i++){      // set each led pin as an output
    pinMode(ledPin[i], OUTPUT);   

Is pin 3 an INPUT or an OUTPUT?