Tuesday, October 25, 2016

Prime Numbers in a Box

-----
How many times have you needed the next prime number in a sequence and, like some animal, had to go to a printed table to look it up.  Well, those days are over.
----
A prime number is any positive whole number that can only get evenly divided only by 1 and itself.  Primes are used in many applications; a popular use being for encryption and cryptography.  Demonstrated here is another use for prime numbers.  That is making use of an older/slower Raspberry PI and a few parts to nerd up the decor of any room.

A few Raspberry PI skills learned will be:
     - writing to text files
     - reading from text files
     - driving a low cost I2C LCD display
     - driving a relay via a transistor
     - simple graceful shutdown method for the RasPI with a button and a JST connector.
-----
 
The project has an entertaining audio effect if you are into numbers.  Primes go on forever and ever; infinitely large.  The smallest numerical difference between two primes is 2 (example: 7-5=2).  What is interesting is the distance (difference) between two consecutive primes stays relatively low as the primes become very large.  Press a button and Primes in a Box gives a audible (relay click) signal for each non prime as it waits to display the next found prime.  If you enjoy mathematics you may find this oddly relaxing.
-----
The python source is pretty straight forward.  On button press a pointer to a file containing the first few million primes is indexed and displayed on the LCD.  A 5VDC relay clicks to represent the non primes in between.  The rig runs via USB power and the last found prime is always saved.  A handy 'shutdown' button is incorporated to allow the Raspbery PI project to be powered down gracefully if it needs to be moved.
-----
You'll need a Raspberry PI, 5VDC relay, PN2222A transistor, 16x2 I2C LCD, two resistors, and two normally open button switches.  A project box holds it all together.  Connect it all up like this:
Once on the breadboard it will look a bit like this:
 -----
Set up the RasPI to run the python code below at reboot (use @reboot in the sudo crontab).
Note, two files are expected to be found in the working directory:
     - prime_list.txt (List of prime numbers in order. One per line. As many as you like)
     - high_prime.txt (Holds the highest prime found in case of a restart/reboot)

#!/usr/bin/python

#
#  WhiskeyTangoHotel.Com
#  OCT 2016
#  
#  Program reads a file and displays prime # on LCD
#  On button press find next prime #.  Click a relay for each non prime
#  Write new highest prime to file to save if restarting the program
#
#  Gives an audible (relay click) representation of the distance between primes
#
#  Expects program to be in: /home/pi/RasPI/Programs-RasPI/Prime_Relay/
#
#  Leverages LCD script using I2C backpack for 16x2 and 20x4 screens.
#  Thanks to Matt Hawkins  http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------

import smbus  # for LCD I2C display
import time # fpr sleep and pause delays
import os # this is for the shutdown button press
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD) # to use Raspberry Pi board pin numbers
# set up GPIO output channel
Relay_Pin = 11 # pin to drive the relay clicks via 2222a transistor
GPIO.setup(Relay_Pin, GPIO.OUT)
Relay_delay = .1  # time delay between relay clicks

Switch = 8  # Button switch to move to next prime number
GPIO.setup(Switch, GPIO.IN)

