Shop OBEX P1 Docs P2 Docs Learn Events
Spin demo code for the TSL235? — Parallax Forums

Spin demo code for the TSL235?

vanmunchvanmunch Posts: 568
edited 2015-02-05 10:43 in Accessories
Hey everyone, I'm evaluation some different color sensors and I would like to try a TSL235 (?R?), but I haven't been able to find any Spin code for it. http://www.parallax.com/product/604-00084 There is an example for using an Arduino from Parallax, but nothing for the Propeller chip. I think the world is upside down....
http://learn.parallax.com/tsl235r-light-frequency-converter-arduino-demo

I've also checked the Obex and no luck, does anyone have a simple demo that they would be willing to share?

Thanks for your time,

Dave

Comments

  • vanmunchvanmunch Posts: 568
    edited 2015-02-04 14:55
  • JonnyMacJonnyMac Posts: 9,163
    edited 2015-02-04 16:58
    There are many more ways to "skin the cat" as it were when using the Propeller. Do you want to read the frequency periodically, or would you like to devote a cog so that you have constant updates? If the latter, how often? Is once per second enough?
  • vanmunchvanmunch Posts: 568
    edited 2015-02-04 17:41
    JonnyMac wrote: »
    There are many more ways to "skin the cat" as it were when using the Propeller. Do you want to read the frequency periodically, or would you like to devote a cog so that you have constant updates? If the latter, how often? Is once per second enough?

    Hey JonnyMac,

    Thanks for asking.

    The goal is to find the best method and sensor(s) for detecting (individually) red, green, blue, IR, & UV light reflected from an object about 2-3 inches away.
    I've currently been testing a number of different sensors (photo-resistors, photo-resistor diodes, TSL235, etc.) In the end I'll create an array of 5-12 covering an area about 8 inches wide. The refresh rate will need to be at least 10 hz. I had been planning on dedicating a cog for this job.

    BTW I always enjoy reading your articles in N&V.

    Dave
  • JonnyMacJonnyMac Posts: 9,163
    edited 2015-02-04 17:51
    I don't have one of those sensors, but I do know how to program the Propeller. I did a direct port of the Arduino demo in just a few minutes. I set the loop time to 10ms so that the output would be an even fraction (1/100) of the input frequency. Since I don't have one of those sensors I used the Synth object to create a test frequency.
    var
    
      long oldcount 
      long temp 
      long avg 
      long freq 
      long loopcount 
    
    
    pub main | t
    
      setup
    
      t := cnt
      repeat
        waitcnt(t += (10 * MS_001))
        temp := phsa                                                 ' capture current count
        freq := temp - oldcount
        avg := avg + freq
        if (++loopcount == 25)
          term.tx(CR)
          term.dec(avg / 25)
          term.tx(CLREOL)
          avg := 0
          loopcount := 0
        oldcount := temp
    
    
    pub setup
    
    '' Setup IO and objects for application
    
      term.start(RX1, TX1, %0000, 115_200)                           ' start serial for terminal
    
      ctra := (%01010 << 26) | TSL                                   ' setup pin to count positive edges
      frqa := 1
    
      ' for testing only -- remove this line when sensor is connected
    
      synth.synth("B", TSL, 300_000)
    


    The key to my code is using a counter in POSEDGE mode -- this mimics the edge-triggered interrupt of the Arduino. You can easily drop this code into a Spin cog to provide a count/period to your foreground application.
  • JonnyMacJonnyMac Posts: 9,163
    edited 2015-02-04 18:10
    Here's how easy it is to monitor the sensor in the background, getting an update every 100ms (10Hz) -- as you would expect, the result is the input frequency frpm the TSL235 / 10.
    var
    
      long  cycles
    
    
    pub main | t
    
      setup
    
      t := cnt
      repeat
        waitcnt(t += (10 * MS_001))
        term.tx(HOME)
        term.dec(cycles)
        term.tx(CLREOL)
    
    
    pub setup
    
    '' Setup IO and objects for application
    
      term.start(RX1, TX1, %0000, 115_200)                           ' start serial for terminal
    
      cognew(run_tsl235(TSL, @cycles, 100), @tslstack)               ' start TLS monitor cog
      
    
      ' for testing only -- remove this line when sensor is connected
    
      synth.synth("B", TSL, 300_000)
    
    
    
    con
    
      { ----------------------------- }
      {  T S L 2 3 5   M O N I T O R  }
      { ----------------------------- }
    
    
    var
    
      long  tslstack[32]
      
    
    pri run_tsl235(pin, p_result, ms) | new, old, t
    
    '' Monitor TSL235
    '' -- updates long at p_result every ms milliseconds
    
      ms *= clkfreq / 1000                                           ' convert ms to ticks
    
      new := 0
      old := 0
      
      ctra := (%01010 << 26) | pin                                   ' setup pin to count positive edges
      frqa := 1
      phsa := 0
    
      t := cnt                                                       ' sync with system timer
      repeat                                                         ' loop forever
        waitcnt(t += ms)                                             ' wait ms milliseconds
        new := phsa                                                  ' capture current count
        long[p_result] := new - old                                  ' report count to foreground
        old := new                                                   ' save for next cycle
    
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2015-02-05 10:43
    Are you also considering integrated RGB color sensors, such as the TCS3472, TCS3490 or ISL29125? Those use the i2c interface.
Sign In or Register to comment.