PIR activated LED with Attiny85

Hello,

I’ve taken a break from my CarDuino project for a couple of days and am now working on a miniature side project with which I want to light an unlit compartment in my car’s center console using two LEDs, an Attiny85, a miniature PIR motion sensor (see — >here) and a toggle switch with which you can switch between two operating modes. One mode will be that the light will be activated by means of the PIR sensor and then stay on for 30 seconds, the other mode will be “always-on”. And then the toggle switch will also have an “always-off” mode. The light will be dimmed up and dimmed down using PWM.

Here’s my code:

#define ledPin 1
#define PIRpin 2
#define switchPin 3
#define brightnessPot 4

boolean lightOn = false;
byte brightness = 0;
int modeVoltage = 0;
int ledMaxBrightness = 0;

void setup() {

  pinMode(PIRpin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(brightnessPot, INPUT);
  analogWrite(ledPin, 0);
}

void loop() {

  modeVoltage = analogRead(switchPin);
  ledMaxBrightness = analogRead(brightnessPot);
  ledMaxBrightness = (int) (ledMaxBrightness/1023)*255; 

  if (modeVoltage > 500) { 
    
    // Mode switch is set to motion-activated lighting
    if (digitalRead(PIRpin) && !lightOn) { // Dimming up the light

      for (brightness = 0; brightness <= ledMaxBrightness; brightness++) {

        analogWrite(ledPin, brightness);
        delay(20);
      }
      lightOn = true;
      delay(30000); // Light stays on for 30 seconds
    }

    if (!digitalRead(PIRpin) && lightOn ) { 
      
      //  If there's no movement at the moment when
      //  30 seconds are up, the light is dimmed down again
      for (brightness = ledMaxBrightness; brightness >= 0; brightness--) {

        analogWrite(ledPin, brightness);
        delay(40);
      }
      lightOn = false;
    }
  }

  else if (modeVoltage < 500 && modeVoltage > 0) { 
    
    // Mode switch is set to "always-on"
    if (!lightOn) {

      for (brightness = 0; brightness <= ledMaxBrightness; brightness++)  {

        analogWrite(ledPin, brightness);
        delay(20);
      }
      lightOn = true;
    }
  }

  else if (modeVoltage == 0 && lightOn) { 
    
    // Mode switch off means light is faded out and/or stays off
    for (brightness = ledMaxBrightness; brightness >= 0; brightness--)  {

      analogWrite(ledPin, brightness);
      delay(20);
    }
    lightOn = false;
  }
}

This code compiles and uploads fine; but it’s still behaving slightly erratically at the moment.

I’ve been trying to work out the bugs, which are mainly that the LEDs keep dimming up from zero over and over, and the toggle switch as well as the PIR sensor and the brightness control pot have no effect on it… I’ve checked that they are all properly connected. I’ve measured all relevant parts with a multimeter, they all seem to work as intended.

Here’s a schematic… right now, on my breadboard, the Attiny is still powered by an Arduino Uno board, which means I haven’t yet put in the regulator, fuse, diode(s), and choke:

EDIT:

Here’s an update of my code… still doesn’t do anything of what it’s supposed to do… the LEDs just flare up and then dim down again over and over.

#define ledPin 1
#define PIRpin A1
#define switchPin A3
#define brightnessPot A2

boolean lightOn = false;
byte brightness;
float ledMaxBrightness;
byte ledMaxBrightnessSet;

void setup() {

  pinMode(PIRpin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(brightnessPot, INPUT);
  analogWrite(ledPin, 0);
}

void loop() {

  ledMaxBrightness = analogRead(brightnessPot) / 1023;
  ledMaxBrightnessSet = (int) (ledMaxBrightness * 255);

  if (analogRead(switchPin) > 500) {

    // Mode switch is set to motion-activated lighting
    if (analogRead(PIRpin) > 5 && !lightOn) { // Dimming up the light

      for (brightness = 0; brightness <= ledMaxBrightnessSet; brightness++) {

        analogWrite(ledPin, brightness);
        delay(20);
      }
      lightOn = true;
      delay(30000); // Light stays on for 30 seconds
    }

    if (analogRead(PIRpin) < 5 && lightOn ) {

      //  If there's no movement at the moment when
      //  30 seconds are up, the light is dimmed down again
      for (brightness = ledMaxBrightnessSet; brightness >= 0; brightness--) {

        analogWrite(ledPin, brightness);
        delay(40);
      }
      lightOn = false;
    }
  }

  if (analogRead(switchPin) < 500 && analogRead(switchPin) > 5) {

    // Mode switch is set to "always-on"
    if (!lightOn) {

      for (brightness = 0; brightness <= ledMaxBrightnessSet; brightness++)  {

        analogWrite(ledPin, brightness);
        delay(20);
      }
      lightOn = true;
    }
  }

  if (analogRead(switchPin) < 5  && lightOn) {

    // Mode switch off means light is faded out and/or stays off
    for (brightness = ledMaxBrightnessSet; brightness >= 0; brightness--)  {

      analogWrite(ledPin, brightness);
      delay(20);
    }
    lightOn = false;
  }
}

It turns out that the way the PIR sensor is connected, it only delivers 1 volt to the Attiny’s input pin when it’s tripped. So I’ve tried to catch that using analogRead on that pin, but without success. :frowning: