1

I'm trying to do a KITT Led Scanner on the simulator at https://trinket.io/sense-hat. This is my code:

from sense_hat import SenseHat
import time
s = SenseHat()
R = (255,0,0)
x = 0
y = 1
s.clear()



while True:
  First = s.set_pixel(x,4,R)
  Second = s.set_pixel(y,4,R)
  time.sleep(0.4)
  s.clear()
  x = x + 1
  y = y + 1

My question is, how do I reverse this motion and loop it once it reaches x = 0 and y = 1? Thank you!

1
  • 1
    it makes no sense to use the y variable .... use the x variable only, then use Second = s.set_pixel(x+1,4,R)
    – jsotola
    Nov 16, 2019 at 20:11

1 Answer 1

1

Use a variable for the increment that can be switched from +1 to -1 depending on the position.

from sense_hat import SenseHat
import time
s = SenseHat()
R = (255,0,0)
x = 0
y = 1
s.clear()

inc = 1

while True:
  First = s.set_pixel(x,4,R)
  Second = s.set_pixel(y,4,R)
  time.sleep(0.4)
  s.clear()
  x = x + inc
  y = y + inc
  if y == 7:
    inc = -1
  if x == 0:
    inc = 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.