reboot_pin = 26 #  Push this button and the RasPI shuts down gracefully
#Set pin to input and set pull-up resistor to hold the pin is high
GPIO.setup(reboot_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#######################################
### LCD DRIVER FUNCTIONS START HERE ###
#######################################

# Define some device parameters
I2C_ADDR  = 0x27 # I2C device address
LCD_WIDTH = 16   # Maximum characters per line

# Define some device constants
LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line

LCD_BACKLIGHT  = 0x08  # On
#LCD_BACKLIGHT = 0x00  # Off

ENABLE = 0b00000100 # Enable bit

# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005

#Open I2C interface
#bus = smbus.SMBus(0)  # Rev 1 Pi uses 0
bus = smbus.SMBus(1) # Rev 2 Pi uses 1 RasPI(Bob)

def lcd_init():
 # Initialise display
 lcd_byte(0x33,LCD_CMD) # 110011 Initialise
 lcd_byte(0x32,LCD_CMD) # 110010 Initialise
 lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
 lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off 
 lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
 lcd_byte(0x01,LCD_CMD) # 000001 Clear display
 time.sleep(E_DELAY)

def lcd_byte(bits, mode):
 # Send byte to data pins
 # bits = the data
 # mode = 1 for data
 #        0 for command

 bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
 bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT

 # High bits
 bus.write_byte(I2C_ADDR, bits_high)
 lcd_toggle_enable(bits_high)

 # Low bits
 bus.write_byte(I2C_ADDR, bits_low)
 lcd_toggle_enable(bits_low)

def lcd_toggle_enable(bits):
 # Toggle enable
 time.sleep(E_DELAY)
 bus.write_byte(I2C_ADDR, (bits | ENABLE))
 time.sleep(E_PULSE)
 bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
 time.sleep(E_DELAY)

def lcd_string(message,line):
 # Send string to display
 message = message.ljust(LCD_WIDTH," ")
 lcd_byte(line, LCD_CMD)
 for i in range(LCD_WIDTH):
  lcd_byte(ord(message[i]),LCD_CHR)
  
#######################################
### LCD DRIVER FUNCTIONS END HERE  ###
#######################################

# Highest calulated prime is stored in a file.  Get that number
highprime = open('/home/pi/RasPI/Programs-RasPI/Prime_Relay/high_prime.txt','r')
storedprime = (highprime.readline())
storedprime_val = int(storedprime)
print ' '
print "Recalled highest prime from file is: " + storedprime
print ' '

#Put status on LCD
lcd_init()
lcd_string("Init to last",LCD_LINE_1)
lcd_string("prime: " + str(storedprime_val),LCD_LINE_2)
highprime.close

def main():
 # Main program block 
 
 # Relay test clicks
 for i in range(1, 3):  
  GPIO.output(Relay_Pin,GPIO.HIGH) # Close relay
  time.sleep(Relay_delay) 
  GPIO.output(Relay_Pin,GPIO.LOW) # Open relay
  time.sleep(Relay_delay)  
 
 # Set the pointer to the correct place in the file that holds the prime list
 # prime_list.txt contains primes to 1,299,709
 primefile = open('/home/pi/RasPI/Programs-RasPI/Prime_Relay/prime_list.txt','r')
 currentprime_val = 0

 while storedprime_val > currentprime_val:
  currentprime = (primefile.readline())
  currentprime_val = int(currentprime)
  
 # Update the LCD
 lcd_string("PRESS for next",LCD_LINE_1)
 lcd_string("prime: " + str(currentprime_val),LCD_LINE_2)
 time.sleep(0.5)
 
 # Relay test clicks
 for i in range(1, 3): 
  GPIO.output(Relay_Pin,GPIO.HIGH) # Close relay
  time.sleep(Relay_delay) 
  GPIO.output(Relay_Pin,GPIO.LOW) # Open relay
  time.sleep(Relay_delay) 
 
 while True:
  
  if (GPIO.input(reboot_pin) == 0):
   # Update the LCD
   lcd_string("Primes in a Box",LCD_LINE_1)
   lcd_string("shutting down!!!" + str(currentprime_val),LCD_LINE_2)
   time.sleep(5)
   #Send command to system to shutdown
   os.system("sudo shutdown -h now")
  
  if (GPIO.input(Switch) == 0):
   # Update the LCD
   lcd_string("Calculating next",LCD_LINE_1)
   lcd_string("prime: " + str(currentprime_val),LCD_LINE_2)
  
   oldprime = currentprime_val
   currentprime = (primefile.readline())
   currentprime_val = int(currentprime)
   deltaprime = currentprime_val - oldprime 
   
   for i in range(1, deltaprime+1):  # click relay for each non prime between primes
    #print i
    GPIO.output(Relay_Pin,GPIO.HIGH) # Close relay
    time.sleep(Relay_delay) 
    GPIO.output(Relay_Pin,GPIO.LOW) # Open relay
    time.sleep(Relay_delay)   

   #Write the prime to a file so it can be recalled at prog start
   hp = open("/home/pi/RasPI/Programs-RasPI/Prime_Relay/high_prime.txt","w")
   hp.write(str(currentprime_val))
   
   print "CURRENT " +  str(currentprime_val)
   print "    OLD " + str(oldprime)
   print '------- '
   print "  DELTA " +  str(deltaprime)
   #print i
   print ' '

   # Update the LCD
   lcd_string("PRESS for next",LCD_LINE_1)
   lcd_string("prime: " + str(currentprime_val),LCD_LINE_2)

   hp.close
   time.sleep(.250)

if __name__ == '__main__':

 try:
  main()
 except KeyboardInterrupt:
  pass
 finally:
  lcd_byte(0x01, LCD_CMD)
----
Thanks for the visit and happy prime numbering!