Shop OBEX P1 Docs P2 Docs Learn Events
Simple program is erratic? — Parallax Forums

Simple program is erratic?

VAR
byte Button
PUB ButtonLED
dira[15]:= %1
dira[6]:= %0
repeat
if Button== 0
if ina[6]== 0

if Button== 1
if ina[6]== 1
!outa[15]
Here is my program using one push button to turn a LED on and off. Some times it works and some times it douse not? Button is P6. LED is P15.
Thank you for any improvements that anyone might give. Still reading all the forms from start to finish. Lots of information!!! 30 aug 2019(fri)

Comments

  • If this is a SPIN program, could you use the Propeller Tool archive button to produce a zip file and attach that in a post?
  • ErNaErNa Posts: 1,738
    VAR
      byte Button
    PUB ButtonLED
    dira[15]:= %1
    dira[6]:= %0
    repeat
     if Button== 0
       if ina[6]== 0
    
         if Button== 1
           if ina[6]== 1]
             !outa[15]
    

    You have to take care of indentation. If you post code, use the [codx][/codx] tags ( replace codx by code )
  • There are also good examples of how to handle this in Programming and Customizing the Propeller, a book that's available on the Parallax website.
  • You don't have any delays which will make things very unwieldy.
  • It's not clear what you're trying to do. If you only want the LED to remain lit only while the button is pressed then you can do this:
    PUB ButtonLED   'LED on while button pressed
      dira[15]:= 1    
      repeat
        outa[15] := ina[6]
    
    If you want to toggle the LED with each button press you can try this:
    I did not build a circuit to test this code but I'm pretty sure it will work.
    VAR
    byte Button
    
    PUB Toggle_LED  | toggle 'LED toggles on and off with each button press
      dira[15]:= 1                       'LED
      toggle := 0  
      repeat
        If ina[6]                          'Is the button pressed?
          waitcnt(clkfreq / 10 + cnt)      'Debounce button. Let the button settle
          If ina[6]                        'is the button still pressed?
            ++toggle 
            Button := toggle // 2          ' // is modulus symbol. Button should only contain a one or zero  
            outa[15] := Button
            waitcnt(clkfreq / 4 + cnt)     'need time to release button
    
  • Cluso99Cluso99 Posts: 18,066
    ErNa has posted indented code. Using that as a base, there isn't any debouncing and/or delays.
    Also, the variable Button is never changed so it's not necessary.

    When a switch or push button is operated, the contacts will switch on/off intermittently many times over a very short instant of time. So your program needs to take care of this. The simplest way is with a timer. So, try this...
    PUB ButtonLED
      dira[15]:= %1                 ' enable the output to the LED
      dira[6]:= %0                  ' disable the output for the button (not necessary_
    
      repeat
        if ina[6] == 0              ' if the button is pressed
            !outa[15]               ' this toggles the output
            repeat 10_000           ' wait some time for the button to debounce
    

    If you shorten the repeat loop count (which is used a a simple delay) you may see the LED change state when the switch is pushed. This will be erratic since it depends on how many state changes the code sees when you press it.

    BTW I have assumed the switch has a pullup resistor to 3V3 and the switch is to ground. (a value > 1K would work so 10K would be nice).
  • Cluso99Cluso99 Posts: 18,066
    edited 2019-08-30 22:31
    You can also try this (doesn't use the pushbutton) but will output back to your computer and toggle the LED. Then you can try modifying this to output the message and toggle the LED whe the button is pressed.
    CON
    
      _clkmode  = xtal1 + pll16x
      _xinfreq  = 5_000_000         ' 5.00MHz
    
      rxPin  = 31                   'serial
      txPin  = 30
      baud   = 115200
    
    OBJ
      fdx  : "FullDuplexSerial"
    
    PUB Main | baud2
    
    'sequence rgb leds
      outa[15]~~                                            ' initialise pin 15 LED as "1"
      dira[15]~~                                            ' enable pin 15 LED as output
      fdx.start(rxPin,txPin,0,baud)                         ' start serial driver to PC
      '''fdx.tx(0)                                           'cls
    
      repeat
        fdx.str(string("If you can read this, the clock is 80MHz",13))
        waitcnt(clkfreq/1 + cnt)                            ' delay 1s
        !outa[15]                                           ' toggle LED
    
  • I thank all of you for your help. I do not know how to produce a zip file to attach in a post???
  • Here's a screenshot of the archive option. Archive just the project. Name it so you can find it easily. I have a desktop folder called "forum files". You can put your pictures in that folder too. Then click Attach a file which you can see below on the left.
    564 x 696 - 57K
  • If you want to zip a Propeller file study the screenshot below: Name it and save it to place where you can easily find it. Below the comment box on the left click "Attach a file" and follow the instructions.
    Zipping%20projects.PNG
    564 x 696 - 57K
Sign In or Register to comment.