Shop OBEX P1 Docs P2 Docs Learn Events
Programming HELP!! — Parallax Forums

Programming HELP!!

edited 2011-07-23 12:55 in General Discussion
Heres the code I have so far: with an LED on pin[0] and a button on pin[17]

Pub control

Dira[0] := 1
Dira[16] := 0
Dira[17] := 0

repeat

if ina[16]
outa[0] := 1
ina[16] := 0
waitcnt(clkfreq * 5 + cnt)
outa[0] := 0

This part of the code does exactly what I would like it to do. Where I am having trouble is I would also like for when Ina[17] receives HI from button to stop everything immediately and return to the beginning. I am very new at all of this and am having a blast learning.... some gentle shoves into the right direction would be greatly appreciated and corrections to however I posted anything wrong here are absolutely welcome.....like i said.... HELP :)

Things I have already tried, but prolly executed wrongly are:

an additional "if" statement saying

if ina[17]
Outa[0] :=0

A "next" statement with another repeat loop that turns outa[0] :=0

A "quit" statement that responds to ina[17] button pressed.

waitpeq


Right now I am using an LED in place of what I hope to be a solar/battery powered FAN in the future. the current code that currently works properly will run that fan for 2hours (code says 5 sec for testing purpose) after the solar source is lost ( ina[16] ). what I would like ina[17] to do is sense temperature. If the temp is fine and is sending a signal to ina[17] but solar is still present on ina[16], then shut everything off and restart once solar is sensed on ina[16] again. I got this to partially work but it would still wait for the "waitcnt" to finish before clearing outa[0]

I would also like to add an additional sensor for battery voltage on ina[18] that will stop all operations immediately, no matter what code is left.

