Shop OBEX P1 Docs P2 Docs Learn Events
PWM at least... — Parallax Forums

PWM at least...

siljamickesiljamicke Posts: 66
edited 2011-01-25 13:15 in Propeller 1
Ok, at least a got what i wanted, PWM controlled with my darn good looking slider pot! So far, so good! However, i got the rather unintended pitchbend aswell :(

I use RCTIME to check my pot in the background. I call it from a routine that runs in the second cog (yes i am aware that running this simple thing in three cogs are somewhat of a waste, but it's only a simple test...) so that the timing of my music playing cog nr. 1 isn't gone, or so i thought. The position of the pot alters the pitch of the tone. Now is this due to the RCTIME running in cog 3, my homebrew debouncer or something else? I'm lost...
[COLOR="plum"]VAR

  long readerStack[16]
  long potValue
  byte PWM_counter
  byte PWM_restart[/COLOR]

PUB PWMtest

  cognew (ReadPot(24,1,@potValue),@readerStack) 

  [COLOR="green"]'I start this in a separate cog to avoid timing problems.
  'Might this be where i do it all wrong?[/COLOR]

  dira[16..23]~~
  PWM_restart:=255
  
  repeat
   
     PWM_counter:=0   

     outa[23..16]:=%11111111

     repeat
        PWM_counter++ 
     while(PWM_counter < [COLOR="red"]potValue[/COLOR] or PWM_counter > 255)
 
   [COLOR="green"] ' The idea is to have ReadPot update potValue through RCTIME transparently to this cog
    ' so that a recent value is always at hand without disrupting this loop...[/COLOR]
        
     outa[16..23]:=%0000000

     repeat
        PWM_counter++
     while(PWM_counter < PWM_restart) 
     

[COLOR="plum"]OBJ
   rc :"RCTIME"

VAR
  long potRead
  long oldPot
  long potDiff[/COLOR]

PUB ReadPot(pin,state,retAddr)

  rc.start(24,1,@potRead)
repeat
  
   [COLOR="green"]'Debounce the pot value...[/COLOR]
  potDiff:= potRead - oldPot
      
      if(potDiff<0)
        potDiff:=-potDiff
      
      if(potDiff>2)
        oldPot:=potRead
  
  long[retAddr]:=(oldPot >> 2) -25

I have a 220 Ohm resistor connected to the pin (pin24) and a 100 nF cap. and 10K pot, if that has anything to do with anything, which i doubt though...

I would be most grateful for some tips, and general pointers aswell!

Comments

  • siljamickesiljamicke Posts: 66
    edited 2011-01-05 14:22
    Ok, so i noticed that if i change potValue into a number between 0 and 255, it behaves the same, the pitch alters. So basically i'm just being really stupid here... If i could just figure out where :)

    It's late here in Sweden, so maybe if i try again tomorrow.

    Take care!
  • siljamickesiljamicke Posts: 66
    edited 2011-01-05 14:29
    A most stupid typo!

    while(PWM_counter < potValue or PWM_counter > 255)

    should be,

    while(PWM_counter < potValue)

    and the byte bounds checking is done in ReadPot before passing back the value...

    How silly ;) Cheers
  • SapiehaSapieha Posts: 2,964
    edited 2011-01-05 14:42
    Hi siljamicke.

    V
  • siljamickesiljamicke Posts: 66
    edited 2011-01-06 02:46
    Nja, v
  • SapiehaSapieha Posts: 2,964
    edited 2011-01-06 03:30
    Hi siljamicke

    Some of my work

    We have not so much snow but still irritating.


    siljamicke wrote: »
    Nja, v
  • siljamickesiljamicke Posts: 66
    edited 2011-01-06 07:02
    Nice! Can i ask you further along as i progress about pcb-manufacturing? I have some project going on...
  • SapiehaSapieha Posts: 2,964
    edited 2011-01-06 07:09
    Hi siljamicke.

    You can ask as many questions as You need to have all answers

    siljamicke wrote: »
    Nice! Can i ask you further along as i progress about pcb-manufacturing? I have some project going on...
  • Ahle2Ahle2 Posts: 1,179
    edited 2011-01-24 08:51
    Jag undrar just hur m
  • siljamickesiljamicke Posts: 66
    edited 2011-01-24 12:42
    Och en bifr
  • JasonDorieJasonDorie Posts: 1,930
    edited 2011-01-24 13:24
    PWM_counter is a byte, but potValue is a long. The code that says "while( PWM_counter < potValue OR PWM_counter > 255)" cannot function properly if potValue is greater than 255, because PWM_counter will wrap back to zero and never finish.

    PWM_counter is able to hold values larger than 255, then if potValue is larger than 255 the 2nd part of the while statement will execute, running more code, making it slower. If the first condition is satisfied, there's no need to run the 2nd part of the statement.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-24 14:58
    Have you considered using an ADC for the pots instead of an rctime method? I helped a friend with a project that has an X/Y joystick, two pots, two motor current circuits, and a voltage sense circuit. I wrote an assembly driver for the MCP3208 that updates an array in the hub every millisecond. My friend doesn't have to worry about calling a method to read one of the channels as that's happening "in the background" every millisecond; all he has to do is use the array variables as desired.
  • siljamickesiljamicke Posts: 66
    edited 2011-01-25 12:35
    JonnyMac:
    I'll keep that in mind!

    To all:
    I was only playing around with a pot, in my neverending quest to learn more about this black box o' magic!
    However, i wonder about one thing, since i've moved on to new stupid attempts at other things, and had forgotten about my pot-problems (no, not the drug kind), how do i mark a thread as solved? I don't find that button...
  • groggorygroggory Posts: 205
    edited 2011-01-25 13:15
    JonnyMac wrote: »
    Have you considered using an ADC for the pots instead of an rctime method? I helped a friend with a project that has an X/Y joystick, two pots, two motor current circuits, and a voltage sense circuit. I wrote an assembly driver for the MCP3208 that updates an array in the hub every millisecond. My friend doesn't have to worry about calling a method to read one of the channels as that's happening "in the background" every millisecond; all he has to do is use the array variables as desired.

    I am doing something very similar to your friend...if you are feeling generous would you mind posting that code? My PASM is very poor...I need to work on that.
Sign In or Register to comment.