Shop OBEX P1 Docs P2 Docs Learn Events
do something once inside repeat — Parallax Forums

do something once inside repeat

xanaduxanadu Posts: 3,347
edited 2012-03-04 13:03 in Propeller 1
I have this code using the LCD object and it's monitoring pin 1 of the prop. Having the LCD output in the monitor loop constantly refreshes the display. How would I make this only change the display if the pin input changes instead of refreshing each loop?

 repeat
   
   if ina[1] == 1 
     
     lcd.backlight(true)
     lcd.clrln(1)
     lcd.gotoxy(0, 1)
     lcd.str(string("Off"))     

   if ina[1] == 0 
     
     lcd.backlight(true)
      lcd.clrln(1)
      lcd.gotoxy(0, 1)
      lcd.str(string("On"))     
                               

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-03-04 12:04
    xanadu wrote: »
    I have this code using the LCD object and it's monitoring pin 1 of the prop. Having the LCD output in the monitor loop constantly refreshes the display. How would I make this only change the display if the pin input changes instead of refreshing each loop?

    You need to keep track of the previous state of the pin.
    [COLOR=#ff0000]previousState := 2[/COLOR]  ' we want and invalid state to start with
      repeat
       
       if ina[1] == 1 and [COLOR=#ff0000]previousState == 0
    [/COLOR]    
         lcd.backlight(true)
         lcd.clrln(1)
         lcd.gotoxy(0, 1)
         lcd.str(string("Off"))     
         [COLOR=#ff0000]previousState := 1
    [/COLOR]     
       if ina[1] == 0 and [COLOR=#ff0000]previousState == 1
    
    [/COLOR]    
         lcd.backlight(true)
          lcd.clrln(1)
          lcd.gotoxy(0, 1)
          lcd.str(string("On"))
          [COLOR=#ff0000]previousState := 0[/COLOR]
    
  • xanaduxanadu Posts: 3,347
    edited 2012-03-04 12:17
    Excellent, thank you.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-03-04 12:37
    To add to Duane's comment, as a general rule, you do not want to make separate, non-independent sections of your code dependent on separate readings of a port. Although it's probably not critical in your code, such a practice can lead to a race condition. In your code, you could assign the value of ina[1] to a variable, say newState at the beginning of your repeat loop, then use newState in your if clauses. You can also use this to advantage to eliminate redundant code:
      previousState := 2  ' we want an invalid state to start with
      repeat
        if ((newState := ina[1]) <> previousState)
          lcd.backlight(true)
          lcd.clrln(1)
          lcd.gotoxy(0, 1)
          if (newState)
            lcd.str(string("Off"))     
          else
            lcd.str(string("On"))
          previousState := newState
    

    -Phil
  • StefanL38StefanL38 Posts: 2,292
    edited 2012-03-04 12:53
    @xanadu: OT but I have to post this: really great avatar! ROFLOL
    best regards Stefan
  • xanaduxanadu Posts: 3,347
    edited 2012-03-04 13:03
    StefanL38 wrote: »
    OT but I have to post this: really great avatar!
    best regards Stefan

    Thanks but the avatar was not my idea, a knock off version if you will hehe.

    @PhiPi - Great input, I see I would need to do that for my code and will, thanks.
Sign In or Register to comment.