How To Build a Flashlight With Perfect Modes (picture heavy)

Wow, that really sucks that temps affect it that much. I think, even with that variance, the off-time memory is much better for non-flashaholics, so I might implement this in some builds for friends.

The DSO Nano was like $80. I'm an electrical noob, so I'm sure it's not all that accurate but it at least gives me an insight into what's going on. I originally bought it for for measuring the ignition system on my motorcycle as I was building an arduino tachometer.

I agree with using the solution anyway. It is not so often my flashlights get very cold .

I wonder if the Polish developers are aware of the temperature dependance.

You cannot be an electrical noob if you build an arduino tachometer!

I have plans myself to use the arduino board to make a battery restauration charger according to the patent of SHERSTYUK (despite that I don't believe in it!). My son uses arduino to control almost everything in his house, including his Philips Hue lamps so I think he can help his father out.

I just had another idea. It will not be terrible difficult to put a chip resistor on top of the chip capacitor. A 10MOhm resistor would approximately halve the influence from the uncontrollable parasitic resistance. Of course the capacitor should have double the old value or another threshold should be selected. Thing to investigate...

Yes, I wrote that idea in another thread; I had experimented with 10uF and 100k, looked fine.

Heureka! That corresponds fine with the values I use (10MOhm and 0.1uF, soon to become 0.22uF)

Can you explain? Like I said, electrical noob ;)

I will try to explain (with the danger of making a fool of myself - I have been a pensionist for some years now, and this is from memory).

When you have a capacitor being discharged by a resistor the voltage have a decay time to a level of 1/e = 1/2.718 or about 37%. This time is 1/RC or in my case 1/(10M*0.1u) for 10MOhm and 0.1uF. That equals 1sec and corresponds nicely to a tap-detection time of 0.5 sec at half the start voltage (our approx. detection level).

When I use a capacitor (0.1uF) alone on the atTiny pin I see approx. this timing for the decay (from 0.7V down) and as a consequence I conclude that the effective resistance into the pin (the bleeding resistance) after start up must be approx. 10MOhm. Correct me if I'm wrong.

Ahh, I just reread what you originally said and now understand it. So you're basically halving the time that it takes to discharge (if you match the resistor with what the common resistance is through the pin of the MCU) in order to get a more reliable discharge timing.

Exactly! and then I must double the capacitance to keep the timing as before...

Hi all,

I've decided I need to learn how to program. I've created the following code but am not quite in a position to actually test it just yet. Regardless, I was wondering if anyone could have a brief look at it and let me know if there is anything wrong that sticks out like dog's balls?

#define F_CPU 4800000

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

ISR(ADC_vect)
{
if (((1.1 * 255) / ADCH) < 3.2)
{
OCR0A=255;
}
else
{
OCR0A=0;
}
}

int main (void)
{

DDRB |= (1 << DDB1) | (1 << DDB0);
PORTB |= (1 << PORTB3);
TCCR0A=0b00100001;
TCCR0B=0b00000001;

ADMUX |= (1 << REFS0);
ADMUX |= (1 << ADLAR);
ADMUX |= (0 << MUX1) | (1 << MUX0);

ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
ADCSRA |= (1 << ADATE);
ADCSRA |= (1 << ADEN);
ADCSRA |= (1 << ADIE);
ADCSRA |= (1 << ADSC);

sei();

while(1)
{
//main program code
}

}

The idea behind the above code is to read a voltage that is input on pin7 (PB2) and then based on that voltage turn a status LED on or off on pin5 (PB0).

It does this using interrupts. I have setup the ADC in the main program (where you see those lines starting with ADMUX and ADCSRA) and have defined what should happen (LED on/off) in the ISR function.

Pin6 (PB1) has also been defined as an output but will be utilized in the while loop.

Yay? Nay?

- Matt

I have a programmer now (whoohoo). It’s one of those cheap USB ASP 2.0 ones from ebay with a 10 pin flat wire. My clip is this one:

I got that one so I could wire it however was needed. My problem is I’ve no clue how to properly wire this thing to my programmer. the programmer has 10 pins and a sort of ‘keyed’ wire (like on the old IDE drives, it only fits one way). I have 8 male to female wires of the same size, so I can easily put any pin to any path; just dunno which ones to use where :slight_smile:

