Shop OBEX P1 Docs P2 Docs Learn Events
Advice for monitoring state of 4 leds? — Parallax Forums

Advice for monitoring state of 4 leds?

stampinatorstampinator Posts: 18
edited 2013-07-29 22:21 in BASIC Stamp
Hi everyone,


I have a circuit with 4 LEDs that have various modes of flashing that they do. I've got the basic stamp 1 wired up to monitor them, and am trying to come up with the best way to detect when they enter a specific mode.


The LEDs do the following:
* individually blink at a rate of ~125ms
* individually can become solid, once all 4 are solid, they all reset to blinking at ~125ms
* alternate on state amongst themselves: 1..., .2.., ..3., ...4, (repeat)
* blink slowly and increase in speed over a 5 second period (1000ms all the way down to 0ms)
* turn off for 3 seconds, and then resume whatever they were doing previously

What I am trying to do is detect when at least one LED has become solid, and then all 4 leds are blinking at ~125ms. And this is where I am a little unsure of how I should tackle this. In order to ensure I am seeing the correct state, I basically need to have the basic stamp look for a LOW over all 4 LEDs, and then look for a HIGH over all 4 leds in under a 125ms period, and I probably need to repeat this like 3 times to verify it's the 125ms state and not one of the other states.


Does anyone have any suggestions on how I should proceed with this?


Thanks.

