Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # Import required libraries
- import time
- import pygame
- import RPi.GPIO as GPIO
- # Use BCM GPIO references
- # instead of physical pin numbers
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- # Define some settings
- StepCounter = 0
- WaitTime = .01
- steppingOne = False
- steppingTwo = False
- positionOne = 0
- positionTwo = 0
- targetOne = 0
- targetTwo = 0
- stepPins = []
- stepPattern = []
- stepPattern = range(0, 8)
- stepPattern[0] = [1,0,0,0] # Starting step
- stepPattern[1] = [1,1,0,0]
- stepPattern[2] = [0,1,0,0] # Second step
- stepPattern[3] = [0,1,1,0]
- stepPattern[4] = [0,0,1,0] # Third step
- stepPattern[5] = [0,0,1,1]
- stepPattern[6] = [0,0,0,1] # Fourth step
- stepPattern[7] = [1,0,0,1]
- def moveFlap(zone, openIt): # zone is 1 or 2, openIt is true or false
- global stepPins
- if (zone==1):
- if (openIt):
- stepPins = [17,4,22,21]
- else:
- stepPins = [21,22,4,17]
- else:
- if (openIt):
- stepPins = [23,18,25,24]
- else:
- stepPins = [24,25,18,23]
- def initGPIOpins():
- # Set all pins as output
- global stepPins
- stepPins = [4,17,18,21,22,23,24,25]
- for pin in stepPins:
- GPIO.setup(pin,GPIO.OUT)
- GPIO.output(pin, False)
- def stepNow():
- for pin in range(0, 4):
- xpin = StepPins[pin]
- for stepCounter in range(0, 8):
- if StepPattern[stepCounter][pin]!=0:
- # print " Step %i Enable %i" %(StepCounter,xpin)
- GPIO.output(xpin, True)
- else:
- GPIO.output(xpin, False)
- def moveAsRequested():
- global positionOne, targetOne, positionTwo, targetTwo
- if (positionOne != targetOne):
- steppingOne = True
- moveFlap(1, positionOne>targetOne)
- stepNow()
- if positionOne>targetOne:
- positionOne -= 4
- else:
- positionOne += 4
- elif (positionTwo != targetTwo):
- steppingTwo = True
- moveFlap(2, positionTwo>targetTwo)
- stepNow()
- if positionTwo>targetTwo:
- positionTwo -= 4
- else:
- positionTwo += 4
- def main():
- pygame.init()
- initGPIOpins()
- screen = pygame.display.set_mode((640,480))
- pygame.display.set_caption("Heater Controller")
- clock = pygame.time.Clock()
- # Start main loop
- while 1==1:
- clock.tick(50)
- moveAsRequested()
- # Handle events
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- return
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_q:
- pygame.quit()
- return
- elif event.key == pygame.K_g:
- targetOne=100
- screen.fill((0,0,0))
- pygame.display.flip()
- # If we reach the end of the sequence
- # start again
- # Wait before moving on
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement