Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 08:31 08 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Using an Apple IR Remote with MicroMite

     Page 1 of 2    
Author Message
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 12:11pm 03 Aug 2014
Copy link to clipboard 
Print this post

All,

I recently needed a method of selecting options & menu settings in a recent MicroMite project design. Initially I used a rotary encoder complete with push button and RGB led.
However, when it came to casing the project (an LED matrix clock/thermometer) the rotary encoder just made it feel 'horrible'. So I rattled my brain and thought Why not use an Apple IR remote? (like the one shown below)


I thought if I add a simple IR receiver to my circuit and write some code then I should be able to get things going with the end result of a nicely designed remote (only cost £15 brand new) and no 'horrible' buttons on my clock.

Note: The remote has 7 buttons which can provide full menu navigation and option selection.

I studied the datasheets and have ended up with a nice interrupt driven subroutine. The only additional component I used was a TSOP4838 and connected as shown in the MicroMite User Manual (Infrared Remote Control Decoder section).

I will send the details to Dave so he can post it on his excellent mmreference website.

In the meantime here is the debug code I used to get the required result (comments included).

IF YOU WANT MORE EXPLANATION THEN PLEASE PM ME!!

Hope some of you find this useful to include in your own projects . . . .

Regards,
WW

[code]
'**************************************************
'*** Program to decode Apple IR Remote Signals ***
'*** ***
'*** Use a 38KHz IR Receiver i.e. TSOP4838 ***
'*** This single component needs to be ***
'*** connected to an INT pin ***
'*** ***
'*** Will display button name when pressed: ***
'*** Up, Down, Left, Right, Centre, Menu, Play ***
'*** Repeats output if button held ***
'*** ***
'*** This code works for CPU 40 and CPU 48 only ***
'*** ***
'*** Written by White Wizzard 02 Aug 2014 ***
'*** ***
'**************************************************

'To help understand/debug, this code provided will display 6 lines of 'data' (5 if non Apple remote) as follows:
'1=Sampled waveform as 0's and 1's
'2=Count of consecutive 0's and 1's (represents pulse durations)
'3=Count of each high pulse (excludes first batch of 1's which is a sync pulse)
'4=bit values based on high-pulse durations (from above line) formatted into nibbles
'5=four byte values (hex) representing data received
'6=IF Apple Remote IR signal, then the name of the button pressed

'----------------------------------------------------------- ------------------------------------------------------------ ---------
InfraPin=10 'Select IR Receiver Pin. MUST BE on a MicroMite INT input (i.e. Pin 10 on 44-pin MicroMite
setpin InfraPin,INTL,Remote 'Set IR Receiver pin as low going interupt (Jumps to 'Remote' when IR signal detectected)
Dim dat(200) 'Array to store samples of IR signal (will be sequences of 0's and 1's) eg 00100111001001111010111
Dim One(200) 'Array to store count of consectuctive 1's in above array. So above eg: 1, 3, 4, 1, 3
'uses variables: i, ww, SeekBit, count, Byte1, Byte2, Byte3, Byte4 (and in debug mode uses: split)

'----------------------------------------------------------- ----------------------
'MAIN PROGRAM GOES HERE . . . .
Do
pause 100
Loop
'----------------------------------------------------------- ----------------------

'Interupt Service Routine To process any IR detected
Remote:

For i = 1 to 200 'immediately capture 200 samples of incoming IR waveform into array dat(i)
dat(i)=pin(InfraPin)
pause 0.175 'this delay allows for the smallest array with enough detail; makes processing quicker
next i

for i = 1 to 200 '********************** This loop prints the sampled waveform as 0's and 1's
print str$(dat(i)); '**********************
next i '**********************
print '**********************

' NOW COUNT THE NUMBER OF CONSECUTIVE 0's and 1's IN THE SAMPLED WAVEFORM
ww=0 'this will count the total number of high pulses in the sample i.e. howm many groups of 1's
i=0 'index pointer to work through the 200 samples
SeekBit=0 'initially we are counting 0's (the SeekBit). When a 1 is encountered we will then be 'seeking' 1's.
Loop1:
count=1 'count' stores the running count of consecutive value seekbits
Loop2:
If i = 200 then goto Done 'exit process once we have reached the end of the sample
i = i + 1 'increment index pointer
if dat(i) = SeekBit then 'if the current sample value is the same as previous (SeekBit) then increment count
count = count + 1
else
print (str$(SeekBit)) + ":" + str$(count) + " "; '********************** IF different (i.e. pulse edge) then display count reached for consecutive SeekBit
if SeekBit = 0 then 'now change SeekBit (if 1 then make 0, if 0 then make 1)
SeekBit = 1
else
SeekBit = 0
One(ww) = count 'whwnever we have counted a batch of consecutive 1's then store the count in array One with a index of ww
ww = ww + 1 'increment ww index ready for next time
endif
goto Loop1 'carry on through sample but need to reset Count to 1 as an edge level was detected
endif
goto Loop2 'carry on through sample (here if current value = previous value)


