Wow.
Thanks for all that info G.
I think I understand most of the basic concepts behind getting it
working, will just need to query some details as we are going along.
Sounds like there is a lot we could learn from you :-)
I look forward to next weeks meeting and discovering your true
identity lol.
Cheers,
Mike
On Jul 8, 1:19 pm, G Bulmer <
gbul...@gmail.com> wrote:
> On Jul 8, 10:26 am, Michael Nicholls <
nicholls...@googlemail.com>
> wrote:> Thanks for look at that.
>
> > Am I right in thinking that when you are sending an IR pulse you have
> > to oscillate it at about 38KHz and this is what you suggest would be
> > best to run off the hardware timer?
>
> Yes. Use the hardware to generate a continuous 38KHz square wave. An
> IR LED on the right pwm pin will then flash with the right modulated
> carrier signal. Then just switch the signal on and off using the
> timings in the article.
>
> Set up the timer registers in setup(), and use analogWrite to the
> appropriate pin.
>
> Here's an example . Please don't assume this is all correct, it is
> just fragments from a program I wrote last year. I set the things to
> flash very slowly so I could see it working with an ordinary LED. It
> needs to be adjusted a bit to generate 38KHz.
>
> #include <avr/io.h>
>
> void setup()
> {
> // Switch off Timer1 PWM
> TCCR1A = 0; // clear control register A - 15.11.1 pg 131 - 133
> TCCR1B = 0; // clear control register B - 15.11.2 pg 133-134
>
> // Set Waveform generation mode - table 15-4 page 133
> // set mode 9: phase and frequency correct pwm with OCR1A sets
> TOP, i.e. controls frequency
> TCCR1B = _BV(WGM13); // WGM1_3=1 WGM1_2=0
> TCCR1A = _BV(WGM10); // WGM1_1=0 WGM1_0=1
>
> // set Timer1 prescaler to 1024, i.e. 16MHz/1024 = 16KHz-ish
> TCCR1B |= _BV(CS12) | _BV(CS10); // CS12=1 CS11=0 CS10=1, Table
> 15-5, pg 134
>
> TCNT1 = 0; // Zero Timer1 counter
> OCR1A = 0x3FFF; // slow frequency -slow enough to be seen, 16KHz/
> 32k = about 1/2 Hz
>
> DDRB |= _BV(PB1); // sets data direction register for pwm output
> pin 9
>
> TCCR1A |= _BV(COM1A0); // activate the output pin to toggle on
> Compare Match
>
> }
>
> then you could use analogWrite, or:
> void pwmOn()
> {
> TCCR1A |= _BV(COM1A0); // activate output pin OC1A (pin
> 9), to toggle on/off when compare match}
>
> and
> void pwmOff()
> {
> TCCR1A &= ~ (_BV(COM1A1) | _BV(COM1A0)); // Disconnect, and hence
> decativate OC1A (pin 9) *ALWAYS*, no matter which mode
>
> }
>
> Assuming the timings are:
> 2.0ms on
> 27.8ms off
> 0.5ms on
> 1.5ms off
> 0.5ms on
> 3.5ms off
> 0.5 ms on
> repeated after a 63.0 ms pause
>
> then write a shoot() function, which would look like (remember I am
> not testing any of this):
>
> void shoot()
> {
> pwmOn();
> delayMicroseconds(2000);
> pwmOff();
> delayMicroseconds(27800);
> pwmOn();
> delayMicroseconds(500);
> pwmOff();
> delayMicroseconds(1500);
> pwmOn();
> delayMicroseconds(500);
> pwmOff();
> delayMicroseconds(3500);
> pwmOn();
> delayMicroseconds(500);
> pwmOff();
>
> delay(63); // wait for 63 mSec
>
> // repeat
> pwmOn();
> delayMicroseconds(2000);
> pwmOff();
> delayMicroseconds(27800);
> pwmOn();
> delayMicroseconds(500);
> pwmOff();
> delayMicroseconds(1500);
> pwmOn();
> delayMicroseconds(500);
> pwmOff();
> delayMicroseconds(3500);
> pwmOn();
> delayMicroseconds(500);
> pwmOff();
>
> }
>
> just call this from loop() when you want to tell the camera to take a
> picture.
>
>
>
> > I have read people have had to tweak when using delayMicroseconds and
> > digitalWrite.
>
> Strongly depends on what they are doing.
>
> I looked at the code (wiring.c), and I believe the error in
> delayMicrosecond() is pretty small because they have done the
> instruction timing for you. Very short delays (under 5 uSec) may be a
> bit problemtic though.
>
> In the timer-based approach, the timer is happily generating the 38KHz-
> ish signal, and the error on pwmOn() and pwmOff() are fairly
> consistent (and should be sub microsecond), so it is very unlikely to
> be an issue.
>
> For many applications (like this one) an Arduino is crazy fast, when
> you can let its hardware do the 'heavy lifting'.
> Once you use the timer to do the basic modulated signal, there isn't
> anything which is timing critical (probably within 50 uSec, or 800
> instructions is good enough).
>
> digitalWrite does have checks to make sure it is working correctly,
> and is general purpose (have a look at wiring_digital.c), which slows
> down how fast it turns a pin on or off.
> I measured it a while ago, and I think it was around a 100KHz, but it
> may have been a bit faster (it is pretty easy to test).
> Even that is likely to be well within the tolerance of the camera.
>
> When I care about speed, I use code like:
> TCCR1A |= _BV(COM1A0);
> which compiles down to similar code to assembler programmer would
> write anyway.
>
> IMHO, it is usually *much* easier to get code working in C, and then
> make it fast enough, than do it all in assembler.
> A good rule of thumb is more than 80% of the performance of a program
> is determined by less than 20% of the code.
> It is unusual to find a problem where it is too hard to get the code
> going well enough in C, but easier in assembler.
>
> > Is there a list of how long each Arduino operation
> > should take?
>
> Yes, look at:
http://www.atmel.com/dyn/resources/prod_documents/8161S.pdf
> Section 6. Instruction Set Summary, page 12.
> It is measured in cycles. An arduiino's ATmega is running at 16MHz-
> ish, so 0.0625-ish uSec/cycle.
>
> > I suppose that is one benefit of using Assembly, you know
> > how many cycles each op will take.
>
> True, but what good will it do?
> It is quite a lot of work to keep accounting accurately, especially
> when the code is experimental and changing.
> The timer interrupt will still fire, so every millisecond, the code
> will take much longer than it looks.
> It is pretty difficult to get two code paths (e.g. in an if () ...
> else ...) to come out the same, and loops can be messy too.
>
> If you move code to a processor which runs at 8MHz (like a lillypad),
> the code would need a rewrite to correct the timing.
>
> I'd rather use reasonably accurate clocks, and just change a bit of
> the setup and a few numbers.
>
> IMHO a key point is I think most mass produced electronics are pretty
> forgiving of errors under 20% because they have to work at widely
> differing temperatures, with stuff made years apart using components
> with 10% tolerances. You could do an experiment with the camera to see
> how forgiving it is.
>
>
>
> > It's funny you should mention an intervalometer as my Dad uses the
> > camera to take photos of weather phenomena a lot. Maybe he could use
> > it to capture a setting sun every min or two to get the best picture.
>
> Yes, that would be straightforward.
>
> You could try to be even sneakier, and measure the light levels, or
> using filters, measure the colour of light !-)
> With experimentation, you may be able to look for coloured sky (a few
> sensors pointed at different parts of the sky).
> I am not a photographer though, so I have little intuition about how
> much effort that would take to get right.
>
> Another thing to try is long exposure photo's of the sky at night:
http://www.flickr.com/photos/steventheamusing/3035302174http://www.digitalfieldguide.com/blog/1413
>
> Do you know if there are other features than 'shoot' available on IR
> for that camera?
> If it lets you adjust exposure as well as shoot, you could do some Hi-
> Dynamic Range images.
>
> > Also I have read you can hook up some kind of light sensor that
> > detects lighting and activates the shutter,
>
> Oops. commented above.
>
> Yes, light sensors like these:
http://uk.rs-online.com/web/search/searchBrowseAction.html?method=ret...
>
> are sensitive to visible light, and are pretty easy to use (you need a
> resistor and a small capacitor).
>
> You would read the light level with analogRead() and control when to
> 'shoot' a picture.
> With this approach, you could shoot a sequence of pictures which shows
> the light gently falling (or on a morning, getting lighter).
>
> > although I suppose IR
> > would be too slow to activate this.
>
> How fast do you want to trigger the camera?
>
> The total time to send the 'shoot' signal twice is roughly 140mSec,
> which is comparable (a bit slower) to human reaction time. If it
> triggers on the first 'signal' that is under 40mSec, which is probably
> faster than most people can shoot (and may be faster than the camera).
> So maybe just give it a go.
>
> > I am guessing the camera would
> > have some kind of port for a wired based remote shutter. Maybe thats a
> > seperate project ;-)
>
> :-)
>
>
>
> > My next question after getting a prototype working on an Arduino was
> > going to be, how do I build this into a permanent key fob, power by a
> > button battery. Not heard of ATtiny, so will go away and research.
>
> Have a look at Rapid electronics:
http://www.rapidonline.com/Electronic-Components/Integrated-Circuits/...
>
> They are exactly the same core processor as the Arduino, so your
> normal code will work unchanged. To make them smaller and cheaper they
> have a subset of an ATmega's peripheral hardware (e.g. fewer PWM's or
> ADC's, no UART), and smaller memories. ATtiny's come in smaller
> package than ATmega's (under 8mm x 6mm if you really want it).
>
> This application is so small, you may be able to make do with a 20,
> 14, or even 8 pin device (depends on the human interface you want to
> use, because the IR control only uses 1 pin).
>
> Lots of information at:
http://www.atmel.com/dyn/products/devices.asp?family_id=607#791
>
> It needs some sort of AVR ISP to get the program in, but once that's
> done, it runs like an Arduino's ATmega.
>
>
>
> > The only thing I have is my Arduino and a few components, which all
> > arrived in the post this morning :-)
> > I had time to compile and upload the blink sketch to confirm things
> > worked.
>
> It is a wonderful feeling when it works !-)
> I got such a thrill, and I've been programming a long time. When I got
> some school children to do it, they were a bit amazed that it was
> within their capability to make something act or react.
>
> With a bit of fiddling around you can make a stroboscope, which can
> 'freeze' things like a spinning fan, or show a guitar string vibrate.
> Get a potentiometer and you can use that to adjust the strobe speed.
> (I hope you are not sensitive to flashing lights. If you are be don't
> do it).
>
>
>
> > I am guess an AVR In Circuit Programmer is somethign you plug an AT
> > chip into to upload code to it.
>
> Yes, exactly. It treats the Atmega like a little memory chip, and just
> writes the program into it.
>
> > (Basically what the Arduino does, but without all the other stuff?)
>
> No, the Arduino processor has a program already loaded into it, called
> a bootloader.
> When you power-on an Aduino, that bootloader program runs on the
> Arduino, and starts looking for a program to come down the wire, and
> it is the bootloader program (talking to the host pc, which is running
> a download program called AVRdude [under the Arduino IDE covers])
> which takes the program off the wire and puts it into flash memory.
>
> > I am going to nip to Maplins and pick up an IR LED. Do you think I
> > will need anything else to get the prototype going? I am thinking at
> > the most basic level I can wire the LED between 13 and GND and just go
> > from there to get it working?
>
> You will need a resistor to limit the current, about 220 ohms
> (anything from 180 to 560 ohm will do).
> I would also get a few ordinary coloured LEDs; I find it really
> frustrating to try debugging something I can't see :-)
>
> If you want to really go for long distance, you might want to shop
> around and get a high-power, narrow focus IR LED and a transistor to
> drive it. But I suggest you get it working first, then upgrade the
> electronics once the code is right.
>
> You can start experimenting on pin 13, but you will need to move to a
> PMW pin if you are going to try the timer-modulation approach.
>
>
>
> > Not going to have any time to play with it as I am away this weekend,
> > so will be coming to the meeting next week all ready to start it.
> > Would appreciate your help if you are going to be there.
>
> Yes. I will be there.
> I will bring the ATmega manual too, so we can look up the right
> settings for the timer - once that is correct, you are probably good
> to go.
>
>
>
> > Also what does the G in your name stand for?
>
> I rarely broadcast that so it makes spam very easy to filter out :-)
> I will tell you when I see you, if that is okay?
>
> GB-)> Cheers,
> > Mike