Advertisement
SoapSuds

BeagleBone - Read an analog signal from pin: 37Header: P9

Jan 25th, 2012
786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. #Script By Shane B.
  2. #Website: PIPBOY3000.com -- Contribute to the OpenSource PipBoy 3000 project
  3. #MIT LICENSE
  4. #Power Supply of 1.8v is on Header: P9 and PIN Number: 32
  5. #Hook the negative to Ground pins 1(or 2) on Header: P9
  6. #Hook signal to PIN Number: 37 on Header: P9
  7.  
  8. #RESOURCE TUTORIAL: http://www.gigamegablog.com/2012/01/22/beaglebone-coding-101-using-the-serial-and-analog-pins/
  9.  
  10. #Import libraries#
  11. ##################
  12. #No Libaries yet
  13.  
  14.  
  15. #Define Functions#
  16. ##################
  17. def average(seq, total=0.0):  
  18.     num = 0  
  19.     for item in seq:  
  20.         total += item  
  21.         num += 1  
  22.     return total / num  
  23.  
  24.  
  25.  
  26. #Define variables#
  27. ##################
  28. avg = 0
  29. inputData = []
  30. sendOutputcounter = 0
  31.  
  32.  
  33. #Begin Script
  34. #########################################################################
  35. while(True):
  36.         #Analog 2
  37.         inputData.append(int(open("/sys/devices/platform/tsc/ain2","rb").read()))
  38.  
  39.         #Once inputData reaches the length of 100 -- delete the most out of date input value
  40.         if len(inputData) >= 20:
  41.                 inputData.pop(0);
  42.  
  43.         avg = average(inputData)
  44.         avg = avg/1000
  45.         avg = avg*10
  46.     avg = round(avg)
  47.  
  48.     #Slow down read out
  49.     sendOutputcounter = sendOutputcounter + 1
  50.     if sendOutputcounter > 100:
  51.         sendOutputcounter = 0
  52.         print avg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement