Advertisement
AverageMan

8x2 Time Lapse

Nov 7th, 2014
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.00 KB | None | 0 0
  1. #!/usr/bin/python
  2. #
  3. # 8x2 time lapse for the Raspberry Pi, By @AverageManVsPi
  4. # AverageManVsRaspberryPi.com
  5. # This project uses a ProtoCam prototyping board - goo.gl/YsaaDu
  6. #
  7. # The wiring for the LCD is as follows:
  8. # 1 : GND
  9. # 2 : 5V
  10. # 3 : Contrast (0-5V)*
  11. # 4 : RS (Register Select)
  12. # 5 : R/W (Read Write)       - GROUND THIS PIN! We do not want the LCD to send anything to the Pi @ 5v
  13. # 6 : Enable or Strobe
  14. # 7 : Data Bit 0             - NOT USED
  15. # 8 : Data Bit 1             - NOT USED
  16. # 9 : Data Bit 2             - NOT USED
  17. # 10: Data Bit 3             - NOT USED
  18. # 11: Data Bit 4
  19. # 12: Data Bit 5
  20. # 13: Data Bit 6
  21. # 14: Data Bit 7
  22. # 15: LCD Backlight +5V
  23. # 16: LCD Backlight GND
  24.  
  25. #import
  26. import RPi.GPIO as GPIO
  27. import time
  28. import os
  29. import datetime
  30.  
  31. # Define GPIO to LCD mapping
  32. LCD_RS = 17
  33. LCD_E  = 18
  34. LCD_D4 = 21
  35. LCD_D5 = 22
  36. LCD_D6 = 23
  37. LCD_D7 = 24
  38.  
  39. # Define GPIO for button Controls
  40. LEFTBUTTON = 11 #GPIO 11 (BCM)
  41. RIGHTBUTTON = 9 #GPIO 9 (BCM)
  42. GREENLED = 14 #GPIO 14 (BCM)
  43.  
  44. # Define some device constants
  45. LCD_WIDTH = 8    # Maximum characters per line
  46. LCD_CHR = True
  47. LCD_CMD = False
  48.  
  49. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  50. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  51.  
  52. # Timing constants
  53. E_PULSE = 0.00005
  54. E_DELAY = 0.00005
  55.  
  56. def main():
  57.   GPIO.setwarnings(False) #Turn off annoying GPIO warnings
  58.   GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  59.   GPIO.setup(LCD_E, GPIO.OUT)  # E
  60.   GPIO.setup(LCD_RS, GPIO.OUT) # RS
  61.   GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  62.   GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  63.   GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  64.   GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  65.  
  66.   GPIO.setup(LEFTBUTTON, GPIO.IN) # set left button to input
  67.   GPIO.setup(RIGHTBUTTON, GPIO.IN) # set right button to input
  68.   GPIO.setup(GREENLED, GPIO.OUT) # set LED GPIO 14 to output
  69.  
  70.   # Initialise display
  71.   lcd_init()
  72.  
  73.   # Initial splash screen
  74.   lcd_byte(LCD_LINE_1, LCD_CMD)
  75.   lcd_string("ProtoCam",2)
  76.   lcd_byte(LCD_LINE_2, LCD_CMD)
  77.   lcd_string("Rules!",2)
  78.   time.sleep(2.5) #Little breather
  79.   menu() #go to menu()
  80.  
  81.  
  82.  
  83. def menu(): #main menu
  84.   time.sleep(0.5)
  85.   while(1):
  86.    if ( GPIO.input(LEFTBUTTON) == True): #if left button clicked, go to still() to take a still shot
  87.     tlapse()
  88.    if ( GPIO.input(RIGHTBUTTON) == True): #if right button clicked, go to video() to take a vid
  89.     menu()
  90.    else:
  91.     lcd_byte(LCD_LINE_1, LCD_CMD)
  92.     lcd_string("^ Start",1)
  93.     lcd_byte(LCD_LINE_2, LCD_CMD)
  94.     lcd_string("Back ^",3)
  95.            
  96. def tlapse(): # the first part of the code here generates the date and time string which will be our picture filename, ensuring a different file name every time.
  97.   var = 0 #var starts a 0
  98.   while var < 840: #var adds '1' after every shot, so I'm setting a maximum number of shots here
  99.     timelastchecked = 0 #
  100.     if time.time() >= timelastchecked:
  101.       timelastchecked = time.time()+2
  102.       imagecount = ""
  103.       f=os.popen("ls -l | grep ^- | wc -l") #this command counts the files in the directory
  104.       for i in f.readlines():
  105.        imagecount += i
  106.        imagecount = imagecount[0:-1] #the part at the end chops a character off the end of the string
  107.        imagecount = imagecount.zfill(4) #this makes the number a 4 digit numbers
  108.      
  109.        lcd_byte(LCD_LINE_1, LCD_CMD)
  110.        lcd_string("T-Lapse",3) #the '1' is align left, 2 is align center, 3 is align right
  111.        lcd_byte(LCD_LINE_2, LCD_CMD)
  112.        lcd_string(imagecount,3) #our image count, aligned right
  113.  
  114.        utc_datetime = datetime.datetime.utcnow()
  115.        s=utc_datetime.strftime("%d_%m_%Y_%H-%M-%S.jpg") #this is our filename string
  116.        filename = s #our string is now called 'filename'
  117.        GPIO.output(GREENLED, True) #LED on
  118.        os.system('raspistill -o ' + filename + ' ') #take a still using the filename set above
  119.        GPIO.output(GREENLED, False) #LED off
  120.        var = var + 1
  121.        time.sleep(25)
  122.  
  123.  
  124. # Everything after this is LCD setup 'stuff' - apart from the very last part
  125. def lcd_init():
  126.   # Initialise display
  127.   lcd_byte(0x33,LCD_CMD)
  128.   lcd_byte(0x32,LCD_CMD)
  129.   lcd_byte(0x28,LCD_CMD)
  130.   lcd_byte(0x0C,LCD_CMD)  
  131.   lcd_byte(0x06,LCD_CMD)
  132.   lcd_byte(0x01,LCD_CMD)  
  133.  
  134. def lcd_string(message,style):
  135.   # Send string to display
  136.   # style=1 Left justified
  137.   # style=2 Centred
  138.   # style=3 Right justified
  139.  
  140.   if style==1:
  141.     message = message.ljust(LCD_WIDTH," ")  
  142.   elif style==2:
  143.     message = message.center(LCD_WIDTH," ")
  144.   elif style==3:
  145.     message = message.rjust(LCD_WIDTH," ")
  146.  
  147.   for i in range(LCD_WIDTH):
  148.     lcd_byte(ord(message[i]),LCD_CHR)
  149.  
  150. def lcd_byte(bits, mode):
  151.   # Send byte to data pins
  152.   # bits = data
  153.   # mode = True  for character
  154.   #        False for command
  155.  
  156.   GPIO.output(LCD_RS, mode) # RS
  157.  
  158.   # High bits
  159.   GPIO.output(LCD_D4, False)
  160.   GPIO.output(LCD_D5, False)
  161.   GPIO.output(LCD_D6, False)
  162.   GPIO.output(LCD_D7, False)
  163.   if bits&0x10==0x10:
  164.     GPIO.output(LCD_D4, True)
  165.   if bits&0x20==0x20:
  166.     GPIO.output(LCD_D5, True)
  167.   if bits&0x40==0x40:
  168.     GPIO.output(LCD_D6, True)
  169.   if bits&0x80==0x80:
  170.     GPIO.output(LCD_D7, True)
  171.  
  172.   # Toggle 'Enable' pin
  173.   time.sleep(E_DELAY)    
  174.   GPIO.output(LCD_E, True)  
  175.   time.sleep(E_PULSE)
  176.   GPIO.output(LCD_E, False)  
  177.   time.sleep(E_DELAY)      
  178.  
  179.   # Low bits
  180.   GPIO.output(LCD_D4, False)
  181.   GPIO.output(LCD_D5, False)
  182.   GPIO.output(LCD_D6, False)
  183.   GPIO.output(LCD_D7, False)
  184.   if bits&0x01==0x01:
  185.     GPIO.output(LCD_D4, True)
  186.   if bits&0x02==0x02:
  187.     GPIO.output(LCD_D5, True)
  188.   if bits&0x04==0x04:
  189.     GPIO.output(LCD_D6, True)
  190.   if bits&0x08==0x08:
  191.     GPIO.output(LCD_D7, True)
  192.  
  193.   # Toggle 'Enable' pin
  194.   time.sleep(E_DELAY)    
  195.   GPIO.output(LCD_E, True)  
  196.   time.sleep(E_PULSE)
  197.   GPIO.output(LCD_E, False)  
  198.   time.sleep(E_DELAY)  
  199.  
  200. #This part leaves a message on the LCD once the script is exited, and cleans up GPIO
  201. try:
  202.   main()
  203. except:
  204.   print "Exit - cleaning up GPIO"
  205.  
  206.   lcd_byte(LCD_LINE_1, LCD_CMD)
  207.   lcd_string("Camera",2)
  208.   lcd_byte(LCD_LINE_2, LCD_CMD)
  209.   lcd_string(" Off",2)
  210.  
  211.   GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement