Code is really simple and based on AVR demo included in AVR studio :
main.c:
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Joerg Wunsch
* ----------------------------------------------------------------------------
*
* Simple AVR demonstration. Controls a LED that can be directly
* connected from OC1/OC1A to GND. The brightness of the LED is
* controlled with the PWM. After each period of the PWM, the PWM
* value is either incremented or decremented, that's all.
*
* This code allows for the pulsating behavior above only when the
* ADC0 (sensor) < ADC5 (threshold )
* Otherwise it it activates a steady LED on PORTB.1.
*/
#include
#include
#include
#include
This ISR will handle multple (2 right now) LEDS via GPIOs
*/
ISR (TIMER1_OVF_vect)
{
if (LowLightFlag)
{
switch (direction[0])
{
case UP:
if ((pwm[0] += adder[0]) >= TIMER1_TOP)
direction[0] = DOWN;
break;
case DOWN:
if (pwm[0]-- == 0)
direction[0] = UP;
break;
}
OCR1A = pwm[0];
PORTB &= ~0x1;
}
else
{
//turn off pulsating LED
//Hold Red LED steady
direction[0] = UP;
pwm[0] = 0;
OCR1A = pwm[0];
PORTB |= 0x1;
}
}
// Peripheral intialization
void Timer1Init (void) /* Note [6] */
{
/* Timer 1 is 10-bit PWM (8-bit PWM on some ATtinys). */
TCCR1A = TIMER1_PWM_INIT;
/*
* Start timer 1.
*
* NB: TCCR1A and TCCR1B could actually be the same register, so
* take care to not clobber it.
*/
TCCR1B |= TIMER1_CLOCKSOURCE;
/*
* Run any device-dependent timer 1 setup hook if present.
*/
#if defined(TIMER1_SETUP_HOOK)
TIMER1_SETUP_HOOK();
#endif
/* Set PWM value to 0. */
OCR1A = 0;
/* Enable OC1 as output. */
DDROC = _BV (OC1);
//setup output LED for ADC
PORTB = 0xFF;
DDRB = 0xFF;
Are you suggesting to using a photo diode to measure and feedback into the system?
There were two things that I considered:
1. I did not have much time to work on this. I had a costume to complete and not a lot of spare time.
2. I did not want to be so clever that if it did not work, tuning in the theater would be difficult. Imagine having to change things quickly minutes before going on stage.
bonus third item: I did not have many of phototransitors I was using. Having to retune passives for a new one seemed painful.
That said, originally, I planned on making a fully analogue version but I couldn’t get the BOM together in time. I have not decided what to do when I need to make several more of these.
Have you considered using the LEDs themselves as a way to measure the current ambient light intensity? Could save you an analog pin and a component?
—
DG
Are you suggesting to using a photo diode to measure and feedback into the system?
There were two things that I considered:
1. I did not have much time to work on this. I had a costume to complete and not a lot of spare time.
2. I did not want to be so clever that if it did not work, tuning in the theater would be difficult. Imagine having to change things quickly minutes before going on stage.
bonus third item: I did not have many of phototransitors I was using. Having to retune passives for a new one seemed painful.
That said, originally, I planned on making a fully analogue version but I couldn’t get the BOM together in time. I have not decided what to do when I need to make several more of these.
Thanks for the comment.