import serial
import pyautogui

port = "/dev/ttyUSB0"

# open serial port
ser = serial.Serial(port,9600)
ser.setDTR()

#scroll value
scroll = 5

#last value of sensor
lvalue = 1

def slope(v):
	# if increasing # neclect larg steps
	if v > lvalue and (v -lvalue != 7):
		return 1
	# if decreasing # neglect larg steps
	elif v < lvalue and (lvalue - v != 7):
		return -1
#
# Mainloop
#
while True:
	# read serial
	v = ord(ser.read())
	#																 8  4  2  1
	# pass if the value of individual sensor is available 0b0 0 0 0 S4 S3 S2 S1
	if v in [1, 2, 4, 8]:
		# get slope
		s = slope(v)
		if s == 1:
			# scroll up
			pyautogui.scroll(scroll)
		elif s == -1:
			# scroll down
			pyautogui.scroll(-1 * scroll)
		lvalue = v