Infra red + Remote control code issues

jensmith25

Senior Member
I have written code to perform various lighting effects using the 14M2 project board with IR reciever and sony remote from Picaxe.

Most of it is working ok but there seems to be issues where the code isn't switching from one effect to another, so when I push the next button on the remote, nothing happens. I also seem to have to wait for the one effect to finish before I can start another. It starts happening at "swon3". After that "swoffall" doesn't work either.

I am assuming that it's something to do with the pwm commands and that they're confusing the chip somehow but I can't figure out what it is. I wondered if the code needs to exit but I thought the "return" command did that. I can imagine it's somehting quite simple. I'm still a newbie with Picaxe but hopefully it's obvious to those of you with plenty of experience!

Code:
#Picaxe 14M2
'Iron Man remote control lighting.  Eyes, chest and hands.
'1) On - off  for the eyes
'2) On - off for the chest and hands
'3) Slow fade from 'off ' to full 'on', on for 6 seconds, then a flash, then slow fade back to 'off' for the chest and hands
'4) Slow fade On-off for the eyes
'5) Slow fade On-off for chest & hands

main:
	irin [1000,main],C.0,b0  ;wait for new signal
	if b0 = 0 then swon1   ;switch on button 1 - eyes on
	if b0 = 1 then swon2  ;switch on button 2 - chest & hands on
	if b0 = 2 then swon3  ;switch on button 3 - eyes slow fade on
	if b0 = 3 then swon4  ;switch on button 4 - chest & hands slow fade on
	if b0 = 4 then swon5  ;switch on button 5 - slow fade from off to on for 6 seconds, flash, slow fade to off for chest and hands
	
	if b0 = 5 then swoff1  ;button 6 - switch off eyes
	if b0 = 6 then swoff2  ;button 7 - switch off chest & hands
	if b0 = 7 then swoff3  ;button 8 - fade off eyes
	if b0 = 8 then swoff4  ;button 9 - fade off chest & hands
	
	if b0 = 21 then swoffall  ;switch off all lights
	
	goto main
	
swon1:  ;switch on eyes - button 1
	high B.4
	goto main
	
swoff1:  ;switch off eyes - button 6
	low B.4
	goto main
	
swon2:  ;switch on chest & hands - button 2
	high B.2
	goto main
	
swoff2:  ;switch off chest & hands - button 7
	low B.2
	goto main
	
swon3:  ;fade on eyes - button 3
	for b1=0 to 255
        pwmout B.4,255,b1
        pause 30
    next
    goto main
    
swoff3:   ;fade off eyes - button 8
	for b2=255 to 0 step -1
        pwmout B.4,255,b2
        pause 30
    next
    goto main
    
swon4:  ;fade on chest & hands - button 4
	for b3=0 to 255
        pwmout B.2,255,b3
        pause 30
    next
    goto main
    
swoff4:  ;fade off chest & hands - button 9
	for b4=255 to 0 step -1
        pwmout B.2,255,b4
        pause 30
    next
    goto main
	
swon5:  ;fade on chest & hands, 6 secs on, flash, fade off  - button 5
	for b5=0 to 255  ;fade to full on
        pwmout B.2,255,b5
        pause 30
    next
    pause 6000  ;pause 6 seconds
    pwmout 2, 255,1022:pause 140 ;briefly flash full brightness
    for b5=255 to 0 step -1  ;fade to full off
        pwmout B.2,255,b5
        pause 30
    next
    goto main
    
 swoffall:  ;switch off all lights
 	low B.2
 	low B.4
 	goto main
Any advice on this would be very much appreciated.
 

erco

Senior Member
Naturally, you have to wait for each routine to finish, since you are only looking for IRINput in your main program loop. You have several for/next loops with pause 30s in your PWM routines. You could also look for IRINput in place of those pause 30s in desired, using IRIN's timeout feature as you did in main.

WRT your program locking up after some routines, this is a great chance to debug your program using the serial monitor. Insert a SERTXD ("made it to main",13,10) command after each label, changing "main" to the label so you can track your program's progress as it's running.