Done:
'simply display the count reached for each high pulse i.e. indicate duration of high pulse
print '**********************
for i = 1 to ww-1 '**********************
print str$(One(i)) + " "; '**********************
next i '**********************
print '**********************

'NOW CONVERT DURATION OF EACH HIGH PULSE INTO A BIT VALUE
split = 1 '********************** using split to format above count into binary nibbles
For i = 1 to ww-1 'work through however many high pulses are in sample
if One(i) < 3 then 'if count <3 then this represents a short pulse meaning a bit value of 0
One(i)=0
print "0"; '********************** print 0
else
One(i)=1 'if count >=3 then this represents a long pulse meaning a bit value of 1
print "1"; '********************** print 1
endif
if split = 4 then '********************** if 4 bits been displayed the break it with a space.
print " "; '********************** Nibbles make it easier to read / debug
split = 0 '**********************
endif '**********************
split = split + 1 '**********************
Next i
print '**********************

'NOW CONVERT 8 NIBBLES INTO THE 4 BYTES REQUIRED TO BE EXAMINED. EACH PAIR OF NIBBLES IS LSB first
Byte1 = 0
for i = 0 to 7
Byte1 = Byte1 + (2^i) * One(i+1)
Next i
print "&h" + Hex$(Byte1); '**********************

Byte2 = 0
for i = 0 to 7
Byte2 = Byte2 + (2^i) * One(i+9)
Next i
print " &h" + hex$(Byte2); '**********************

Byte3 = 0
for i = 0 to 7
Byte3 = Byte3 + (2^i) * One(i+17)
Next i
print " &h" + hex$(Byte3); '**********************

Byte4 = 0
for i = 0 to 7
Byte4 = Byte4 + (2^i) * One(i+25)
Next i
print " &h" + Hex$(Byte4) '**********************
print '**********************

'FINALLY CHECK THE 4 BYTES FOR VALIDITY
If hex$(Byte1)="EE" and hex$(Byte2)="87" then 'IF APPLE IR REMOTE THEN THE FIRST TWO BYTES WILL HAVE THESE VALUES
if Byte3=11 then Print "Up" ' IF FIRST TWO BYTES OK THEN CHECK THIRD BYTE WHICH REPRESENTS BUTTON PRESSED
if Byte3=13 then Print "Down" ' NOTE 1: 4th BYTE REPRESENTS APPLE REMOTE ID (Can be used as an additional check to ensure correct remote is being used)
if Byte3=8 then Print "Left" ' NOTE 2: HERE WE ARE PRINTING THE BUTTON PRESSED. COULD INSTEAD LOAD A VARIABLE WITH A USER SPECIFIC VALUE
if Byte3=7 then Print "Right"
if Byte3=93 then Print "Centre"
If Byte3=2 then Print "Menu"
If Byte3=94 then Print "Play"
endif
print '**********************
ireturn
[/code]
For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9067
Posted: 01:34pm 03 Aug 2014
Copy link to clipboard 
Print this post

Who's a clever boy, then? (rhetorical!)

Well done - nice work, WW.

EDIT: I would also suggest you submit that to the MMBASIC library.Edited by Grogster 2014-08-04
Smoke makes things work. When the smoke gets out, it stops!
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 03:40pm 03 Aug 2014
Copy link to clipboard 
Print this post

White, this is very cool. I am super impressed. Quick question, as a newbie, does this interrupt routine take a long time to do its thing? What happens if a key is held down, does it repeat? It is late here so can't try it right now but will do tomorrow after work. Again, nice idea and implementation!!!!

edit...

do you know if this works with the old style apple remotes?

sorry for crappy pic...



Edited by viscomjim 2014-08-05
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 06:07pm 03 Aug 2014
Copy link to clipboard 
Print this post

Well done WW
As this remote uses the NEC protocol this opens a whole world of possibilities

I have a few NEC type remotes will test and post the results

Regards
Jman
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 07:07pm 03 Aug 2014
Copy link to clipboard 
Print this post

  viscomjim said  
do you know if this works with the old style apple remotes?


Yes it will but you may need to add a different value to check for the Centre button.

This is easy to do; simply run the code and it will display the
value of the button pressed. Look at the third byte (from the four bytes returned) and add this value into the 'checking' at the end of the code supplied.

