sandeep03edu

Mann Morrision Raspberry Pi Code

May 4th, 2023
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. import math
  4.  
  5. pi = 3.14159265359
  6. f = 0.25
  7. timeDelay = 1/(100*f)
  8. peak = 5
  9. offSet = 2.5
  10.  
  11. last = -10
  12. prevLast = -10
  13.  
  14. def bin2Dec(in1, in2, in3, in4, in5, in6, in7, in8):
  15.     ans = 128*in1 + 64*in2 + 32*in3 + 16*in4 + 8*in5 + 4*in6 + 2*in7 + in8
  16.     return ans
  17.  
  18. def getVoltage(score):
  19.     ans = score/256
  20.     ans = ans*peak
  21.     return ans
  22.  
  23.  
  24. def getPeakVol(curr):
  25.     vdash = curr - prevLast
  26.     vdash/=timeDelay
  27.     vdash/=2
  28.  
  29.     omega = 2*pi*f
  30.  
  31.     last2 = last*last
  32.     term2 = vdash/omega
  33.     term2 = term2*term2
  34.  
  35.     vPeak = last2 + term2
  36.     vPeak = math.sqrt(vPeak)
  37.     return vPeak
  38.  
  39.  
  40. # Set GPIO mode and pins
  41. GPIO.setmode(GPIO.BCM)
  42. GPIO.setup(26, GPIO.IN)
  43. GPIO.setup(12, GPIO.IN)
  44. GPIO.setup(13, GPIO.IN)
  45. GPIO.setup(15, GPIO.IN)
  46. GPIO.setup(16, GPIO.IN)
  47. GPIO.setup(25, GPIO.IN)
  48. GPIO.setup(22, GPIO.IN)
  49. GPIO.setup(24, GPIO.IN)
  50.  
  51.  
  52. try:
  53.     while True:
  54.         # Read input from pins
  55.         input1 = GPIO.input(26)
  56.         input2 = GPIO.input(12)
  57.         input3 = GPIO.input(13)
  58.         input4 = GPIO.input(15)
  59.         input5 = GPIO.input(16)
  60.         input6 = GPIO.input(25)
  61.         input7 = GPIO.input(22)
  62.         input8 = GPIO.input(24)
  63.        
  64.         vol =  getVoltage(bin2Dec(input1, input2, input3, input4, input5, input6, input7, input8))
  65.         vol = vol - offSet
  66.  
  67.         # Print input voltage values
  68.         print(vol, " ")
  69.  
  70.  
  71.         if(prevLast==-10):
  72.             prevLast = vol
  73.         elif(last==-10):
  74.             last = vol
  75.         else:
  76.             print("Peak: ", getPeakVol(vol))
  77.             prevLast =last
  78.             last = vol
  79.             pass
  80.  
  81.         # Wait for timeDelay duration
  82.         time.sleep(timeDelay)
  83.        
  84. except KeyboardInterrupt:
  85.     # Clean up GPIO settings
  86.     GPIO.cleanup()
  87.     pass
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment