IRIN, PWM, One Button command, lets talk IR....

Bernie_of_CPE

New Member
Hi Guys,

My fifth week with Picaxe electronics and my first excursion into a project design. Please pay attention there will be newb questions at the end.

I am starting to meddle with IRIN. I have an 08M2 and have bread boarded the receiver LED circuit (as per the manual) have managed to understand that data is taken in, manipulated and PWM output is generated according to parameters set up in the program code. This is a large chunk of learning change for me.

Now things are getting exciting as I have taken my first off piste adventure and tried to set up the Infra Red remote system to operate basic functions as follows.

I want to soft start and stop a small motor to protect the delicate (aka cheap) gearbox. So I decided after researching through the forum that I needed to understand what a PWM was and how I could bottle it's ether to work for me. The trick seems to be turning rapid on then off blips in power, the longer the blip remains "on" the faster my motor will turn. I set up an 08M2 on my breadboard and coded a quick test using an LED to substitute for the motor to better see the result:

Main:

for b1=0 to 255 'start a counter to make steps from fully off (0) to fully on (255)
pwmout C.2,255,b1 'pushes the PWM signal through pin C.2 looping through the counter b1 times
next 'runs another b1 counter loop and adds 1 to the count
goto main 'what to do when all 255 loop steps are done

So far so good, I can make it get brighter and reversing the counter direction means I can make it dimmer:

for b1=255 to 0
pwmout C.2,255,b1
next

goto main

So now I want to control the speed at which this all happens so I can be sure that I can select the best option for my motor and it's swinging load

The pause command is perfect for this as I can slow each loop for fine control just before the next loop:

for b1=255 to 0
pwmout c.2,255,b1
pause 1000
next

goto main

To decrease the time taken to start and stop the motor I had a play with the values starting from 10 and ending at 150, decreasing the number of steps in this way speeds up the process overall i.e. the total length of time it takes to soft start (or stop) the motor from rest to full speed (or back again).

So now I decide I want to be a smart guy and do this through an infrared remote, I would need to operate the start and stop as separate functions, a bit more coding, first the IRIN part that gets the 08M2 to look at the IR LED receiver signal, if you don't find anything look again, if you do then check it's value and run either poweron or poweroff. I choose 16 as this is the code for the "up" arrow on the remote, guess what key "17" is?

main:

irin [1000,main],C.3,b0 'look for the IR remote signal it will be at C.3 and load it's value to b0

if b0 = 16 then poweron 'if keystroke is up then soft start the motor

if b0 = 17 then poweroff 'if keystroke is down then soft stop the motor

goto main 'Look again for an IR remote signal

Here are the sequences for starting and stopping:

poweron: 'Soft start the motor
for b1=0 to 255
pwmout C.2,255,b1
pause 20
next
goto main

'poweroff: 'Soft stop the motor
'for b1=255 to 0
'pwmout C.2,255,b1
'pause 20
'next
goto main

I ran this in simulation (first time I ever used it) to see what happens. I will be using this to play test all my programs in future it is a very powerful tool. It did take me a bit of head scratching to work out how to input the control code, solution click in the generic box and change the value to whatever key code you need. It is delightful to see the program step advance to the other routines and you can change the value on the fly to see it's effect.

In my blue sky world I want the red power button to operate the motor start and the motor stop sequences. This is how I would expect my TV remote to operate and I like this functionality.

So chaps, I am stuck at this stage having been a master of the universe for an afternoon and much enjoying my successes but I realise I have just cleared the first foothill peak and looked over the top to the next slightly taller one. I don't really have a firm idea how to do this (perhaps looking at C.2 to see if it is outputting and using a toggle command?)

So I am asking for a solution if anyone can be so kind please?

Bernie.
 

westaust55

Moderator
As a first comment, when you wish to decrement a pointer/counter/index variable used in a FOR...NEXT loop you need the STEP -1 appended, so it will be
FOR b0 = 255 TO 0 STEP -1
:
:
 

westaust55

Moderator
To toggle the output on and off, one option is to use a bit variable flag (note these overlap byte variables so you need to reserve b0 or b1 for bit variables)
Then use the Exclusive-Or function to toggle the bit variable each time the red button is pressed.
An IF...THEN test can be used to ascertain if the output should be on or off and act accordingly.
 

Bernie_of_CPE

New Member
As a first comment, when you wish to decrement a pointer/counter/index variable used in a FOR...NEXT loop you need the STEP -1 appended, so it will be
FOR b0 = 255 TO 0 STEP -1
:
:
Hi Westaust,

I actually do have the code in my running program but had been fiddling with the step size to see what effect it had and omitted it in error. Good spot, thank you.
 
Last edited:

Bernie_of_CPE

New Member
To toggle the output on and off, one option is to use a bit variable flag (note these overlap byte variables so you need to reserve b0 or b1 for bit variables)
Then use the Exclusive-Or function to toggle the bit variable each time the red button is pressed.
An IF...THEN test can be used to ascertain if the output should be on or off and act accordingly.
So I'll need to rename the b0 variable in my code so I can use it elsewhere? I think I remember reading that the range available is quite big. I was hesitating investigating using toggle as I wasn't sure how this would interact with the PWM output. For some reason I just imagined when a pin check was made I would randomly hit a high or low by chance and cause a fault I would never be able to find.

Is there any possibility of a complete sample of code to study? It would be very helpful and this is how I have managed to understand what I know.

Here is what I have written so far, I did add a set of LED light code to turn on the band stand lights eventually I will add flash functions etc. It's amazing what can be done with the smallest chip.

main: 'look for the IR remote signal

irin [1000,main],C.3,b0 'wait for new signal

'Motor
if b0 = 16 then poweron 'if keystroke is up then soft start the motor
if b0 = 17 then poweroff 'if keystroke is down then soft stop the motor

'Lights
if b0 = 19 then ledson 'if keystroke is 19 then leds are on
if b0 = 18 then ledsoff 'if keystroke is 18 then leds are off

goto main 'Look again for an IR remote signal

poweron: 'Soft start the motor
pause 20
for b1=0 to 150 step 5
pwmout C.2,150,b1
pause 20
next

goto main

poweroff: 'Soft stop the motor
pause 20
for b1=150 to 0 step - 5
pwmout C.2,150,b1
pause 20
next

goto main

ledson: 'Turn leds on
high C.1
goto main

ledsoff: 'Turn leds off
low C.1
goto main


Bernie
 
Last edited:
Top