Maybe this will help.

This

Confuses me? What is your plan? What is the 3.2?
What if there is zero voltage……

If you want to compare with a voltage instead of a simple number to increase comfort, I would try something like this:
(1.1/255)*ADCresult=voltage on ADC pin.
This should give you the voltage on the pin if you use 8bit ADCconverter with internal reference voltage of 1.1V.

I have not read the rest because there are no comments and I am to lazy to search the datasheet.

That code may bring in the floating point library which will add around 6 Kb to your program… you don’t want to use any floating point numbers in your code.

My bad. Now with comments although its next to impossible to format it properly on here.

#define F_CPU 4800000

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

ISR(ADC_vect)
{
if (((1.1/255)*ADCH) < 3.2) //if ADC result is less than 3.2V
{
OCR0A=255; //turn indicator LED on
}
else //otherwise
{
OCR0A=0; //turn indicator LED off
}
}

int main (void)
{

DDRB |= (1 << DDB1) | (1 << DDB0); //set PB0 and PB1 (Pins 5 and 6) as outputs
PORTB |= (1 << PORTB3); //set PB3 (pin 2) as switch input
TCCR0A=0b00100001; //set PWM frequency
TCCR0B=0b00000001; //set PWM frequency

ADMUX |= (1 << REFS0); //set ADC reference to internal voltage reference
ADMUX |= (1 << ADLAR); //left adjust ADC result to allow for simpler 8 bit reading
ADMUX |= (0 << MUX1) | (1 << MUX0); //set ADC1/PB2/Pin7 as ADC input

ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); //set ADC prescaler to 128 - 125KHz sample rate @ 16MHz THIS NEEDS TO CHANGE FOR MY SETUP
ADCSRA |= (1 << ADATE); //set ADC to Free-Running Mode - assumes ADCSRB[2:0] are 0 by default
ADCSRA |= (1 << ADEN); //enable ADC
ADCSRA |= (1 << ADIE); //enable ADC Interrupt
ADCSRA |= (1 << ADSC); //start A2D Conversions

sei(); //enable global interupt

while(1)
{
//main program code goes here
}

}

Same description as before. If the voltage read on Pin7 is below 3.2V a small indicator LED connected to Pin5 should light up. Otherwise it is off. Pin6 is regular PWM out and will be used to control the main LED in the flashlight.

I have a very lose grasp on what I am doing so any feedback and advice would be greatly appreciated.

- Matt

You can upload code on http://codeviewer.org/ and simply post the link.
TP is right, you should simply compare with a plain number between 0 and 255 in the if.
If(ADCresult>50){action;}

Well I’ve downloaded a few files, wired my clip (improperly I suspect : ), and I’m sort of wondering wth I’m doing. I’m reading up on some of the files (AVR Dude GUI, AVR DUDE, AVR Eclipse, BLF-VLD and WinAVR). I’m so far only trying to verify that I am wired wrong and figure out how to wire correctly (gonna post this in the reply to the part you linked but with more info). Thanks for getting me restarted (gonna be saying that a lot I think; if all ends up going well maybe I can put it all together and make a sort of stiky/mini thread with just the walkthrough : ) .

How's that? I've used 100 as an arbitary value as I need to actually understand what's happening a bit better. Say VIN max is 4.2V - how do I ensure 4.2V equals 255 on the ADC register? Then , if 4.2V = 255, does that mean that 3.2V would be 195? (roughly 77% of the maximum input).

Does everything else at least look OK? Am I engaging everything properly? As in will I get meaningful output from Pins 5 and 6?

I have rewired my ispavr according to the photos above your post (How To Build a Flashlight With Perfect Modes (picture heavy) post #54). I checked continuity of all my pins on my clip vs where they plug into the back of the clip and skipped pin 8 on the programmer (simply using pin nine from the programmer as though it were pin 8). My AVRISP still has the led lit up in red, this means something is wrong with my software right?

I wouldn't rule out the programmer being dodgy. I went through 3 programmers before I found one that worked. My working model came from Fasttech.