Shop OBEX P1 Docs P2 Docs Learn Events
Look at my code and schematic-comment-before I plug it in. — Parallax Forums

Look at my code and schematic-comment-before I plug it in.

Bill C CooleyBill C Cooley Posts: 18
edited 2014-06-25 15:52 in General Discussion
Hello,

I have written some code that didn't complain until I tried to... refer to, start, call a little subroutine called SafetyFirst. I just don't know the correct syntax for that. (or how to return when done)
But mostly I want to know if I have missed something that will take out one of my components when I power this thing up. That has already happened to me once, I'm Leary now.

The RF portion is still under development and far from done, but the switching to pulse mode and the motor test moves or code or approach

Any comments, hints, cautions, suggestions will be greatly appreciated.

Both code and a schematic are attached.

Bill Cooley

Comments

  • kwinnkwinn Posts: 8,697
    edited 2014-06-22 12:41
    Didn't' see any obvious problems with the schematic or the code but it might be a good idea to substitute a led and resistor for the stepper motor windings for testing.
  • Bill C CooleyBill C Cooley Posts: 18
    edited 2014-06-22 14:03
    Kwinn,
    Thanks for looking at my code. Could I direct your attention to the words SafetyFirst, currently apostrophed out, near the beginning of the main program. That is an attempt to run the 'PRI SafetyFirst', but it is the wrong syntax, or worse, not done that way at all.
    Could you show me how to correctly go to that sub routine and return to the main program when done.
    Thanks in advance for your efforts Kwinn.
  • kwinnkwinn Posts: 8,697
    edited 2014-06-22 20:10
    Right now the program goes to SafetyFirst and loops there forever. If you want the subroutine to return if the case is closed you need to use an "if then else" or "repeat until/while" where it loops on the if as long as the case is open and returns when it is closed. Is this what you want it to do?
  • kwinnkwinn Posts: 8,697
    edited 2014-06-22 20:14
    BTW, if a propeller pin is receiving a signal from a 5V device you need to have a minimum of a 3.3K resistor between the prop pin and the 5V device.
  • kwinnkwinn Posts: 8,697
    edited 2014-06-22 21:11
    Another BTW, some of your other repeat loops will also loop forever once they are entered.

    repeat 'Endless Loop Waiting for Radio Input

    PRI JogForward

    repeat

    PRI JogReverse

    repeat

    They need a condition that exits the repeat loop. See repeat until, repeat while, and repeat from/to/step in the propeller manual.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-06-22 21:29
    kwinn wrote: »
    BTW, if a propeller pin is receiving a signal from a 5V device you need to have a minimum of a 3.3K resistor between the prop pin and the 5V device.

    +1 on this.

    I know when the Propeller first came out, resistor values as low as 1K were said to be okay when used with 5V devices but apparently you really need about 3K to be safe. The current through the clamping diodes is supposed to be limited to 0.5mA.

    One trivial thing I noticed was the identical resistor values on the RGB LED. Since the drop across the red LED is likely less than the blue and green LEDs, it's common practice to use a high resistor value on the red LED. This just makes it a bit easier to get a nice white color but this can be adjusted with software so it's hardly worth mentioning and I wouldn't bother changing it unless it's really easy to do so.
  • Bill C CooleyBill C Cooley Posts: 18
    edited 2014-06-23 09:46
    Yes exactly, I modified it to this

    PUB (I moved it up to PUB)

    repeat 'Endless Loop Waiting for Case Open P6 to go High
    if ina[6]<>0 'If Pin 6 goes High (case open) then
    serial[0].str(string("{S}")) 'Change to Pulse Mode (effectively disabling button Inputs)
    OutA[17] :=0 'Blue LED On in Jog Mode / Safe Mode / Case Open
    OutA[16] :=1 'Green LED Off in Jog Mode
    OutA[15] :=1 'Red LED Off in Jog Mode
    else 'When case closes then
    serial[0].str(string("{R}")) 'Return to Serial Mode
    OutA[17] :=1 'Blue LED Off
    OutA[16] :=0 'Green LED On Returning to normal mode ]

    On one hand I want it to always be on the lookout for the case to open or already open on power up, but send the {R} only when needing to leave {S} or as [6] goes to back to 0. Will it behave that way now? ( I don't think it's right yet)

    I guess it's obvious, this is my first attempt at spin code, I'm very grateful for your comments and assistance. I think I need binoculars to see beginner from here.
    Bill
  • Bill C CooleyBill C Cooley Posts: 18
    edited 2014-06-23 18:00
    Duane,

    I did see that the values were different some where in some ones code. What are the good resistances, at least for 3.3v? I am changing the 2.2Ks for 3.3k.

    Hey Thanks again, I'm so new to any code writing & microcontrollers that each of these tips is helping a lot. When kwinn told me you must provide an exit "see repeat while" in the manual, I'm ashamed to say its the first time I'd ever seen the manual, That will help a ton. I couldn't find the !outa command, but in use it looks it toggles the state ea visit, but both high and low would have the same wait time.

    Bill
  • Cluso99Cluso99 Posts: 18,069
    edited 2014-06-23 21:28
    Bill,
    If you want post a snippet of code, use the code tags <code> and </code> where the "<" and ">" are square brackets. This keeps the indentation correct.
  • frank freedmanfrank freedman Posts: 1,983
    edited 2014-06-23 23:38
    You should consider moving the fuse to the +5V line ahead of the switch. Most of the component failures I have seen tend to be short circuits when they go. Typically to ground/return. May not be an issue here, but good practice to fuse the source just in case there is another return path through case. common ground etc.
  • JonnyMacJonnyMac Posts: 9,188
    edited 2014-06-24 11:47
    I agree that you should change your current limiters to 3.3K (2.8K is the calculated minimum, based on the data sheet).

    I would strongly suggest that you use named constants for pins -- it is very easy to forget what a pin does, and by using "magic numbers" in all your dira and outa instructions, you're opening up a big opportunity for a bug and confusing others looking at your code. Likewise with magic numbers in waitcnt instructions.

    Most of the code I write is for public consumption hence I tend to be very conservative and verbose in my approach. I suggest you start there, too. Not that you were wondering, "What would JonnyMac do?" -- but I moved your code into my standard template style. This is my style and, ultimately, you will develop your own. What that style should avoid, however, is magic numbers in the code. Note that by using named pins and a few constants, the code can become somewhat self-commenting.

    Could you show me how to correctly go to that sub routine and return to the main program when done.

    My version of your program allows safety_first, jog_forward, and jog_reverse to exit.
  • Bill C CooleyBill C Cooley Posts: 18
    edited 2014-06-25 00:07
    Jon,

    WOW, WOW, Wow. Thanks so very much. That was a big effort. Well, as you could no doubt tell, I had learned only 3 or 4 things and tried to start making my gizmo. As an electrician, I've made a lot of gizmos, but usually bigger like retractable dark room, using limit switches, relays. This is my first venture into microcontrollers and code. I didn't realize how first grade my try was. It's going take me quite some time to absorb how your code works, all the cool tricks, the naming convention. I am blown away, I can barley read it. Again I can't thank you enough. Boy, That hill is always much farther away once you start walking it.

    Really just Great!
    Bill
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-06-25 15:52
    I did see that the values were different some where in some ones code. What are the good resistances, at least for 3.3v?

    Sorry to say, I don't know. In my RGB arrays I use 100 ohms for the green and blue LEDs and 150 ohms on the red. The voltage on the RGB arrays is closer to 5V which makes it easier to control the current through the green and blue LEDs. Since the green and blue LEDs have a forward voltage pretty close to the Propeller's 3.3V output, it's hard to know how much of the voltage drop will be across the resistors. I personally don't think you need to worry about this. I'd just leave the resistor values as they are.
Sign In or Register to comment.