Different logic will be required for other NEC protocol remotes as their waveforms may be a longer duration (in which case you will require a bigger initial sample array).
The byte values returned will then need to be deciphered.

However, that said, the core of the code can be used to convert the NEC protocol into byte values.

Good luck; let us know how you get on . . . .

For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 07:18pm 03 Aug 2014
Copy link to clipboard 
Print this post

  viscomjim said  Quick question, as a newbie, does this interrupt routine take a long time to do its thing?

The simple answer is no it doesn't take a long time. In fact it is very quick and will easily keep up with button presses from the Apple remote.

NOTE: The code supplied displays a list of data showing how to interpret the IR waveform received. It is NOT recommended to do all this 'printing' in an interrupt routine. Therefore simply remove all the lines that end with '*********** once you are happy with how it works.
ALSO: I would slightly modify the end of the routine where I convert the value of Byte3 into a printed output. I would recommend loading a variable with a unique user defined value within the interrupt sub, and then interpret this value in the main code loop.

I cannot comment on speed for other NEC protocol remotes but it should be fine. Hopefully someone can report back with regards to other remotes . . . .

WW
For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 09:38pm 03 Aug 2014
Copy link to clipboard 
Print this post

  viscomjim said  What happens if a key is held down, does it repeat?


The Apple remote will transmit a Sync pulse every 100mS when a button is held down. The way the code works (and with the time taken to interpret a sample) it will repeat the name of the button pressed at a rate of around 4 per second (haven't measured the timing accurately but this should be fast enough for most requirements).

I could add more logic to intercept when a button is held down (and exit the sample earlier) but this wasn't a requirement for my application.

If anyone wants some 'bespoke' code for the APPLE IR remote then please let me know what it is and I will see what I can do!

WW
For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 02:23pm 04 Aug 2014
Copy link to clipboard 
Print this post

So White, when are you going to post a pic of your matrix clock? Dying to see it....

Which led matrix are you using? I see that you can go either spi with the max7219 or i2c with the holtek chip. I want to get my hands on one to play with and am curious as to how fast the uMite can update things on either.Edited by viscomjim 2014-08-06
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 03:54am 05 Aug 2014
Copy link to clipboard 
Print this post

@viscomjim, I am waiting for the delivery of some red acrylic sheet (ordered on 24th July!) which I have been promised will be delivered tomorrow. I will post some photos once I have that in place!

I am using five SPI (MAX7219) matrix modules with each one selected individually by one of five Select lines. Therefore 5 select lines, 1 data, and 1 clock = 7 lines to drive display very fast.
BitBanging was way too slow as I am 'scrolling' the screen with various effects.

Also using one Com port to talk to GPS unit (for dead accurate time), one I/O for temp sensor, two I/O's for piezo buzzer (push-pull with two lines gives louder output), one I/O for IR sensor (Apple Remote), and also wanting to add a DAB module with digital controlled amp (but yet to decide on final modules). May add SOMOII too but have to watch the expenditure on this sideline project!!

WW


For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 04:08am 05 Aug 2014
Copy link to clipboard 
Print this post

White, that sounds awesome. Can't wait to see this! I added chime sounds to a uMite clock project using the MDfly module with a $7.00 stereo amp and it sounded way bigger than the unit looked like it should. The MDfly module used serial to access the MP3 sound bites and worked very well. You might want to look into that for ease of use. That module was also $7.00.

I know you work very hard on these projects and the fact that you share your code is very cool of you. I know I will be implementing the apple remote on something soon. Just ordered the tsop4838s and am patiently waiting on those to start playing with your code. I would love to see your implementation of the matrix on the uMite in MMbasic if you are so inclined. Soon we will have a nice group of components and libraries for the uMite that will make this little powerhouse very viable for a lot of cool projects.
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 04:26am 05 Aug 2014
Copy link to clipboard 
Print this post

@viscomjim

I looked at MDFly but I have a big issue with ordering quantities of 'cheap' modules from 'unsustainable' suppliers (interpret that however you wish ). That said, I will probably get one of the MDfly's just to play with personally.

I will be more than happy to post my Matrix code on Dave's excellent mmreference.com website once I have tidied it up a bit. (Will also post here on a new thread too).

I currently sell the TSOPs (both 38Khz and 40KHz version) on MicroMite.org and will now be adding the Apple Remote too. Even though the remote isn't currently on there, if anyone wants one then PM me and I can ship immediately. Cost is £17.50 for a brand new silver Apple Remote as pictured in my original post. (This cost is thanks to PayPal having to take their cut!).

WW

For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 01:44pm 09 Aug 2014
Copy link to clipboard 
Print this post

Hi All,

So far, WW IR code is working great with the following remotes I have. What fun!!!





I will be using the white older apple remote instead of a rotary encoder (just like WW) on a project for my mother in law... I think this will be easier for her to navigate. A gutted vintage table top radio that will have a uMite controlling an MP3 player with a small amp. The SD card will have many hours of Old Time Radio programs that can be found on the web as mp3 files. There are so many old time radio programs available that it will probably take a year to listen to it all. My personal favorite so far is the "whos on first..." from Abbot and Costello. I am even throwing in some real old radio commercials for products I am sure she has heard of, but I haven't. The old cigarette commercials are kind of funny too.

Thanks for the code WW.
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 911
Posted: 01:22am 10 Aug 2014
Copy link to clipboard 
Print this post

  Quote  My personal favorite so far is the "whos on first..." from Abbot and Costello.


Jim, I put "whos on first" up there with Monty Python's "Dead parrot" sketch as top classics.

GM
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 01:47am 10 Aug 2014
Copy link to clipboard 
Print this post

GM, found it on you tube, thats a riot!!!
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 06:11am 11 Aug 2014
Copy link to clipboard 
Print this post

Just a quick update... The apple remote is amazing when it comes to range. I tested this with several other remotes I had laying around and I can say that the apple white and aluminum version beat the rest by a factor of almost 3 in range. I don't know what they do different, but it truly works that much better. I did check the batteries to make sure the test was fair. At certain ranges the others also had corrupted data which I expected as the distance became larger, however that was not the case with the apple ones.

WW, did you try any other remotes and see a significant difference with the apple ones?
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 06:19am 11 Aug 2014
Copy link to clipboard 
Print this post

I did not try other remotes - this was purely an exercise to get the elegant & stylish Apple IR remote to work.
And the main reason for this (apart from looks) is as you say, the range is outstanding. In a 4m square room, no matter where I point the remote (and no matter where the MicroMite Rx, it just works!.

I reckon they drive the IR Tx LED at an above average current to get the increased performance.
Also, they only transmit the 'button' message once; and then if the button is held down, every 100mS is a much shorter message confirming button still pressed.

This is a sensible design: blast 32 bit (4 byte) message once at above average current (70mS), then a short (11mS) repeat message (sync bit + stop bit) every 100mS.

By the way, nice use for your remote you posted previously

WW


For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 06:42am 11 Aug 2014
Copy link to clipboard 
Print this post

For anyone interested, here is an overview of how to interpret the Apple IR remote message.

It shows a Logic Analyser screen-shot (download below) of the IR signal; along with instructions on how to interpret it.


2014-08-11_163758_Interpret_Apple_IR_Remote.zip

Somewhere the formatting got messed up between Windows and iMac.
For completeness, Byte 4 = Remote ID.

And the values of the button press (Byte 3) can be seen in the code previously posted.

Any questions then feel free to ask . . .

WW

For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 05:00am 13 Aug 2014
Copy link to clipboard 
Print this post

Hi White, I am not sure how far along you are on your project, but I have come across a weird problem with mine.

While running the code, all the buttons show up perfectly on the terminal when pressed just like they should, however, If I press a button, and then just let the remote sit there (not pressing anymore, just one press), randomly the last button pressed will magically print again like it was pressed again. There is no specific timing to it, it just happens.

It doesn't happen fast, just here and there. At first I thought it was noise being read by the interrupt pin, but looking at the code, you check 4 bytes of data before it can actually print something to the terminal. It doesn't matter which button I press, as long it is the last button pressed, it will randomly "press" again. I checked this with two different white apple remotes. Did you implement any kind of noise filtering between the TSOP4838 and the uMite in your project? Could that actually be the problem? I am stumped.

Edit...

I just did a bit of googling and came across this. I might give this a whirl tonight to see if it makes a difference. Also, are you powering your tsop4838 with 3.3v or 5v? I'm using 3.3v, maybe another problem.



Edited by viscomjim 2014-08-14
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 06:16am 13 Aug 2014
Copy link to clipboard 
Print this post

@viscomjim

I am using 5v and no problem observed here. No other components - just connected as per User Manual.

I will play with this later tonight to see if I can re-create your issue (will run at 3v3 first).

Be in touch in a few hours . . . . .

WW
For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 06:33am 13 Aug 2014
Copy link to clipboard 
Print this post

Thanks White, I will first try 5v also. Maybe that'll do it. The data sheet says 2.5 - 5.5v, but we'll see.

edit...

a little more googling and looking at the datasheet shows this...




as does quite a few sites. I will give this a whirl also when I get home and see if it makes a difference. I will keep you posted.Edited by viscomjim 2014-08-14
 
     Page 1 of 2    
Print this page
© JAQ Software 2024