# ScrollPhat HD query

**URL:** <https://forums.pimoroni.com/t/scrollphat-hd-query/8848>\
**Category:** Support\
**Created:** [October 6, 2018, 11:23am UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848 "2018-10-06T11:23:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard238](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard238](https://forums.pimoroni.com/u/Richard238)\
**Post date:** [October 6, 2018, 11:23am UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/1 "2018-10-06T11:23:35Z")

</div>

This code prints the temp (from envirophat) to the terminal every second.  
ScrollPhatHD scrolls only very slowly.

I’m not sure where the scrollphat.clear needs to go, or how to control the scroll speed as it’s much slower than what’s being printed on the terminal.

All help, much appreciated.  
Thank you.

```auto
import time
import sys
from envirophat import light, weather, motion, analog, leds
import scrollphathd

try:
    while True:
        temperature = weather.temperature()
	temp = " The temp is " + str(weather.temperature())
	

	print str(temp)
	
        scrollphathd.write_string(
        temp, 
        brightness=0.1
        )

#! Show the buffer
	scrollphathd.show()	
#! Scroll the buffer content
	scrollphathd.scroll()

# Where does .clear go?!	        
# scrollphathd.clear()

# Why does terminal print every second, but phat is much slower?
        time.sleep(1)
        

except KeyboardInterrupt:
    pass

```

---

<div class="post-metadata">

**Author:** ![anon83280986](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@anon83280986](https://forums.pimoroni.com/u/anon83280986)\
**Post date:** [October 6, 2018, 12:02pm UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/2 "2018-10-06T12:02:16Z")

</div>

Here is what I use to scroll the time on a ScrollPhat.

Not a HD version, but the principle should be the same.

```
#!/usr/bin/env python

import sys
import time

import scrollphat

brightness = scrollphat.set_brightness(5)
             
while True:
    try:
        scrollphat.write_string(time.strftime("%H:%M "))
        scrollphat.scroll(1)
        time.sleep(0.5)

    except KeyboardInterrupt:
        scrollphat.clear()
        sys.exit(-1)

```

Adjust the:

```
    scrollphat.scroll(1)
    time.sleep(0.5)

```

Until you get the speed you need.

Cheers,

Simon.

---

<div class="post-metadata">

**Author:** ![Richard238](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard238](https://forums.pimoroni.com/u/Richard238)\
**Post date:** [October 6, 2018, 12:57pm UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/3 "2018-10-06T12:57:21Z")

</div>

I’ve got that OK, tested with the [Christmas countdown example project.](https://learn.pimoroni.com/tutorial/tanya/santabot-xmas-timer)

I can’t work out how to scroll a reading from envirophat.

Thanks.

---

<div class="post-metadata">

**Author:** ![gadgetoid](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/gadgetoid/32/13_2.png) [@gadgetoid](https://forums.pimoroni.com/u/gadgetoid)\
**Post date:** [October 8, 2018, 9:06am UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/4 "2018-10-08T09:06:05Z")

</div>

Look into `string.format` - [https://docs.python.org/3.1/library/string.html#formatspec](https://docs.python.org/3.1/library/string.html#formatspec)

The specification language is fairly opaque but you’ll only generally need a few useful snippets.

A whole number

```auto
>>> "{:03d}".format(100)
'100'
>>>

```

With leading zeros:

```auto
>>> "{:03d}".format(99)
'099'

```

Temperature (or a decimal number with 5 digits and 2 decimal places):

```auto
>>> "{:5.2f}".format(19.22)
'19.22'

```

Pressure (or a decimal number with 7 digits and 2 decimal places):

```auto
>>> "{:7.2f}".format(1020.22)
'1020.22'

```

And you can use these at any point in your text to add your number:

```auto
>>> "The temperature is {:5.2f}C".format(22.15)
'The temperature is 22.15C'

```

And, of course, from a variable too:

```auto
temperature = 19.2524
>>> "The temperature is {:5.2f}C".format(temperature)
'The temperature is 19.25C'

```

---

<div class="post-metadata">

**Author:** ![Richard238](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard238](https://forums.pimoroni.com/u/Richard238)\
**Post date:** [October 8, 2018, 11:59am UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/5 "2018-10-08T11:59:35Z")

</div>

I commented out `scrollphathd.clear()` and now have some scrolling going on.  
Scrolphathd shows “Temp is 20.## CC”  
Where ## is blocky text  
And CC is an offset C on another C

Seems it’s writing on top of itself?

```auto
import time
import sys
from envirophat import light, weather, leds
import scrollphathd

try:
    while True:

#	lux = light.light()
	temperature = weather.temperature()	

#	print light.light()
#	print(" Temp is {:5.2f} C".format(temperature))
	
#	scrollphathd.clear()	
        scrollphathd.write_string(" Temp is {:5.2f} C".format(temperature),
        brightness=0.5
        )

		
		
#! Show the buffer
	scrollphathd.show()	
#! Scroll the buffer content
	scrollphathd.scroll()

# Why does terminal print every second, but phat is much slower?
        time.sleep(0.01)
        

except KeyboardInterrupt:
    pass

```

Using temperature now as it’s easier to change for testing.  
But, EVENTUALLY!, I would like Enviorphat to tell me, via a scrollphathd, the status of an up-n-over garage door.

Heading seems ideal for this purpose.

_But at this rate, with my lack of python understanding, I may as well just go outside and look at the door myself!_

If anyone has a simple dummies guide to linking envirophat & scrollphat, I’d be much appreciative.

Ta.

---

<div class="post-metadata">

**Author:** ![gadgetoid](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/gadgetoid/32/13_2.png) [@gadgetoid](https://forums.pimoroni.com/u/gadgetoid)\
**Post date:** [October 9, 2018, 9:42am UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/6 "2018-10-09T09:42:03Z")

</div>

You have two quite frustratingly opposing problems:

1. When you call `clear` it also resets the scroll position
2. When you write out the temperature without calling `clear` you’re drawing over the top of the existing text- so the values that change will end up the garbled mess that you’re seeing.

Probably the easiest way to overcome this is to rely on the fact `write_string` returns the length, in pixels, of the string it has written, so you can:

1. Write out the string, saving the length
2. Show the string
3. Scroll
4. Clear the area you drew the string
5. Sleep
6. Goto 1

Helpfully there’s a `clear_rect` method you can use in lieu of `clear` to just empty a particular space in your buffer, versus resetting everything. Text is usually 7 pixels high, so:

```auto
text_width = scrollphathd.write_string(...)
# Show, scroll, sleep, etc
scrollphathd.clear_rect(0, 0, text_width, 7)

```

---

<div class="post-metadata">

**Author:** ![Richard238](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard238](https://forums.pimoroni.com/u/Richard238)\
**Post date:** [October 9, 2018, 11:47am UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/7 "2018-10-09T11:47:01Z")

</div>

That makes sence, thank you.  
Alas, python indents have never made sense, I’ve tabbed this line around, but always the same error:

_File “H1.py”, line 25_  
\_ text\_width = scrollphathd.write\_string(" Heading {} ".format(hding)\_  
\_ ^\_  
_IndentationError: unexpected indent_

```auto
import time
import sys
from envirophat import light, weather, leds, motion
import scrollphathd

try:
    while True:

	lux = light.light()
	temperature = weather.temperature()
	hding = motion.heading()

#	print light.light()
#	print(" Temp is {:5.2f} C".format(temperature))
	
#	scrollphathd.clear()	
# scrollphathd.write_string(" Temp {:5.2f} C".format(temperature)
        scrollphathd.write_string(" Heading {} ".format(hding),
# scrollphathd.write_string(" Lux {} ".format(lux),
        brightness=0.1
        )

		text_width = scrollphathd.write_string(" Heading {} ".format(hding)
# Show, scroll, sleep, etc
	

#! Show the buffer
		scrollphathd.show()	
#! Scroll the buffer content
		scrollphathd.scroll()

# Sleep
        time.sleep(0.05)
 
 
		scrollphathd.clear_rect(0, 0, text_width, 7)        

except KeyboardInterrupt:
    pass

```

---

<div class="post-metadata">

**Author:** ![gadgetoid](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/gadgetoid/32/13_2.png) [@gadgetoid](https://forums.pimoroni.com/u/gadgetoid)\
**Post date:** [October 9, 2018, 11:49am UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/8 "2018-10-09T11:49:22Z")

</div>

Alas, Python’s forced indentation-as-syntax definitely has its ups and its downs.

In this case it looks like you might be running into a problem with mixed tabs and spaces in your indents.

From the looks of it, you can fix this by taking the `text_width`, `clear_rect` show, and scroll lines, deleting their indentation and replacing it with 8 spaces.

---

<div class="post-metadata">

**Author:** ![Richard238](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard238](https://forums.pimoroni.com/u/Richard238)\
**Post date:** [October 9, 2018, 12:54pm UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/9 "2018-10-09T12:54:18Z")

</div>

No change from code shown above, other than the suggested 8 spaces on the four lines.  
I do tend to use tab rather than spaces, if that makes any difference?

It now says

```auto
  File "H1.py", line 33
    scrollphathd.scroll()
               ^
SyntaxError: invalid syntax

```

How can:

```auto
    scrollphathd.scroll()
               ^
SyntaxError: invalid syntax

```

Be a syntax error, there’s nothing wrong with it whatsoever?!

---

<div class="post-metadata">

**Author:** ![Richard238](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard238](https://forums.pimoroni.com/u/Richard238)\
**Post date:** [October 12, 2018, 4:14pm UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/10 "2018-10-12T16:14:37Z")

</div>

Anyone?

```auto
import time
import sys
from envirophat import motion
import scrollphathd

try:
    while True:

        hding = motion.heading()

        scrollphathd.write_string(" Heading {} ".format(hding),
        brightness=0.1
        )

        text_width = scrollphathd.write_string(" Heading {} ".format(hding),

#! Show the buffer
        scrollphathd.show()	
#! Scroll the buffer content
        scrollphathd.scroll()

# Sleep
        time.sleep(0.05)
  
        scrollphathd.clear_rect(0, 0, text_width, 7)        

except KeyboardInterrupt:
    pass

```

---

<div class="post-metadata">

**Author:** ![anon83280986](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@anon83280986](https://forums.pimoroni.com/u/anon83280986)\
**Post date:** [October 13, 2018, 8:27am UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/11 "2018-10-13T08:27:33Z")

</div>

It’s an error because:

> scrollphathd.scroll(what?)

The Documentation for these commands is here:  
[http://docs.pimoroni.com/scrollphathd/](http://docs.pimoroni.com/scrollphathd/)

You have to pass an integer into the `scroll()` variable like:

> scrollphathd.scroll(1)

Cheers,

Simon.

---

<div class="post-metadata">

**Author:** ![Richard238](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard238](https://forums.pimoroni.com/u/Richard238)\
**Post date:** [October 14, 2018, 10:05am UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/12 "2018-10-14T10:05:28Z")

</div>

I’ve tried countless figures, positive and negative…  
`scrollphathd.scroll(n)`  
`scrollphathd.scroll(n,n)`  
Always still syntax error.

---

<div class="post-metadata">

**Author:** ![gadgetoid](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/gadgetoid/32/13_2.png) [@gadgetoid](https://forums.pimoroni.com/u/gadgetoid)\
**Post date:** [October 15, 2018, 10:17am UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/13 "2018-10-15T10:17:28Z")

</div>

Usually inexplicable syntax errors like this are triggered by problems on previous lines, and not the actual line in question. Look at your code as a whole, and not just a single line.

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [October 15, 2018, 11:45am UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/14 "2018-10-15T11:45:18Z")

</div>

Syntax especially indents has to be one of the biggest head banging things about Python.  
If you open your code in say idle, then do the check module, it might clue you into where things are going wrong.

---

<div class="post-metadata">

**Author:** ![anon83280986](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@anon83280986](https://forums.pimoroni.com/u/anon83280986)\
**Post date:** [October 15, 2018, 2:02pm UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/15 "2018-10-15T14:02:34Z")

</div>

If the code sample posted above is your actual code, I have a couple of suggestions:

> ```
> scrollphathd.write_string(" Heading {} ".format(hding),
> brightness=0.1
> )
> 
> ```

Probably split this out so the brightness setting is on a separate earlier line.  
(It’s a personal preference, and not strictly needed.)

And,

> ```
> text_width = scrollphathd.write_string(" Heading {} ".format(hding),
> 
> ```

This will never work, because:

- There is a comma at the end of the command.

- There is an unclosed bracket.

As Phil stated earlier, the error probably doesn’t lie with the `scrollphathd.scroll()` but with the preceding lines.

I’ve not got an Enviro pHAT to debug this properly but I managed to get to the point where I was getting IO errors as there was no board communicating with the Pi.

Hope that helps,

Simon.

---

<div class="post-metadata">

**Author:** ![Richard238](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard238](https://forums.pimoroni.com/u/Richard238)\
**Post date:** [October 16, 2018, 11:18am UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/16 "2018-10-16T11:18:16Z")

</div>

Thanks everyone for the help and ideas.  
I’m a bit busy with real work this week, but really appreciate your advice, and will have a go just as soon as I can.

Thank you! :)

---

<div class="post-metadata">

**Author:** ![Richard238](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard238](https://forums.pimoroni.com/u/Richard238)\
**Post date:** [December 31, 2018, 4:10pm UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/17 "2018-12-31T16:10:28Z")

</div>

I’ve had a play with this over the Xmas break.  
I got better results making use of Pimoroni’s own Santa countdown project.

```auto
import time
import sys
from envirophat import light, motion, weather
import scrollphathd
from scrollphathd.fonts import font5x7

str_len = 0
scroll_x = 0

while True:

    hding = motion.heading()
    temperature = weather.temperature()
    lux = light.light()
    rgb = str(light.rgb())[1:-1].replace(' ', '')
        
    scrollphathd.set_brightness(0.2)
    scrollphathd.clear()

str_len = scrollphathd.write_string(" Heading/%i Temp/%.1fC Lux/%i " %(hding, temperature, lux), x=0, y=0, font=font5x7)

    scrollphathd.scroll_to(scroll_x, 0)
    scrollphathd.show()
    time.sleep(0.01)
    scroll_x += 1
    if scroll_x >= str_len:
        scroll_x = 0

```

What I’d now like to do, is have it scroll some more meaningful text messages.

```auto
    if lux <= 100 :

        print("Too dark!")
    else :
        print("Too bright!")   
       
    if hding <= 200 :
        print("Closed")
    else :
        print("Open")

```

Straightforward enough to print out on the console, but how do I get them on the Scrollphathd?

Thank you.

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [December 31, 2018, 4:30pm UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/18 "2018-12-31T16:30:10Z")

</div>

Replace the “print” with “scrollphathd.write\_string” maybe?

I scroll text on my Sense Hat LED matrix.  
day, date, time, temp, humidity, pressure etc.  
All color coded based on conditions, Blue text for the temperature if the temperature is zero c or lower for example. I’ll post a bit of my code but I don’t think it will help you much.

```auto
t = sense.get_temperature()
    t = round(t)
          
    if t <= 0: 
        tc = [w, w, 255] # Blue
    elif t > 0 and t < 13:
        tc = [255, 255, w] # Yellow
    elif t >= 13 and t <= 25:
        tc = [w, 255, w] # Green
    elif t > 25 and t < 30:
        tc = [255, o, w] # Orange
    elif t >= 30:
        tc = [255, w, w] # Red   

    msg = "and %sc" % (t)
    sense.show_message(msg, scroll_speed=s, text_colour=tc)

```

---

<div class="post-metadata">

**Author:** ![Richard238](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard238](https://forums.pimoroni.com/u/Richard238)\
**Post date:** [December 31, 2018, 4:57pm UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/19 "2018-12-31T16:57:11Z")

</div>

Progress!

This works…

```auto
    if lux <= 100 :

        str_len = scrollphathd.write_string(("Too dark! "), x=0, y=0, font=font5x7)
    else :
        str_len = scrollphathd.write_string(("Too bright! "), x=0, y=0, font=font5x7)   
       

```

But if I add a second sensor, the first is overwritten on the scrollphathd…

```auto

    if lux <= 100 :

        str_len = scrollphathd.write_string(("Too dark! "), x=0, y=0, font=font5x7)
    else :
        str_len = scrollphathd.write_string(("Too bright! "), x=0, y=0, font=font5x7)   
        
    
    scrollphathd.clear()   
      
    if hding <= 200 :
        str_len = scrollphathd.write_string(("Closed"), x=0, y=0, font=font5x7)
    else :
        str_len = scrollphathd.write_string(("Open"), x=0, y=0, font=font5x7)  

```

Thank you.

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [December 31, 2018, 5:30pm UTC](https://forums.pimoroni.com/t/scrollphat-hd-query/8848/20 "2018-12-31T17:30:49Z")

</div>

Does the first message only scroll the once and stop.  
Or repeat endlessly until you do the clear?  
You might have to take out the clear or add a time sleep command to add a delay between messages.

[Next page](https://forums.pimoroni.com/t/scrollphat-hd-query/8848.md?page=2)