Also, add sertxd ("irin= ",#b0,13,10) in main, immediately after your IRIN command to verify which command was received.

You're very close, keep up the excellent work and be sure to post a video of your finished Iron Man in action!
 

The bear

Senior Member
Hi Jen,
Good to see a new project.
While we are waiting for the real brains to answer your questions .
I have found the remote control often responds to the next lower number, than the one you have pressed.
Did you find this also?
Regards, Bear (Beginner).
 

erco

Senior Member
@the bear: Yes, that's SOP for digits 0-9 in the Sony protocol. Attached image is the key map for my favorite TV-only remote.
 

Attachments

jensmith25

Senior Member
Naturally, you have to wait for each routine to finish, since you are only looking for IRINput in your main program loop. You have several for/next loops with pause 30s in your PWM routines. You could also look for IRINput in place of those pause 30s in desired, using IRIN's timeout feature as you did in main.

WRT your program locking up after some routines, this is a great chance to debug your program using the serial monitor. Insert a SERTXD ("made it to main",13,10) command after each label, changing "main" to the label so you can track your program's progress as it's running.

Also, add sertxd ("irin= ",#b0,13,10) in main, immediately after your IRIN command to verify which command was received.

You're very close, keep up the excellent work and be sure to post a video of your finished Iron Man in action!
Thanks Erco, I'll give those a try.

It's for a customer so if I get sent a photo I'll certianly post it.
 

jims

Senior Member
Why not use a call to a subroutine like this in place of your "irin' command, and have the numeric key codes match the key top legend. Saves me a lot of "grief'. JimS

Code:
'************************************
'** Subroutine to get data from IR clicker
'*  & correct the numeric key offset.
'************************************
use_clicker:  
	irin C.0,b0	'Read data from IR clicker.
	'do :loop until pinC.0 is off 'Optional to wait until key is released.
	if b0>9 then return:endif	'Return if NOT a numeric key.
	b0=b0+1//10	'Correct numeric key offset.
	return
 

jensmith25

Senior Member
Jim - is an IR clicker literally just the remote I'm already using or something different? I've not heard of that term before until you mentioned it on my post the other day.
 

jims

Senior Member
Jim - is an IR clicker literally just the remote I'm already using or something different? I've not heard of that term before until you mentioned it on my post the other day.
Sorry about that Jen... Yes I do mean the remote that you're using. Guess I'm showing my age. Back in the 60's when we got our first remote TV controls they made a "clicking" sound and we called them "clickers". I never realized that it's not a universal thing to call them "clickers". JimS
 

Dartmoor

Member
I had a similar problem with my first project.
Using multiple tasks (manual "getting started" pages 62-64) is one way around it?
Use 3 loops. One reads the infra-red, one does the eyes & one does the chest/hands.
That way the pauses in the fades will not affect reading the input or the other outputs?
You may need to use some more variables so that the fade on or off can be stored after another button has been pressed?

Hope that makes sense?
I am a newbie too.
 

jensmith25

Senior Member
I had a similar problem with my first project.
Using multiple tasks (manual "getting started" pages 62-64) is one way around it?
Use 3 loops. One reads the infra-red, one does the eyes & one does the chest/hands.
That way the pauses in the fades will not affect reading the input or the other outputs?
You may need to use some more variables so that the fade on or off can be stored after another button has been pressed?

Hope that makes sense?
I am a newbie too.
Thanks Dartmoor. I will try the loop option as well.

I'm not sure what you mean about additional variables? I've already set up each effect with it's own variable (b1 to b5) so where would I add additional ones and what will that do?
 

Dartmoor

Member
Thanks Dartmoor. I will try the loop option as well.

I'm not sure what you mean about additional variables? I've already set up each effect with it's own variable (b1 to b5) so where would I add additional ones and what will that do?
I think you are right & have enough variables. Just need to make sure it is not trying to fade on & off at the same time.

Have fun!
 

jensmith25

Senior Member
Dartmoor - I'm unsure how to incorporate the different loops. Gave it a try and now all the lights are solid on so obviously done something majorly wrong. You don't happen to have a sample of your code to give me an idea do you? It's the incorporation into the eyes and chest effects that I'm struggling with.

eg, loop 2 for the eyes...

Code:
start1:
swon1:  ;switch on eyes - button 1
	high B.4
	goto start1
	
swoff1:  ;switch off eyes - button 6
	low B.4
	goto start1
	
swon3:  ;fade on eyes - button 3
	for b1=0 to 255
        pwmout B.4,255,b1
        pause 30
    next
    goto start1
    
swoff3:   ;fade off eyes - button 8
	for b2=255 to 0 step -1
        pwmout B.4,255,b2
        pause 30
    next
    goto start1
How do I incorporate the start 1 loop? as what I've done I don't think is right.
 

Dartmoor

Member
Dartmoor - I'm unsure how to incorporate the different loops. Gave it a try and now all the lights are solid on so obviously done something majorly wrong. You don't happen to have a sample of your code to give me an idea do you? It's the incorporation into the eyes and chest effects that I'm struggling with.

eg, loop 2 for the eyes...

Code:
start1:
swon1:  ;switch on eyes - button 1
	high B.4
	goto start1
	
swoff1:  ;switch off eyes - button 6
	low B.4
	goto start1
	
swon3:  ;fade on eyes - button 3
	for b1=0 to 255
        pwmout B.4,255,b1
        pause 30
    next
    goto start1
    
swoff3:   ;fade off eyes - button 8
	for b2=255 to 0 step -1
        pwmout B.4,255,b2
        pause 30
    next
    goto start1
How do I incorporate the start 1 loop? as what I've done I don't think is right.
I think your "start1:" goes straight in to set B.4 high? (& I guess similar for hands/chest?)
The "if . . then" statements associated with those subroutines probably need to be moved from "main" into "start1"?
As there is only a 1 sec wait whilst reading the I/R input, the on/off controls can probably stay in main?

The cause of the original problem is the delay caused by the for/next loops. The pause 30 itself is not an issue because it is 30ms (not 30 sec).
Therefore another solution may be to check the b0 value every time round the for/next loop.

I still use the flowcharts whenever possible, so my code looks a bit odd when converted to basic.
I should probably stick to asking questions, rather than trying to answer them!
 

jensmith25

Senior Member
Hmm, think I've screwed it up good and proper now. It seems totally confused and is doing random fade on effects non-related to which button I press!

Code:
#Picaxe 14M2
'Iron Man remote control lighting.  Eyes, chest and hands.
'1) On - off  for the eyes
'2) On - off for the chest and hands
'3) Slow fade from 'off ' to full 'on', on for 6 seconds, then a flash, then slow fade back to 'off' for the chest and hands
'4) Slow fade On-off for the eyes
'5) Slow fade On-off for chest & hands

'Start loop 1 for on/off effects
start0:
	irin [1000,start0],C.0,b0  ;wait for new signal
	if b0 = 0 then swon1   ;switch on button 1 - eyes on
	if b0 = 1 then swon2  ;switch on button 2 - chest & hands on
	if b0 = 5 then swoff1  ;button 6 - switch off eyes
	if b0 = 6 then swoff2  ;button 7 - switch off chest & hands	
	if b0 = 21 then swoffall  ;switch off all lights
	goto start0
	
swon1:  ;switch on eyes - button 1
	high B.4
	goto start0
	
swoff1:  ;switch off eyes - button 6
	low B.4
	goto start0
	
swon2:  ;switch on chest & hands - button 2
	high B.2
	goto start0
	
swoff2:  ;switch off chest & hands - button 7
	low B.2
	goto start0
	
swoffall:  ;switch off all lights
 	low B.2
 	low B.4
 	goto start0

'Start loop 2 for eyes effects
start1:
	
irin [1000,start1],C.0,b1  ;wait for new signal
if b1 = 2 then swon3  ;switch on button 3 - eyes slow fade on
if b1 = 7 then swoff3  ;button 8 - fade eyes off

swon3:
	for b3=0 to 255
        pwmout B.4,255,b3
        pause 30
    next
    goto start1

swoff3:
	for b4=255 to 0 step -1
        pwmout B.4,255,b4
        pause 30
    next
    goto start1	
	
'Start loop 3 for chest & hands effects
start2:

irin [1000,start2],C.0,b2  ;wait for new signal
if b2 = 3 then swon4  ;switch on button 4 - chest & hands slow fade on
if b2 = 8 then swoff4  ;button 9 - fade off chest & hands
if b2 = 4 then swon5  ;switch on button 5 - slow fade from off to on for 6 seconds, flash, slow fade to off for chest and hands

swon4:
	for b5=0 to 255
        pwmout B.2,255,b5
        pause 30
    next
    goto start2

swoff4:
	for b6=255 to 0 step -1
        pwmout B.2,255,b6
        pause 30
    next
    goto start2

swon5:
	for b7=0 to 255  ;fade to full on
        pwmout B.2,255,b7
        pause 30
    next
    pause 6000  ;pause 6 seconds
    pwmout 2, 255,1022:pause 140 ;briefly flash full brightness
    for b7=255 to 0 step -1  ;fade to full off
        pwmout B.2,255,b7
        pause 30
    next
    goto start2
Maybe I should go back to the original.
 

hippy

Ex-Staff (retired)
Maybe I should go back to the original.
That would be my suggestion, though even better; define exactly what it is you are attempting to achieve. And clarify if things were stopping working, hanging, or simply were not responding as quickly as you had hoped for with your initial code.

I could see nothing wrong with your code other than it would be slow to respond after some actions were initiated. If it is other than that, it would be best to identify what that problem is as that problem will likely remain present not matter what you try.

I suspect the actual problem is that you want an immediate response to any IR button push whereas the code only responds once an initiated action completes.
 

jensmith25

Senior Member
That would be my suggestion, though even better; define exactly what it is you are attempting to achieve. And clarify if things were stopping working, hanging, or simply were not responding as quickly as you had hoped for with your initial code.

I could see nothing wrong with your code other than it would be slow to respond after some actions were initiated. If it is other than that, it would be best to identify what that problem is as that problem will likely remain present not matter what you try.

I suspect the actual problem is that you want an immediate response to any IR button push whereas the code only responds once an initiated action completes.
I've tried waiting for the effects to finish when I started this but there's something screwed after I start the pwm effects which is why I posted originally. As soon as the first pwm effect starts swon3 the rest of the programme fails to work properly.

swon3, 4 & 5 all work but swoffall doesn't nor does any other swoff command, no matter how long I wait.
 

hippy

Ex-Staff (retired)
So did you add SERTXD commands as erco suggested to trace where execution is going ?

Have you tried commenting out the active code within 'swon3', just leaving the 'goto main', and seeing what happens then ? If it still misbehaves it is probably not something within the 'swon3' routine causing the problem, is something else. You can then proceed to do similar with other parts of the code. If removing something causes things to behave as expected when it didn't before you can concentrate on that code, analyse what might be causing the difference.

You could alternatively take all the active code out and just have jumps to the now empty routines which go back to main. Check that works with SERTXD's and then start adding code in, see what breaks it.

At some point you should be able to identify a case where "if I have these lines it fails, if I remove them it works". From that it should be possible to analyse what the issue may be. The hard part, admittedly, is getting to what those few lines may be. And presumes the fault is within the program.

Adding just a SERTXD before and after the IRIN will let you know that the program is getting back to reading IRIN and what it's getting and acting on after that. You never know, it could be something as bizarre as the remote control going wonky after pressing the key which runs 'swon3'.
 

hippy

Ex-Staff (retired)
Or it could simply be that you are not explicitly turning PWMOUT channels off before you try to control the associated pins directly with HIGH and LOW commands.
 

jensmith25

Senior Member
So did you add SERTXD commands as erco suggested to trace where execution is going ?

Have you tried commenting out the active code within 'swon3', just leaving the 'goto main', and seeing what happens then ? If it still misbehaves it is probably not something within the 'swon3' routine causing the problem, is something else. You can then proceed to do similar with other parts of the code. If removing something causes things to behave as expected when it didn't before you can concentrate on that code, analyse what might be causing the difference.

You could alternatively take all the active code out and just have jumps to the now empty routines which go back to main. Check that works with SERTXD's and then start adding code in, see what breaks it.

At some point you should be able to identify a case where "if I have these lines it fails, if I remove them it works". From that it should be possible to analyse what the issue may be. The hard part, admittedly, is getting to what those few lines may be. And presumes the fault is within the program.

Adding just a SERTXD before and after the IRIN will let you know that the program is getting back to reading IRIN and what it's getting and acting on after that. You never know, it could be something as bizarre as the remote control going wonky after pressing the key which runs 'swon3'.
I think SERTXD only works with the PC version? I have a Mac. The MacAXEpad software doesn't have the terminal window so I might have to just try the commenting out option unless I'm wrong about the SERTXD command.
 

hippy

Ex-Staff (retired)
As far as I am aware the MacAXEpad should support Terminal. But take a look at previous post #20 which may have been missed as your reply has come on to a new page.
 

jensmith25

Senior Member
Or it could simply be that you are not explicitly turning PWMOUT channels off before you try to control the associated pins directly with HIGH and LOW commands.
Ah, so on reading about pwmout on the website, the website says you can issue the "'pwmout pin, off' command but it stops all channels so wasn't sure if that was what I need or if it's this bit " To just stop one channel use 'pwmduty pin, 0'.

My mistake about Terminal. I looked it up and it is availaible. I didn't realise.
 

hippy

Ex-Staff (retired)
The "PWMOUT pin, OFF" command turns off the PWMOUT channel for the named pin. It is only where two PWMOUT channels share the same internal timer that both associated pins will be turned off and PWMDUTY would be used to turn one off but not the other.

From my recollection, on a 14M2, B.2 and B.4 each use separate timers so can be turned off individually.
 

jensmith25

Senior Member
Thanks Hippy.

It turns out the main issue was using b1, b2 variables. My other half suggested using w1, w2 etc.

It seems to now be working with no errors, including using the "PWMOUT pin, OFF command.

I also realised the LEDs were not fading to full brightness so that is also now fixed.

Code:
#Picaxe 14M2
'Iron Man remote control lighting.  Eyes, chest and hands.
'1) On - off  for the eyes
'2) On - off for the chest and hands
'3) Slow fade from 'off ' to full 'on', on for 6 seconds, then a flash, then slow fade back to 'off' for the chest and hands
'4) Slow fade On-off for the eyes
'5) Slow fade On-off for chest & hands

main:
	irin [1000,main],C.0,b0  ;wait for new signal
	if b0 = 0 then swon1   ;switch on button 1 - eyes on
	if b0 = 1 then swon2  ;switch on button 2 - chest & hands on
	if b0 = 2 then swon3  ;switch on button 3 - eyes slow fade on
	if b0 = 3 then swon4  ;switch on button 4 - chest & hands slow fade on
	if b0 = 4 then swon5  ;switch on button 5 - slow fade from off to on for 6 seconds, flash, slow fade to off for chest and hands
	
	if b0 = 5 then swoff1  ;button 6 - switch off eyes
	if b0 = 6 then swoff2  ;button 7 - switch off chest & hands
	if b0 = 7 then swoff3  ;button 8 - fade eyes off
	if b0 = 8 then swoff4  ;button 9 - fade off chest & hands
	
	if b0 = 21 then swoffall  ;switch off all lights
	
	goto main
	
swon1:  ;switch on eyes - button 1
	high B.4
	goto main
	
swoff1:  ;switch off eyes - button 6
	low B.4
	goto main
	
swon2:  ;switch on chest & hands - button 2
	high B.2
	goto main
	
swoff2:  ;switch off chest & hands - button 7
	low B.2
	goto main
	
swon3:  ;fade on eyes - button 3
	for w1=0 to 1023
        pwmout B.4,255,w1
        pause 5
    next
    	'pwmout B.4,255,1023
    goto main
    
swoff3:   ;fade off eyes - button 8
	for w2=1023 to 0 step -1
        pwmout B.4,255,w2
        pause 5
    next
    	pwmout B.4,0,0
    goto main
    
swon4:  ;fade on chest & hands - button 4
	for w3=0 to 1023 
        pwmout B.2,255,w3
        pause 5
    next
    	'pwmout B.2,255,1023
    goto main
    
swoff4:  ;fade off chest & hands - button 9
	for w4=1023 to 0 step -1
        pwmout B.2,255,w4
        pause 5
    next
    	pwmout B.2,0,0
    goto main
	
swon5:  ;fade on chest & hands, 6 secs on, flash, fade off  - button 5
	for w5=0 to 500   ;fade to full on
        pwmout B.2,255,w5
        pause 5
    next
    pause 6000  ;pause 6 seconds
    pwmout 2, 255,1023:pause 140 ;briefly flash full brightness
    for w5=500 to 0 step -1  ;fade to full off
        pwmout B.2,255,w5
        pause 5
    next
    	pwmout B.2,0,0
    goto main
    
 swoffall:  ;switch off all lights
 	low B.2
 	low B.4
 	goto main
I'll test it and make sure it continues working but hopefully it's now error free.
 
Top