Comments

  • SapphireSapphire Posts: 496
    edited 2013-07-28 19:23
    I'm curious what these LEDs are on. The only thing that blinks like that is my router! How do you want to indicate when the pattern you want to detect has occurred? What are you using this for?

    Since you are using a BS1 and its programming is limited, this will be challenging but not impossible. I wrote a small test program that catches when all 4 LEDs are steady and releases when they start blinking again, but it will take some changes to catch the first LED going steady.
  • stampinatorstampinator Posts: 18
    edited 2013-07-28 20:26
    > I'm curious what these LEDs are on. The only thing that blinks like that is my router!

    They are on the playfield of a pinball machine.

    >
    How do you want to indicate when the pattern you want to detect has occurred? What are you using this for?

    Basically I will be energizing a solenoid when the pattern is detected...

    After more thought, I think I just need to experiment with how many cycles fit into a 125ms period, and once I have that close then I can just use a counter to determine if I should be looking for a low vs. high, repeat that 2-3x and then theoretically would be good to go.

    THEORETICALLY!!!!!!!!
  • stampinatorstampinator Posts: 18
    edited 2013-07-28 20:39
    And hmmm.. I just did a simple test:
    symbol counter w0 = 0
    main:
    counter = counter + 1
    debug counter
    goto main

    and the text output for counter happens incredibly slowly.. slower than the rate the leds I want to track are flashing....... So this tells me that debugging to the console is not an accurate way to figure out my timing issues.. Looks like I am going to have to start blinking an LED of my own to figure this out.
  • SapphireSapphire Posts: 496
    edited 2013-07-28 20:43
    Well, on the BS1, using DEBUG will slow down the process so much you won't get accurate readings. It's not like the DEBUG on the BS2. You'll have to use an output pin to indicate if the state was caught or not.

    Should have something for you soon...
  • ercoerco Posts: 20,257
    edited 2013-07-28 21:09
    Sapphire wrote: »
    Well, on the BS1, using DEBUG will slow down the process so much you won't get accurate readings. It's not like the DEBUG on the BS2.

    IIRC, DEBUG slows down the BS2 significantly as well.
  • SapphireSapphire Posts: 496
    edited 2013-07-28 21:25
    erco,

    True, but not nearly as much as the BS1. The DEBUG in the BS1 sends the entire RAM memory to the PC at 4800 bps, which can really knock timing off. At least in the BS2 it only sends what you specify in the DEBUG statement when it is encountered at 9600 bps.

    http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/vol7/col/NV131.pdf
  • SapphireSapphire Posts: 496
    edited 2013-07-28 21:44
    Give this a try. It's set up to monitor 4 inputs on P0-P3, and output a HIGH on P7 when the condition you describe is met (namely one or more of the LEDs is on for a specified time). The output will go LOW once all the LEDs start blinking again.

    There are a few parameters you can set. From the top, DELAY specifies how long to wait between loops. Not knowing for sure how fast your inputs are flashing, this may be too high. Adjust up or down to suit. The next, LIMIT, is how many passes through the test the LED has to be ON before the alarm is set. If you lower DELAY you probably should raise LIMIT. You want DELAY low enough to catch the quickest flashes, and LIMIT to be high enough at that DELAY setting to ignore the longest flashes. ALARM is just the pin you want to make HIGH when the event occurs. There are some hard-coded values in the LOOKUP statement that assume the input pins are P0-P3. They are also assumed to go HIGH when the LED is ON. If you wired it up differently, let me know because changes will be necessary.

    Disclaimer: Since I don't have a nifty pinball machine like you do, I couldn't fully test this. But I think it will work.
  • stampinatorstampinator Posts: 18
    edited 2013-07-28 22:41
    Sapphire, thanks-- I just downloaded your program and will try it out.

    In the meantime, I was working on my own experimentation and am TOTALLY confused........ check out the attached program. I am simply repeatedly looking to see if I receive a low and a high multiple times within a period of time. I have a test LED rigged up to go off when the "fire_coil" method is called... So my plan was to test this out by tapping pin4 with 5v rapidly and see the led go off...

    However, just by applying power, the led goes off.. And so I threw in some debug statements to tell what was going on, and it appears that the "increment_high" method is being called regardless of the fact that there is no 5v going to it!!!!!! WTF?!?!!

    The debugger shows:
    COUNTER = 1HIGH_COUNT = 1
    COUNTER = 2
    LOW_COUNT = 1
    COUNTER = 3
    LOW_COUNT = 2
    COUNTER = 4
    LOW_COUNT = 3
    COUNTER = 5
    LOW_COUNT = 4
    COUNTER = 6
    LOW_COUNT = 5
    COUNTER = 7
    HIGH_COUNT = 2
    COUNTER = 8
    LOW_COUNT = 6
    COUNTER = 9
    LOW_COUNT = 7
    COUNTER = 10

    ?!??!!??!?!!?!?

    Why is this happening? Why in the world is high_count incrementing? Seems like total nonsense.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-07-29 01:20
    Why is this happening? Why in the world is high_count incrementing? Seems like total nonsense.

    Do you have a pull down resistor on pin 4? I'm betting not.

    In post #3 of my index (see signature) there's a link to a tutorial on how to use code tags. Code tags preserve indentation and make code easier to read.
    scanner:  counter = counter + 1
      DEBUG counter
      IF counter > 200 THEN main
      IF low_count > 2 AND high_count > 2 AND counter = 200 THEN fire_coil
      IF PIN4 = 1 THEN increment_high
     ' not needed IF PIN4 = 0 THEN increment_low
     ' not needed GOTO scanner
    
    
    increment_low:
      low_count = low_count + 1
      DEBUG low_count
      GOTO scanner
    

    I commented out two lines of code which I'm pretty sure aren't needed. Since if the pin isn't high, you can assume it's low and the program flow will continue on to "increment_low" without any intervention required.

    You're just testing to see if pin 4 isn't continuously high or low right? You're checking to make sure not all 200 loops where all high or all low?

    Does the BS1 really dump the entire RAM with each debug statement?

    Edit: I just looked at Sapphire's code. It has an example of letting the code flow after an "if" statement who's condition isn't met.
    FOR index = 0 TO 3                                  ' test four bits
      LOOKUP index,($1,$2,$4,$8),bit                    ' convert index to bit value
      test = current & bit                              ' see if the input is on
      IF test = 0 THEN Off                              ' input is off
      On: BRANCH index,(On0,On1,On2,On3)                ' select counter based on index
      On3: count3 = count3 + 1 MAX LIMIT : GOTO Done    ' increment count3
      On2: count2 = count2 + 1 MAX LIMIT : GOTO Done    ' increment count2
      On1: count1 = count1 + 1 MAX LIMIT : GOTO Done    ' increment count1
      On0: count0 = count0 + 1 MAX LIMIT : GOTO Done    ' increment count0
    

    The code is only directed to the "Off" section. The "On" section is reached by default.

    (Sorry, these comments about programming style don't really help in your efforts to monitor LEDs.)
  • stampinatorstampinator Posts: 18
    edited 2013-07-29 07:45
    Duane Degn wrote: »
    Do you have a pull down resistor on pin 4? I'm betting not.

    I don't... Do I need one?
    Duane Degn wrote: »
    You're just testing to see if pin 4 isn't continuously high or low right? You're checking to make sure not all 200 loops where all high or all low?


    I was simply trying to test that if a high AND a low on pin 4 is detected multiple times within 200 loops, then light up my test LED to prove that it's working... It makes absolutely no sense to me why high_count increments, regardless of the lines that you commented out. It should only be incremented if it actually sees a high, and there is no 5v being applied to the pin, so the fact that the output shows high_count going up, is total nonsense and makes me question how anything is going to work at all.
    COUNTER = 1
    HIGH_COUNT = 1

    COUNTER = 2
    LOW_COUNT = 1
    COUNTER = 3
    LOW_COUNT = 2
    COUNTER = 4
    LOW_COUNT = 3
    COUNTER = 5
    LOW_COUNT = 4
    COUNTER = 6
    LOW_COUNT = 5
    COUNTER = 7
    HIGH_COUNT = 2
    COUNTER = 8
    LOW_COUNT = 6
    COUNTER = 9
    LOW_COUNT = 7
    COUNTER = 10

  • SapphireSapphire Posts: 496
    edited 2013-07-29 07:59
    How about trying a simple program to make sure your pin is working and you've connected the right one?
    Start:
    DEBUG PIN4
    GOTO Start
    

    You'll see either "PIN4 = 1" or "PIN4 = 0" and that will let you know if PIN4 is working.

    You could also test your output with this to drive PIN6 with whatever is on PIN4:
    OUTPUT 6
    Start:
    PIN6 = PIN4
    GOTO Start
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-07-29 08:15
    I don't... Do I need one?

    Yes.
    makes me question how anything is going to work at all.

    By not letting your input pins float (not being pulled high or low). If look in the various Stamp manuals, they show how to wire a button. They'll always have the pin pulled high or low with a resistor so when the button isn't pressed the pin is in a well defined state. The button press will then switch the state of the pin. I think it would be a good idea to wire up a button to your input pin for testing.
  • stampinatorstampinator Posts: 18
    edited 2013-07-29 08:25
    Sapphire,

    Your basic stamp code is a little different than I've seen in the manual, so I am a wondering how some of this works.. Would you mind explaining some of your code to me?
    current = PINS & $F ' read inputs, mask pins 4-7 to only check pins 0-3
    

    How is this working exactly? what is PINS? a constant representing p0-p7 ? $F is 15, correct? I get that you are anding a bit representation, but I am a little confused-- and also there is no where in the program designating which pins are inputs and which are outputs-- which I thought you have to do?
    LOOKUP index,($1,$2,$4,$8),bit                    ' convert index to bit value
    

    is this doing:

    bits = [1,2,4,8]
    bit = bits[index]

    as in, assign bit to one of the indices in the bits array through the index variable?
      On3: count3 = count3 + 1 MAX LIMIT : GOTO Done    ' increment count3
    

    MAX means it will not increment count3 if it is higher than the limit variable? Does it goto Done regardless if count3 <=> limit ? Or is going to done only after limit has been reached?

    thanks!
  • SapphireSapphire Posts: 496
    edited 2013-07-29 09:56
    PINS is a variable representing p0-p7, not a constant. All pins are input by default, that's why I don't need to explictly set p0-p3 as inputs. Using the HIGH and LOW commands force a pin to be an output, so I also don't need to explictly make ALARM an output. You could do so if you want, but it's not required. Also LET is not required.

    $F is 15 in hex notation. The & (AND) operator takes the value of PINS and returns only the lowest 4 bits. The upper 4 bits are zero. Turns out this operation isn't necessary, I had it in there in the first version which was doing a differrent kind of check that needed to ignore the high bits. With the current program it could be simplified as:
    current = PINS      ' read inputs
    

    Yes, bit will be set to 1,2,4,8 for index of 0,1,2,3 respectively.

    Yes, MAX limits the count to LIMIT. GOTO always goes to Done regardless of the count.
  • stampinatorstampinator Posts: 18
    edited 2013-07-29 13:47
    Duane Degn wrote: »
    Yes.



    By not letting your input pins float (not being pulled high or low). If look in the various Stamp manuals, they show how to wire a button. They'll always have the pin pulled high or low with a resistor so when the button isn't pressed the pin is in a well defined state. The button press will then switch the state of the pin. I think it would be a good idea to wire up a button to your input pin for testing.

    How big of a resistor do I need to qualify as a "pull down?"
  • SapphireSapphire Posts: 496
    edited 2013-07-29 13:58
    Big enough not to load down the input during normal operation. I use 10k ohm, but as low as 1k ohm should work.

    Also, post a schematic of how you wired in the LEDs from the pinball machine. You may not need pulldown resistors depending on where you are getting the inputs from.
  • stampinatorstampinator Posts: 18
    edited 2013-07-29 20:26
    Sapphire wrote: »
    Also, post a schematic of how you wired in the LEDs from the pinball machine. You may not need pulldown resistors depending on where you are getting the inputs from.

    Here are the schematics...

    The LEDs I am hooking into are 17-20. My plan was to just go from the BS1 pins directly to where the diodes are in the circuit.
    1024 x 606 - 111K
    516 x 1238 - 334K
  • SapphireSapphire Posts: 496
    edited 2013-07-29 20:50
    The first drawing appears to be switch inputs only. There are a lot of switches! The second shows lamp replacement but not the wiring for the 4 LEDs you want to monitor.

    Can you find a drawing like the first one for the lights? You'll need that to determine how they are driven, what the voltage is on the LEDs and where you are going to tap into the circut. And you need to find out if they are multiplexed as that could affect how they are monitored.
  • stampinatorstampinator Posts: 18
    edited 2013-07-29 21:28
    Can you find a drawing like the first one for the lights?

    Oops.. sorry, yeah I attached the wrong one! Here is the one with the lamps...

    So they read approx ~6.6v when they are on solid... And 1.9v when they are blinking--- Which is my volt meter getting an average of the rapid on/off I guess..

  • SapphireSapphire Posts: 496
    edited 2013-07-29 21:49
    Okay, this is as I suspected. The lights are multiplexed, which means even when they are on steady to your eye, they are actually flashing at a very fast rate. The controller is strobing them to give the appearance of being on, off or blinking. This is typical for devices with a large number of lights. Even a digital clock strobes the 7-segments that make up each digit.

    What does this mean? Well for one, directly connecting them to the BS1 won't work, because the stamp could read the input between strobes and think they are off when to your eye they are on steady! Also, 6.6v is too high for the stamp, and it's actually probably a bit higher than that due to the multiplexing. You will need to build an interface circuit that taps the 4 lights you want, buffers them, reduces the voltage, and extends the on-time to cover the strobe so the BS1 sees a clean on or off condition. This could be done with hardware (opto-isolators, inverters, resistors and capacitors). One thing your going to need to find out is the multiplex rate. Do you have an oscilloscope?
  • stampinatorstampinator Posts: 18
    edited 2013-07-29 22:03
    The fact that the LEDs reads 6.6v, that means that's probably too much voltage for the basic stamp to receive as an input, correct? So I assume I would want to split the wire at the cathode of the LED, and have one path go where it's currently going, and the other path go to the basic stamp pins... Does that sound correct?
  • stampinatorstampinator Posts: 18
    edited 2013-07-29 22:05
    ah looks like we were posting at the same time... I do not have an oscilloscope, but vector graphics is my favorite thing in the world, so I should own one! Do you recommend a particular model? Also I see you are in Arcadia.. I am in Westwood, just outside of Santa Monica.
  • SapphireSapphire Posts: 496
    edited 2013-07-29 22:21
    Yeah, that happens some time. Nearly any scope will do, there are some that connect to your PC that work well. I have one of those and a B&K Precision scope. In any case, whatever you can get to measure the strobe frequency would help. You should tap the annode side of the lamp/LED as that side would have the drive voltage. The cathode side is at ground potential when the light is on, and high when the light is off. I suggested the opto-isolator which would go across the lamp/LED and would turn on with it. Are the four LEDs in the same column by any chance? What are their numbers on the drawing?

    And yes, I'm familiar with Westwood. I grew up in Westchester.
Sign In or Register to comment.