Comments

  • BeanBean Posts: 8,129
    edited 2011-07-22 15:16
    Okay, first you need to get your code formatted. Put the tag [ code] before your code and [ /code] after to make it look like this:
    Pub control
    
      Dira[0] := 1
      Dira[16] := 0
      Dira[17] := 0
    
      repeat
    
        if ina[16]
          outa[0] := 1
          ina[16] := 0
          waitcnt(clkfreq * 5 + cnt)
          outa[0] := 0
    

    You have pin 16 set as an input, so I'm not sure what you are trying to do with the "ina[16] :=0" line ???

    To make it end when pin 17 goes high you'll need to make a loop to check pin 17 instead of waiting the whole 5 seconds at one time.

    Something like: (warning untested code)
    Pub control | temp
    
      Dira[0] := 1
      Dira[16] := 0
      Dira[17] := 0
    
      repeat
    
        if ina[16]
          outa[0] := 1
          temp:=0
          repeat
            waitcnt(clkfreq/100 + cnt) ' 10mSec
            temp++
          until (ina[17]) or (temp == 500)
          outa[0] := 0
    

    I hope this helps. Please let us know.

    P.S. If you post the WHOLE program, it makes it easier for us to test it.
    Bean
  • edited 2011-07-22 16:03
    Thanks for the reply Bean :)

    Here is my code.. hopefully posted correctly this time... here it goes:
    Pub FanControl
    
    Dira[0] := 1
    Dira[16] := 0
    dira[17] := 0
    
    repeat
    
     if ina[16]
         outa[0] := 1
         ina[16] := 0
         waitcnt(clkfreq * 5 + cnt)
         outa[0] := 0
    

    The ina[16] :=0 line starts the waitcnt when the button is released and keeps the fan/ LED on for the specified time. I first entered it without the " := 0 " thinking it should be fine since pin[16] was set as an input but got the message "variable needs an operator". with ":= 0" added it works just as desired, on that half of it any ways.

    I tried out your code and couldnt get the desired results, I tried manipulating it to see if I could get it to work but couldnt. I dont really understand the "Temp" commands and could not find it in the Propeller Quick Reference.

    About the P.S.: This is all the code I have so far, and would work great if I could just get pin[17] to turn pin[0] low even though pin[16] is still trying to keep it at high. When I actually have everything hooked up, and since the fan is 12v, I will be using a MOFSET with pin[0] at the gate.

    All other code I have tried has been like walking around in the dark desperately feeling around for a light switch. Seems like that kind of code would be useless, besides, because it did not work I did not save it.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-07-22 17:07
    You can't do INA[ 16 ] := 0. INA (like CNT) is a read-only register. What you're doing is setting bit 16 of the "shadow RAM" location corresponding to INA which doesn't help you at all here. The hub RAM is actually 512 longs, not 496 longs. Some kinds of "illegal" operations that attempt to change read-only registers actually change the shadow RAM location, but you can't really read it except with some other "illegal" operations.

    Do you mean to write "repeat while ina[ 16 ]" to wait for pin 16 to become zero?
  • edited 2011-07-22 17:21
    I am using a Propeller Education Kit and still working my way trough the projects, so, I may learn about "shadow RAM" and the difference between 512 longs and 496 longs in the future, but, as of now that may as well be Japanese. I am glad you pointed out there is something "wrong" with that current code, which is expected since until 3 weeks ago I had never touched a resistor nor programmed a blinking LED, but do you have any suggestions that could help point me in the proper direction? I hope what you mean by "illegal" is the chip will not allow for it.... Because I am NOT a criminal!
  • BeanBean Posts: 8,129
    edited 2011-07-22 18:42
    Daniel, He means illegal as in the chip will not allow it. I'm afraid Mike got a little carried away with his explaination.

    I know it's really overwelming when you start, the best advise I can give you is to don't take offense of people trying to help you. There is a wide range of personallities on the forums so you have to try not to read things into people's comments.

    Here is how you can help us help you. Post the entire code you are using formatted with code blocks (you did this in you post, so far so good). Tell us EXACTLY what it did or didn't do. "couldn't get desired result" tells us nothing. Did the LED not come on ??? Did it come on for a short time ??? Did it catch on fire ??? We need to know all the details to help.

    As Mike stated I think you want "repeat while ina[16]" if you want the code to wait until pin 16 goes low. "ina[16] := 0" does nothing constructive.

    A saying we use at work might help you "If it was easy...Everyone could do it." Programming is NOT easy. It takes alot of time and you have to stick with it. But it IS very rewarding once you get the hang of it.

    Bean
  • edited 2011-07-22 19:48
    Thanks Bean.... very nice of you. No offense was taken at all by Mr. Greens post, but it wasnt helpful (for me... the noob) either, and I just wanted to be clear that it wasnt, no animosity whatsoever.

    The code I posted (properly this time..yay!) is the only code I have so far. I will do my best this time, from the top, to explain exactly what I am trying to do. Forgive me for trying to be clear about something that is so unclear to me so far. But dont worry... I LOVE programming so far, so I will stick with it.

    Here goes nothing.....

    Elements in my project:

    - 12v fan (represented by an LED on PIN[0]
    - solar panel (PV) (represented by the button on PIN[16]
    - temperature sensor (represented by the button on PIN[17]
    - voltage sensor (prolly going to use a schmitt trigger) represented by button on PIN[18]

    - I would like the fan to operate all day, as soon as the PV is lost (night time) Id like to wait for 2 hours until the fan shuts off (5-10 sec for testing purposes is good).
    - BUT, if its cooler out than the set point I give the temp sensor, Id like that signal stop the fan even if there is still signal from the PV (pin[16])
    - BUTTIF (my new programming language) The voltage from the battery is low, ignore pin 16 and 17, stop all code and shut the fan down.
    - Then of course being able to repeat all this each day.

    Ive been using buttons and the LED to test the logic, id like to get the programming right before I start diving into the MOFSETs and triggers and sensors and wires and more resistors and more diods..... if you are wondering if I know that I am in way over my head on this one the answer is YES, YES i do. I enjoy a good challenge.

    After doing tons and tons of internet searching to find a product already made to do the above with no success, I decided to head over to FRY's electronics to see if I might be able to wire something up myself. After more endless searching, because I had no clue about electronics, I stumbled across the PEK 40-pin DIP. I knew it wasnt going to teach me the foundation of electronics, but I also knew if i could grasp it just a little, I would hit the ground running... or, land on my face. Seems I was wrong and am doing both simultaneously. Either way, I am enjoying it immensely. I bought a book from the shack called Getting Started in Electronics.... its a great book! really has helped me a lot. I still wish I had someone to sit next to and help me out. I could learn so much faster!

    Thanks again for your time and help Bean,

    Daniel
  • GadgetmanGadgetman Posts: 2,436
    edited 2011-07-23 02:09
    Landing on your face is quite normal in this hobby...
    Also happens to the professionals, but they don't like to tell anyone about it...

    I wouldn't call 'writing to an input' for an 'illegal operation' as the program still runs.
    Doing a 'Divide by Zero' though, that's a bit on the bad side. On other platforms, attempting to write to a memory segment that's read-only or belonging to another process may be 'illegal'.

    As far as I see it, we have:
    Working instructions,
    waste of time instrutions(everything from good old 'NOP', to manually clearing registers or flags that doesn't have any influence on the next operation, and anyway will be changed by that next instruction anyway)
    Then there's the 'Undocumented' instructions(feel free to pick up a good book on the Zilog Z80 CPU once and study the list of instructions, including the well known 'undocumented' ones. Some are a waste of time, but others are pretty useful. Unfortunately, we can't use them in commercial systems/programs as they aren't verified OK on the chip by the manufacturer)
    The illogical instructions or also known as 'werd things happen' instructions, such as what you did, by writing to an input. Mostly, these doesn't DO anything, but sometimes... things happen...
    And finally, 'illegal instructions' which brings the house down.

    If there's anything really wrong with your code, it's that there's no comments.
    There's a saying 'Real programmers doesn't comment code. If it was hard to write it should be hard to read'...
    And I can tell you from personal experience that a program that has been left untnded for a month or more will look like just that much gobbledygook when you try to pick up where you left.

    At a minimum, a decent description of what a procedure does at the beginning of it, preferably add a description of what the variables are for, too.
    Remember, comments doesn't add size to the executable, so no need to keep them brief. ;-)
    As you're a beginner, you still have time to make this a good habit.
  • edited 2011-07-23 12:55
    I am starting to understand more and more what this "illegal" stuff is all about. After you guys told me ina[16] :=0 "doesn't do anything" I removed it. Sure enough... works just as fine without it. Hope you guys can see the logic I was so illogically using. That said, I will post the old code, take your advice Gadgetman, and put comments next to it... just as good practice.
     
    Pub fancontrol | illegal 
    
    Dira[0] := 1                               set pin[0] to output
    Dira[16] := 0                              set pin[16] to input
    dira[17] := 0                              set pin[17] to input
    
    repeat                                     repeat connected code indefinitely
    
     if ina[16]                                   when pin[16] goes hi
         outa[0] := 1                           set pin[0] 
         ina[16] := 0                            when pin[16] goes low
         waitcnt(clkfreq * 5 + cnt)          wait 5 seconds
         outa[0] := 0                            clear pin[0]
    

    Now heres my new code.... pretty easy, I know.
    Pub FanControlSimple
    
    Dira[0] := 1                                                       set pin[0] as output
    Dira[16] := 0                                                      set pin[16] as input
    dira[17] := 0                                                       set pin[17] as input
    
       repeat while ina[16]                                         repeat block of code while pin[16] is high
    
         if ina[16]                                                       when pin[16] goes high
           outa[0] := 1                                                 set pin[0]
           waitcnt(clkfreq * 5 + cnt)                               wait 5 seconds 
           outa[0] := 0                                                 then clear pin[0]
    

    I see the value of commenting on your own code. I can imagine how complicating it can get for some of the larger projects. Also, it kind of shows me my level of understanding of what each command really does.

    That said.... feel free to comment on my comments. Those comments are how I understand things so far and would love to be set straight early on if my understanding is incorrect.
Sign In or Register to comment.