Neto Carrer
Published © GPL3+

Physical computing with ATtiny

Using Arduino to make home decoration more fun.

IntermediateFull instructions provided2,208
Physical computing with ATtiny

Things used in this project

Hardware components

ATtiny45
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
LED (generic)
LED (generic)
×6
Any old 3V-like battery
×1
Hook-Up Wire
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Circuit diagram

Code

mask-06.02.2015.ino

C/C++
#include <avr/sleep.h>
#include <avr/interrupt.h>

#define pirPin    3
#define ledPinL   1
#define ledPinR   0

void turnLed(byte mode, boolean left, boolean right)
{
    if (left)
      digitalWrite(ledPinL, mode);        
    if (right)
      digitalWrite(ledPinR, mode);        
}

void turnLedOn(boolean left, boolean right)
{
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=15)
  {
    // sets the value (range from 0 to 255):
    if (fadeValue > 255)
      fadeValue=255;
    
    if (left)
      analogWrite(ledPinL, fadeValue);        
    
    if (right)
      analogWrite(ledPinR, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
  }
}

void turnLedOff(boolean left, boolean right)
{
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=15)
  {
    if (fadeValue<0)
      fadeValue=0;
    // sets the value (range from 0 to 255):
    if (left)
      analogWrite(ledPinL, fadeValue);        
    
    if (right)
      analogWrite(ledPinR, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect    
    delay(10);                            
  }
}

void setup() {

    pinMode(pirPin, INPUT);
    digitalWrite(pirPin, HIGH);
    pinMode(ledPinR, OUTPUT);
    pinMode(ledPinL, OUTPUT);

    // blink quickly to show it's started
    for (int k = 0; k < 20; k = k + 1) {
      if (k % 2 == 0)
          turnLed(HIGH, true, true);
      else
          turnLed(LOW, true, true);            
      delay(100);
    } // for

} // setup

void sleep() {

    GIMSK |= _BV(PCIE);                     // Enable Pin Change Interrupts
    PCMSK |= _BV(PCINT3);                   // Use PB3 as interrupt pin
    ADCSRA &= ~_BV(ADEN);                   // ADC off
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);    // replaces above statement

    sleep_enable();                         // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
    sei();                                  // Enable interrupts
    sleep_cpu();                            // sleep

    //           z          z  z  z
    //    z   z    z     z               <-- sleeping here
    //  z   z         z                  
  
    // an interruption has happened on pin3    
    cli();                                  // Disable interrupts
    PCMSK &= ~_BV(PCINT3);                  // Turn off PB3 as interrupt pin
    sleep_disable();                        // Clear SE bit
    ADCSRA |= _BV(ADEN);                    // ADC on

    sei();                                  // Enable interrupts
    } // sleep

ISR(PCINT0_vect) {
    // This is called when the interrupt occurs, but I don't need to do anything in it
    }

void loop() {
    sleep();

    // blink
    turnLedOn(true, true);
    delay(1000);
    turnLedOff(true, true);
    turnLedOn(true, true);
    delay(2000);
    turnLedOff(true, true);
    turnLedOn(true, true);
    delay(1000);
    turnLedOff(true, true);
}

Credits

Neto Carrer

Neto Carrer

3 projects • 7 followers

Comments