Language: Python
I want the value of a variable to be initialized to zero at the start of the execution. This variable is used in a function & its value may also get changed within the function. But I do not want the value of this variable to reset to zero whenever a function call is made. Instead, its value should be equal to the updated value from its previous function call.
Example:
get_current() is constantly returning a different value when it is called.
ctemp is initially zero.
In the first function call get_current_difference() will return cdiff & then update the value of ctemp such that ctemp = current.
In the second function call, the value of ctemp should not be zero. Instead, it should be equal to the updated value from the first function call.
import serial
arduino = serial.Serial('/dev/ttyACM1',9600)
ctemp = 0
def get_current():
line = arduino.readline()
string = line.replace('\r\n','')
return string
def get_current_difference():
global ctemp
current = get_current()
cdiff = float(current) - ctemp
return cdiff
ctemp = current
while 1:
current = get_current_difference()
print(current)