class DCMOTOR_ROTATION: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.pin1=9 self.pin2=10 self.obj_arduino.cmd_dcmotor_setup(1,3,1,self.pin1,self.pin2) self.obj_arduino.cmd_dcmotor_run(1,1,100) sleep(2) self.obj_arduino.cmd_dcmotor_release(1,1) def exit(self): self.obj_arduino.close_serial()
class PUSHBUTTON_LED: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.blue=9 self.green=10 self.red=11 self.pushbutton=12 for i in range(20): val=self.obj_arduino.cmd_digital_in(1,self.pushbutton) #sleep(0.5) print val self.obj_arduino.cmd_digital_out(1,self.blue,val) sleep(0.5) def exit(self): self.obj_arduino.close_serial()
class LED_ON_OFF_MULTICOLOR: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.blue = 9 self.green = 10 self.red = 11 self.obj_arduino.cmd_digital_out(1, self.blue, self.baudrate) self.obj_arduino.cmd_digital_out(1, self.red, self.baudrate) sleep(5) self.obj_arduino.cmd_digital_out(1, self.blue, 0) sleep(3) self.obj_arduino.cmd_digital_out(1, self.red, 0) def exit(self): self.obj_arduino.close_serial()
class SIPO_IC: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): dataPin=11 clockPin=9 latchPin=10 inPin=5 for _ in range(0,100): self.obj_arduino.cmd_digital_out(1,latchPin,0) #So that the data is stored and not passed on to the output LEDs self.obj_arduino.cmd_shift_out_(dataPin,clockPin,inPin) self.obj_arduino.cmd_digital_out(1,latchPin,1) #So that the stored data is now passed on to the output LEDs #and the output is obtained sleep(0.5) def exit(self): self.obj_arduino.close_serial()
class THERM_BUZZER: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.therm=4 self.buzzer=3 for i in range(20): val=self.obj_arduino.cmd_analog_in(1,self.therm) print val if (int(val) > 550): self.obj_arduino.cmd_digital_out(1,self.buzzer,1) else: self.obj_arduino.cmd_digital_out(1,self.buzzer,0) sleep(2) def exit(self): self.obj_arduino.close_serial()
class LED_ON_OFF_MULTICOLOR: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.blue=9 self.green=10 self.red=11 self.obj_arduino.cmd_digital_out(1,self.blue,self.baudrate) self.obj_arduino.cmd_digital_out(1,self.red,self.baudrate) sleep(5) self.obj_arduino.cmd_digital_out(1,self.blue,0) sleep(3) self.obj_arduino.cmd_digital_out(1,self.red,0) def exit(self): self.obj_arduino.close_serial()
class COUNTER_wo_ff: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): pinstate = '0' lastpinstate = '0' clockPin = 5 # Pulse to be counted #outputs Pin1 = 9 #LSB Pin2 = 10 #middle bit Pin3 = 11 #MSB i = 0 a = 0 b = 0 c = 0 for _ in range(0, 500): pinstate = self.obj_arduino.cmd_digital_in(1, clockPin) #negative edge of clock pulse to FF1 if pinstate != lastpinstate: if pinstate == '0': i += 1 else: pass sleep(0.05) else: pass lastpinstate = pinstate a = i % 2 b = (i / 2) % 2 c = (i / 4) % 2 self.obj_arduino.cmd_digital_out(1, Pin1, a) #LSB self.obj_arduino.cmd_digital_out(1, Pin2, b) #middle bit self.obj_arduino.cmd_digital_out(1, Pin3, c) #MSB sleep(0.1) if i > 7: i = 0 else: pass def exit(self): self.obj_arduino.close_serial()
class LED_ON_OFF_LOOP: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.blue=9 self.green=10 self.red=11 for i in range(6): self.obj_arduino.cmd_digital_out(1,self.green,1) sleep(2) self.obj_arduino.cmd_digital_out(1,self.green,0) sleep(2) def exit(self): self.obj_arduino.close_serial()
class SERVO_INIT: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.pin1=9 self.obj_arduino.cmd_servo_attach(1,1) self.obj_arduino.cmd_servo_move(1,1,30) sleep(1) self.obj_arduino.cmd_servo_detach(1,1) sleep(1) def exit(self): self.obj_arduino.close_serial()
class SERVO_POT: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.pin1=9 self.pot=2 self.pos=0 self.obj_arduino.cmd_servo_attach(1,1) sleep(1) self.angle=18 for i in range(100): val=self.obj_arduino.cmd_analog_in(1,self.pot) print val val = int(val*180/1023) print val self.obj_arduino.cmd_servo_move(1,1,val) sleep(0.5) self.obj_arduino.cmd_servo_detach(1,1) def exit(self): self.obj_arduino.close_serial()
class LDR: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.ldr=5 self.blue=9 self.green=10 self.red=11 for i in range(100): val=self.obj_arduino.cmd_analog_in(1,self.ldr) print val if int(val) < 300: self.obj_arduino.cmd_digital_out(1,self.blue,1) else: self.obj_arduino.cmd_digital_out(1,self.blue,0) sleep(0.5) def exit(self): self.obj_arduino.close_serial()
class THERM_BUZZER: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.therm = 4 self.buzzer = 3 for i in range(20): val = self.obj_arduino.cmd_analog_in(1, self.therm) print val if (int(val) > 550): self.obj_arduino.cmd_digital_out(1, self.buzzer, 1) else: self.obj_arduino.cmd_digital_out(1, self.buzzer, 0) sleep(2) def exit(self): self.obj_arduino.close_serial()
class PUSHBUTTON_LED: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.blue = 9 self.green = 10 self.red = 11 self.pushbutton = 12 for i in range(20): val = self.obj_arduino.cmd_digital_in(1, self.pushbutton) #sleep(0.5) print val self.obj_arduino.cmd_digital_out(1, self.blue, val) sleep(0.5) def exit(self): self.obj_arduino.close_serial()
class SERVO_POT: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.pin1 = 9 self.pot = 2 self.pos = 0 self.obj_arduino.cmd_servo_attach(1, 1) sleep(1) self.angle = 18 for i in range(100): val = self.obj_arduino.cmd_analog_in(1, self.pot) print val val = int(val * 180 / 1023) print val self.obj_arduino.cmd_servo_move(1, 1, val) sleep(0.5) self.obj_arduino.cmd_servo_detach(1, 1) def exit(self): self.obj_arduino.close_serial()
class T_FF: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.T='0' self.tPin=5 #assuming initial state: self.Q='0' self.Qbar='1' self.qPin=9 self.qbarPin=10 self.clockPin=2 #external clock for _ in range(0,500): if self.Q=='0': self.obj_arduino.cmd_digital_out(1,self.qPin,0) self.obj_arduino.cmd_digital_out(1,self.qbarPin,1) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,self.qPin,1) self.obj_arduino.cmd_digital_out(1,self.qbarPin,0) sleep(0.1) if self.obj_arduino.cmd_digital_in(1,self.clockPin)=='1': #Reads clock and executes when it is HIGH self.T=self.obj_arduino.cmd_digital_in(1,self.tPin) #Reads input T if self.T=='1': temp=self.Q self.Q=self.Qbar self.Qbar=temp else: pass def exit(self): self.obj_arduino.close_serial()
class T_FF: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.T = '0' self.tPin = 5 #assuming initial state: self.Q = '0' self.Qbar = '1' self.qPin = 9 self.qbarPin = 10 self.clockPin = 2 #external clock for _ in range(0, 500): if self.Q == '0': self.obj_arduino.cmd_digital_out(1, self.qPin, 0) self.obj_arduino.cmd_digital_out(1, self.qbarPin, 1) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, self.qPin, 1) self.obj_arduino.cmd_digital_out(1, self.qbarPin, 0) sleep(0.1) if self.obj_arduino.cmd_digital_in( 1, self.clockPin ) == '1': #Reads clock and executes when it is HIGH self.T = self.obj_arduino.cmd_digital_in( 1, self.tPin) #Reads input T if self.T == '1': temp = self.Q self.Q = self.Qbar self.Qbar = temp else: pass def exit(self): self.obj_arduino.close_serial()
class SIPO_IC: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): pinstate=0 n=int(raw_input("Enter no. of bits: ")) data=[0 for _ in range(0,n)] #an 8-elements list representing an 8 bit binary number dataPin=11 clockPin=9 latchPin=10 inPin=5 for _ in range(0,50): pinstate=self.obj_arduino.cmd_digital_in(1,inPin) if pinstate=='1': data[0]=1 #the msb becomes 1 when input is given #high which is henceforth shifted else: data[0]=0 print data self.obj_arduino.cmd_digital_out(1,latchPin,0) self.obj_arduino.cmd_shift_out_n(dataPin,clockPin,'LSBFIRST',data,n) self.obj_arduino.cmd_digital_out(1,latchPin,1) sleep(0.5) for k in range(0,(n-1)): data[(n-1)-k]=data[(n-2)-k] data[0]=0 #every element of the matrix is #shifted one place to the right #so effectively the 8 bit #binary number is divided by 2 def exit(self): self.obj_arduino.close_serial()
class PISO_IC_p: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) # self.obj_icm=IC_methods(self.baudrate) def run(self): dataPin=9 clockPin=10 latchPin=11 ledPin=5 #LED that shows serial output clockLed=6 #LED that shows clock pulses p=int(raw_input("Enter no. of times parallel load is to be given: ")) self.obj_arduino.cmd_digital_out(1,latchPin,1) #parallel load mode for _ in range(0,p): print ("Give input, Parallel load mode:") sleep(2) self.obj_arduino.cmd_digital_out(1,clockPin,1) #positive edge occurs #parallel load is stored print("Inputs stored, Serial shift mode:") sleep(0.5) self.obj_arduino.cmd_digital_out(1,clockPin,0) self.obj_arduino.cmd_digital_out(1,latchPin,0) #serial out mode self.obj_arduino.cmd_shift_in(dataPin,clockPin,ledPin,clockLed) self.obj_arduino.cmd_digital_out(1,latchPin,1) self.obj_arduino.cmd_digital_out(1,ledPin,0) def exit(self): self.obj_arduino.close_serial()
class PUSHBUTTON_LED: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.blue=9 self.green=10 self.red=11 self.pushbutton=12 l = 10 # length x = range(l) # x axis T = [0 for i in range(l)] # initial value for i in range(20): val=self.obj_arduino.cmd_digital_in(1,self.pushbutton) #sleep(0.5) print val self.obj_arduino.cmd_digital_out(1,self.blue,val) sleep(0.5) plt.ion() #plt.show() t = int(val) T.pop(0) # pop first value T.append(t) # push at the end keeping list of same size plt.title("Is Pushbutton pressed ?") plt.grid(True) plt.ylabel('Pushbutton') plt.legend(loc='upper left') plt.axis([0, l, 0, 2]) plt.scatter(x, T, linewidth=5) plt.draw() plt.pause(.0001) plt.clf() plt.close('all') def exit(self): self.obj_arduino.close_serial()
class LDR: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.ldr=5 self.blue=9 self.green=10 self.red=11 l = 100 # length x = range(l) # x axis T = [420 for i in range(l)] # initial value for i in range(20): val=self.obj_arduino.cmd_analog_in(1,self.ldr) print val if int(val) < 300: self.obj_arduino.cmd_digital_out(1,self.blue,1) else: self.obj_arduino.cmd_digital_out(1,self.blue,0) sleep(0.5) plt.ion() #plt.show() t = int(val) T.pop(0) # pop first value T.append(t) # push at the end keeping list of same size plt.title("Reading LDR Data from Arduino...!") plt.grid(True) plt.ylabel('LDR Readings') plt.legend(loc='upper left') plt.axis([0, l, 0.55 * min(T), 2 * max(T)]) plt.plot(x, T ,linewidth=1) plt.draw() plt.pause(.0001) plt.clf() plt.close('all') def exit(self): self.obj_arduino.close_serial()
class SIPO_IC: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): pinstate = 0 n = int(raw_input("Enter no. of bits: ")) data = [0 for _ in range(0, n) ] #an 8-elements list representing an 8 bit binary number dataPin = 11 clockPin = 9 latchPin = 10 inPin = 5 for _ in range(0, 50): pinstate = self.obj_arduino.cmd_digital_in(1, inPin) if pinstate == '1': data[0] = 1 #the msb becomes 1 when input is given #high which is henceforth shifted else: data[0] = 0 print data self.obj_arduino.cmd_digital_out(1, latchPin, 0) self.obj_arduino.cmd_shift_out_n(dataPin, clockPin, 'LSBFIRST', data, n) self.obj_arduino.cmd_digital_out(1, latchPin, 1) sleep(0.5) for k in range(0, (n - 1)): data[(n - 1) - k] = data[(n - 2) - k] data[0] = 0 #every element of the matrix is #shifted one place to the right #so effectively the 8 bit #binary number is divided by 2 def exit(self): self.obj_arduino.close_serial()
class PISO_IC_n: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) # self.obj_icm=IC_methods(self.baudrate) def run(self): dataPin = 9 clockPin = 10 latchPin = 11 ledPin = 5 #LED that shows serial output clockLed = 6 #LED that shows clock pulses p = int(raw_input("Enter no. of times parallel load is to be given: ")) n = int(raw_input("Enter no. of bits: ")) self.obj_arduino.cmd_digital_out(1, latchPin, 1) #parallel load mode for _ in range(0, p): print("Give input, Parallel load mode:") sleep(2) self.obj_arduino.cmd_digital_out(1, clockPin, 1) #positive edge occurs #parallel load is stored print("Inputs stored, Serial shift mode:") sleep(0.5) self.obj_arduino.cmd_digital_out(1, clockPin, 0) self.obj_arduino.cmd_digital_out(1, latchPin, 0) #serial out mode self.obj_arduino.cmd_shift_in_n(dataPin, clockPin, ledPin, clockLed, n) self.obj_arduino.cmd_digital_out(1, latchPin, 1) self.obj_arduino.cmd_digital_out(1, ledPin, 0) def exit(self): self.obj_arduino.close_serial()
class LED_ON_OFF: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port,self.baudrate) def run(self): self.blue=9 self.obj_arduino.cmd_digital_out(1,self.blue,1) def exit(self): self.obj_arduino.close_serial()
class XOR_GATE: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.ledPin=9 self.aPin=5 self.bPin=6 for _ in range(0,100): vala=self.obj_arduino.cmd_digital_in(1,self.aPin) #Reads state of aPin and stores it in vala print "A= "+vala #print type(vala) #sleep(0.1) valb=self.obj_arduino.cmd_digital_in(1,self.bPin) #Reads state of bPin and stores it in valb print "B= "+valb #print type(valb) #sleep(0.1) if vala=='0' and valb=='0': self.obj_arduino.cmd_digital_out(1,self.ledPin,0) #sets state of output pin as LOW sleep(0.1) elif vala=='1' and valb=='1': self.obj_arduino.cmd_digital_out(1,self.ledPin,0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,self.ledPin,1) #sets state of output pin as HIGH sleep(0.1) def exit(self): self.obj_arduino.close_serial()
class XNOR_GATE: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.ledPin = 9 self.aPin = 5 self.bPin = 6 for _ in range(0, 100): vala = self.obj_arduino.cmd_digital_in( 1, self.aPin) #Reads state of aPin and stores it in vala print "A= " + vala #print type(vala) #sleep(0.1) valb = self.obj_arduino.cmd_digital_in( 1, self.bPin) #Reads state of bPin and stores it in valb print "B= " + valb #print type(valb) #sleep(0.1) if vala == '0' and valb == '0': self.obj_arduino.cmd_digital_out( 1, self.ledPin, 1) #sets state of output pin as HIGH sleep(0.1) elif vala == '1' and valb == '1': self.obj_arduino.cmd_digital_out(1, self.ledPin, 1) sleep(0.1) else: self.obj_arduino.cmd_digital_out( 1, self.ledPin, 0) #sets state of output pin as LOW sleep(0.1) def exit(self): self.obj_arduino.close_serial()
class PUSHBUTTON: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.pushbutton = 12 for i in range(10): val = self.obj_arduino.cmd_digital_in(1, self.pushbutton) sleep(1) print val def exit(self): self.obj_arduino.close_serial()
class LDR: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.ldr=5 for i in range(100): val=self.obj_arduino.cmd_analog_in(1,self.ldr) print (val) sleep(0.5) def exit(self): self.obj_arduino.close_serial()
class LDR: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.ldr=5 for i in range(100): val=self.obj_arduino.cmd_analog_in(1,self.ldr) print val sleep(0.5) def exit(self): self.obj_arduino.close_serial()
class LED_ON_OFF: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.blue = 9 self.obj_arduino.cmd_digital_out(1, self.blue, 1) sleep(2) self.obj_arduino.cmd_digital_out(1, self.blue, 0) sleep(2) def exit(self): self.obj_arduino.close_serial()
class SOLAR_TRACKER: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() #Locates the port self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.ldr1=0 #LDR1 connected at port 0 self.ldr2=1 #LDR2 connected at port 1 s=0 self.angle=5 # Step angle self.initial=90 self.obj_arduino.cmd_servo_attach(1,1) # Attach the servo at pin 9 sleep(1) self.obj_arduino.cmd_servo_move(1,1,self.initial) #Rotate servo by 90 deg sleep(1) for i in range(20): p=self.obj_arduino.cmd_analog_in(1,self.ldr1) #Read analog input from LDR1 q=self.obj_arduino.cmd_analog_in(1,self.ldr2) # Read analog input from LDR2 print p print q sleep(1) if int(p) < int(q): s=s+1 self.obj_arduino.cmd_servo_move(1,1,90-self.angle*s) # Clockwise rotation sleep(1) else: s=s-1 self.obj_arduino.cmd_servo_move(1,1,90-self.angle*s) # Anti-clockwise rotation sleep(1) self.obj_arduino.cmd_servo_detach(1,1) # Detach the motor def exit(self): self.obj_arduino.close_serial() # Close the serial port
class THERM_BUZZER: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.therm = 4 for i in range(20): val = self.obj_arduino.cmd_analog_in(1, self.therm) print val sleep(0.5) def exit(self): self.obj_arduino.close_serial()
class SERVO_INIT: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.pin1 = 9 self.obj_arduino.cmd_servo_attach(1, 1) self.obj_arduino.cmd_servo_move(1, 1, 30) sleep(1) self.obj_arduino.cmd_servo_detach(1, 1) sleep(1) def exit(self): self.obj_arduino.close_serial()
class PUSHBUTTON: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.pushbutton=12 for i in range(10): val=self.obj_arduino.cmd_digital_in(1,self.pushbutton) sleep(1) print val def exit(self): self.obj_arduino.close_serial()
class TEST_FIRMWARE: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port,self.baudrate) def run(self): def exit(self): self.obj_arduino.close_serial() def main(): obj_led=TEST_FIRMWARE(115200) if __name__== '__main__': main()
class SENSE: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() #Locates the port self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): jlast=0 k=0 j=0 self.pin1=9 self.pin2=10 self.intrp=5 ilast=self.obj_arduino.cmd_digital_in(1,self.intrp) self.obj_arduino.cmd_dcmotor_setup(1,3,1,self.pin1,self.pin2) # Initialize motor of the interrupt for s in range(5000): self.obj_arduino.cmd_dcmotor_run(1,1,100) # Rotate the motor i=self.obj_arduino.cmd_digital_in(1,self.intrp) # Read the signal of the receiver of IR sensor if i!=ilast: # Check whether the motor is rotating if i=='1': # Condition when the receiver receives the signal from transmitter j=j+1 sleep(0.01) ilast=i if j!=jlast: print j if j%17==0 and j!=0: # Condition for one complete rotation k=k+1 # No of rotations print 'number of rotations completed are',k jlast=j self.obj_arduino.cmd_dcmotor_release(1,1) def exit(self): self.obj_arduino.close_serial()
class LED_ON_OFF_LOOP: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.blue = 9 self.green = 10 self.red = 11 for i in range(10): self.obj_arduino.cmd_digital_out(1, self.green, 1) sleep(2) self.obj_arduino.cmd_digital_out(1, self.green, 0) sleep(2) def exit(self): self.obj_arduino.close_serial()
class POT: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.pot = 2 self.blue = 9 self.green = 10 self.red = 11 for i in range(20): val = self.obj_arduino.cmd_analog_in(1, self.pot) print(val) if (int(val) >= 0 and int(val) < 320): self.obj_arduino.cmd_digital_out(1, self.blue, 1) sleep(2) self.obj_arduino.cmd_digital_out(1, self.blue, 0) elif (int(val) >= 320 and int(val) < 900): self.obj_arduino.cmd_digital_out(1, self.green, 1) sleep(2) self.obj_arduino.cmd_digital_out(1, self.green, 0) else: self.obj_arduino.cmd_digital_out(1, self.red, 1) sleep(2) self.obj_arduino.cmd_digital_out(1, self.red, 0) def exit(self): self.obj_arduino.close_serial()
class SIPO: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): pinstate='0' lastpinstate='0' clockPin=5 # Pulse to be counted D2='0' #serial data input, input by user is given to FF2 (MSB FF) #output of FF2=input of FF1 D1='0' #D1=Q2, FF1 (middle bit FF), output of FF1=input of FF0 D0='0' #D0=Q1, FF0 (LSB FF), output of FF0 = Q0 Q='0' #output of FF0 #Serial input inPin=5 #Parallel output outPin1=9 #LSB = Q outPin2=10 #middle bit outPin3=11 #MSB #external clock pulse clockPin=2 for _ in range(0,100): #pin 9=Q=LSB if D0=='0': self.obj_arduino.cmd_digital_out(1,outPin1,0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,outPin1,1) sleep(0.1) #pin 10=Q1=D0=middle bit if D1=='0': self.obj_arduino.cmd_digital_out(1,outPin2,0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,outPin2,1) sleep(0.1) #pin 11=Q2=D1=MSB if D2=='0': self.obj_arduino.cmd_digital_out(1,outPin3,0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,outPin3,1) sleep(0.1) #reads the state of clock pinstate=self.obj_arduino.cmd_digital_in(1,clockPin) #clock is common for all FFs #thus only 1 if statement for detecting positive edge of clock if pinstate!=lastpinstate: if pinstate=='1': #order of FFs: serial input-FF2-FF1-FF0 #FF0 (LSB FF, i.e. third FF) if D0=='0': Q='0' else: Q='1' #FF1 (middle bit FF i.e. second FF) if D1=='0': D0='0' else: D0='1' #FF2 (MSB FF i.e first FF) if D2=='0': D1='0' else: D1='1' D2=self.obj_arduino.cmd_digital_in(1,inPin) #input is given to D of FF2 (MSB FF) sleep(0.05) lastpinstate=pinstate def exit(self): self.obj_arduino.close_serial()
class PIPO: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): pinstate = '0' lastpinstate = '0' D2 = '0' #MSB input D1 = '0' #middle bit input=MSB output D0 = '0' #LSB input=middle bit output Q = '0' #LSB output #Parallel inputs inPin1 = 5 #LSB input inPin2 = 6 #middle bit inPin3 = 7 #MSB #Parallel output outPin1 = 9 #LSB = Q outPin2 = 10 #middle bit = D0 outPin3 = 11 #MSB = D1 #external clock pulse clockPin = 2 for _ in range(0, 100): #pin 9=Q=LSB output if Q == '0': self.obj_arduino.cmd_digital_out(1, outPin1, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, outPin1, 1) sleep(0.1) #pin 10=Q1=D0=middle bit output if D0 == '0': self.obj_arduino.cmd_digital_out(1, outPin2, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, outPin2, 1) sleep(0.1) #pin 11=Q2=D1=MSB output if D1 == '0': self.obj_arduino.cmd_digital_out(1, outPin3, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, outPin3, 1) sleep(0.1) #reads the state of clock pinstate = self.obj_arduino.cmd_digital_in(1, clockPin) #clock is common for all FFs #thus only 1 if statement for detecting positive edge of clock if pinstate != lastpinstate: if pinstate == '1': #order of FFs: serial input-FF2-FF1-FF0 D0 = self.obj_arduino.cmd_digital_in(1, inPin1) D1 = self.obj_arduino.cmd_digital_in(1, inPin2) D2 = self.obj_arduino.cmd_digital_in(1, inPin3) #FF0 (LSB FF, i.e. third FF) if D0 == '0': Q = '0' else: Q = '1' #FF1 (middle bit FF i.e. second FF) if D1 == '0': D0 = '0' else: D0 = '1' #FF2 (MSB FF i.e first FF) if D2 == '0': D1 = '0' else: D1 = '1' sleep(0.05) lastpinstate = pinstate def exit(self): self.obj_arduino.close_serial()
class D2B: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): #binary Outputs self.led1=10 #LSB self.led2=11 #middle bit self.led3=12 #MSB #decimal inputs self.Pin1=2 #decimal input 1 (LSB) self.Pin2=3 #decimal input 2 self.Pin3=4 #decimal input 3 self.Pin4=5 #decimal input 4 self.Pin5=6 #decimal input 5 self.Pin6=7 #decimal input 6 self.Pin7=8 #decimal input 7 (MSB) for _ in range(0,500): d1=self.obj_arduino.cmd_digital_in(1,self.Pin1) d2=self.obj_arduino.cmd_digital_in(1,self.Pin2) d3=self.obj_arduino.cmd_digital_in(1,self.Pin3) d4=self.obj_arduino.cmd_digital_in(1,self.Pin4) d5=self.obj_arduino.cmd_digital_in(1,self.Pin5) d6=self.obj_arduino.cmd_digital_in(1,self.Pin6) d7=self.obj_arduino.cmd_digital_in(1,self.Pin7) print (d1+" "+d2+" "+d3+" "+d4+" "+d5+" "+d6+" "+d7) #decimal input 0, binary output 000 if d1=='0' and d2=='0' and d3=='0' and d4=='0' and d5=='0' and d6=='0' and d7=='0': self.obj_arduino.cmd_digital_out(1,self.led1,0) self.obj_arduino.cmd_digital_out(1,self.led2,0) self.obj_arduino.cmd_digital_out(1,self.led3,0) sleep(0.1) #decimal input 1, binary output 001 elif d1=='1' and d2=='0' and d3=='0' and d4=='0' and d5=='0' and d6=='0' and d7=='0': self.obj_arduino.cmd_digital_out(1,self.led1,1) self.obj_arduino.cmd_digital_out(1,self.led2,0) self.obj_arduino.cmd_digital_out(1,self.led3,0) sleep(0.1) #decimal input 2, binary output 010 elif d2=='1' and d3=='0' and d4=='0' and d5=='0' and d6=='0' and d7=='0': self.obj_arduino.cmd_digital_out(1,self.led1,0) self.obj_arduino.cmd_digital_out(1,self.led2,1) self.obj_arduino.cmd_digital_out(1,self.led3,0) sleep(0.1) #decimal input 3, binary output 011 elif d3=='1' and d4=='0' and d5=='0' and d6=='0' and d7=='0': self.obj_arduino.cmd_digital_out(1,self.led1,1) self.obj_arduino.cmd_digital_out(1,self.led2,1) self.obj_arduino.cmd_digital_out(1,self.led3,0) sleep(0.1) #decimal input 4, binary output 100 elif d4=='1' and d5=='0' and d6=='0' and d7=='0': self.obj_arduino.cmd_digital_out(1,self.led1,0) self.obj_arduino.cmd_digital_out(1,self.led2,0) self.obj_arduino.cmd_digital_out(1,self.led3,1) sleep(0.1) #decimal input 5, binary output 101 elif d5=='1' and d6=='0' and d7=='0': self.obj_arduino.cmd_digital_out(1,self.led1,1) self.obj_arduino.cmd_digital_out(1,self.led2,0) self.obj_arduino.cmd_digital_out(1,self.led3,1) sleep(0.1) #decimal input 6, binary output 110 elif d6=='1' and d7=='0': self.obj_arduino.cmd_digital_out(1,self.led1,0) self.obj_arduino.cmd_digital_out(1,self.led2,1) self.obj_arduino.cmd_digital_out(1,self.led3,1) sleep(0.1) #decimal input 7, binary output 111 elif d7=='1': self.obj_arduino.cmd_digital_out(1,self.led1,1) self.obj_arduino.cmd_digital_out(1,self.led2,1) self.obj_arduino.cmd_digital_out(1,self.led3,1) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,self.led1,0) self.obj_arduino.cmd_digital_out(1,self.led2,0) self.obj_arduino.cmd_digital_out(1,self.led3,0) sleep(0.1) def exit(self): self.obj_arduino.close_serial()
class PIPO: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): pinstate='0' lastpinstate='0' D2='0' #MSB input D1='0' #middle bit input=MSB output D0='0' #LSB input=middle bit output Q='0' #LSB output #Parallel inputs inPin1=5 #LSB input inPin2=6 #middle bit inPin3=7 #MSB #Parallel output outPin1=9 #LSB = Q outPin2=10 #middle bit = D0 outPin3=11 #MSB = D1 #external clock pulse clockPin=2 for _ in range(0,100): #pin 9=Q=LSB output if Q=='0': self.obj_arduino.cmd_digital_out(1,outPin1,0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,outPin1,1) sleep(0.1) #pin 10=Q1=D0=middle bit output if D0=='0': self.obj_arduino.cmd_digital_out(1,outPin2,0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,outPin2,1) sleep(0.1) #pin 11=Q2=D1=MSB output if D1=='0': self.obj_arduino.cmd_digital_out(1,outPin3,0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,outPin3,1) sleep(0.1) #reads the state of clock pinstate=self.obj_arduino.cmd_digital_in(1,clockPin) #clock is common for all FFs #thus only 1 if statement for detecting positive edge of clock if pinstate!=lastpinstate: if pinstate=='1': #order of FFs: serial input-FF2-FF1-FF0 D0=self.obj_arduino.cmd_digital_in(1,inPin1) D1=self.obj_arduino.cmd_digital_in(1,inPin2) D2=self.obj_arduino.cmd_digital_in(1,inPin3) #FF0 (LSB FF, i.e. third FF) if D0=='0': Q='0' else: Q='1' #FF1 (middle bit FF i.e. second FF) if D1=='0': D0='0' else: D0='1' #FF2 (MSB FF i.e first FF) if D2=='0': D1='0' else: D1='1' sleep(0.05) lastpinstate=pinstate def exit(self): self.obj_arduino.close_serial()
class MUX: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.ledPin = 9 #Output #select lines self.aPin = 6 #input A (MSB) self.bPin = 7 #input B (LSB) #inputs self.Pin4 = 5 #input 4 (MSB) self.Pin3 = 4 #input 3 self.Pin2 = 3 #input 2 self.Pin1 = 2 #input 1 (LSB) for _ in range(0, 200): i4 = self.obj_arduino.cmd_digital_in(1, self.Pin4) #MSB input i3 = self.obj_arduino.cmd_digital_in(1, self.Pin3) i2 = self.obj_arduino.cmd_digital_in(1, self.Pin2) i1 = self.obj_arduino.cmd_digital_in(1, self.Pin1) #LSB input A = self.obj_arduino.cmd_digital_in( 1, self.aPin) #MSB select line input B = self.obj_arduino.cmd_digital_in( 1, self.bPin) #LSB select line input if A == '0' and B == '0': #input i1 is selected, output is same as i1 if i1 == '0': self.obj_arduino.cmd_digital_out(1, self.ledPin, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, self.ledPin, 1) sleep(0.1) elif A == '0' and B == '1': #input i2 is selected, output is same as i2 if i2 == '0': self.obj_arduino.cmd_digital_out(1, self.ledPin, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, self.ledPin, 1) sleep(0.1) elif A == '1' and B == '0': #input i3 is selected, output is same as i3 if i3 == '0': self.obj_arduino.cmd_digital_out(1, self.ledPin, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, self.ledPin, 1) sleep(0.1) elif A == '1' and B == '1': #input i4 is selected, output is same as i4 if i4 == '0': self.obj_arduino.cmd_digital_out(1, self.ledPin, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, self.ledPin, 1) sleep(0.1) else: print("Invalid input!") def exit(self): self.obj_arduino.close_serial()
class DEMUX: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.inPin=5 #Input #select lines self.aPin=6 #input A (MSB) self.bPin=7 #input B (LSB) #outputs self.ledPin4=11 #output 4 (MSB) self.ledPin3=10 #output 3 self.ledPin2=9 #output 2 self.ledPin1=8 #output 1 (LSB) for _ in range(0,500): i=self.obj_arduino.cmd_digital_in(1,self.inPin) #input A=self.obj_arduino.cmd_digital_in(1,self.aPin) #MSB select line input B=self.obj_arduino.cmd_digital_in(1,self.bPin) #LSB select line input print ("A= "+A+", B= "+B+", i= "+i) if i=='0': #all outputs will be zero irrespective of which one is selected self.obj_arduino.cmd_digital_out(1,self.ledPin1,0) self.obj_arduino.cmd_digital_out(1,self.ledPin2,0) self.obj_arduino.cmd_digital_out(1,self.ledPin3,0) self.obj_arduino.cmd_digital_out(1,self.ledPin4,0) sleep(0.1) elif i=='1': if A=='0' and B=='0': #input i is seen at first output pin (LSB) self.obj_arduino.cmd_digital_out(1,self.ledPin1,1) self.obj_arduino.cmd_digital_out(1,self.ledPin2,0) self.obj_arduino.cmd_digital_out(1,self.ledPin3,0) self.obj_arduino.cmd_digital_out(1,self.ledPin4,0) sleep(0.1) elif A=='0' and B=='1': #input i is seen at second output pin self.obj_arduino.cmd_digital_out(1,self.ledPin2,1) self.obj_arduino.cmd_digital_out(1,self.ledPin1,0) self.obj_arduino.cmd_digital_out(1,self.ledPin3,0) self.obj_arduino.cmd_digital_out(1,self.ledPin4,0) sleep(0.1) elif A=='1' and B=='0': #input i is seen at third output pin self.obj_arduino.cmd_digital_out(1,self.ledPin3,1) self.obj_arduino.cmd_digital_out(1,self.ledPin1,0) self.obj_arduino.cmd_digital_out(1,self.ledPin2,0) self.obj_arduino.cmd_digital_out(1,self.ledPin4,0) sleep(0.1) elif A=='1' and B=='1': #input i is seen at fourth output pin (MSB) self.obj_arduino.cmd_digital_out(1,self.ledPin4,1) self.obj_arduino.cmd_digital_out(1,self.ledPin1,0) self.obj_arduino.cmd_digital_out(1,self.ledPin2,0) self.obj_arduino.cmd_digital_out(1,self.ledPin3,0) sleep(0.1) def exit(self): self.obj_arduino.close_serial()
class COUNTER_wo_ff: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): pinstate='0' lastpinstate='0' clockPin=5 # Pulse to be counted #outputs Pin1=9 #LSB Pin2=10 #middle bit Pin3=11 #MSB i=0 a=0 b=0 c=0 for _ in range(0,500): pinstate=self.obj_arduino.cmd_digital_in(1,clockPin) #negative edge of clock pulse to FF1 if pinstate!=lastpinstate: if pinstate=='0': i+=1 else: pass sleep(0.05) else: pass lastpinstate=pinstate a=i%2 b=(i/2)%2 c=(i/4)%2 self.obj_arduino.cmd_digital_out(1,Pin1,a) #LSB self.obj_arduino.cmd_digital_out(1,Pin2,b) #middle bit self.obj_arduino.cmd_digital_out(1,Pin3,c) #MSB sleep(0.1) if i>7: i=0 else: pass def exit(self): self.obj_arduino.close_serial()
class MUX: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.ledPin=9 #Output #select lines self.aPin=6 #input A (MSB) self.bPin=7 #input B (LSB) #inputs self.Pin4=5 #input 4 (MSB) self.Pin3=4 #input 3 self.Pin2=3 #input 2 self.Pin1=2 #input 1 (LSB) for _ in range(0,200): i4=self.obj_arduino.cmd_digital_in(1,self.Pin4) #MSB input i3=self.obj_arduino.cmd_digital_in(1,self.Pin3) i2=self.obj_arduino.cmd_digital_in(1,self.Pin2) i1=self.obj_arduino.cmd_digital_in(1,self.Pin1) #LSB input A=self.obj_arduino.cmd_digital_in(1,self.aPin) #MSB select line input B=self.obj_arduino.cmd_digital_in(1,self.bPin) #LSB select line input if A=='0' and B=='0': #input i1 is selected, output is same as i1 if i1=='0': self.obj_arduino.cmd_digital_out(1,self.ledPin,0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,self.ledPin,1) sleep(0.1) elif A=='0' and B=='1': #input i2 is selected, output is same as i2 if i2=='0': self.obj_arduino.cmd_digital_out(1,self.ledPin,0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,self.ledPin,1) sleep(0.1) elif A=='1' and B=='0': #input i3 is selected, output is same as i3 if i3=='0': self.obj_arduino.cmd_digital_out(1,self.ledPin,0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,self.ledPin,1) sleep(0.1) elif A=='1' and B=='1': #input i4 is selected, output is same as i4 if i4=='0': self.obj_arduino.cmd_digital_out(1,self.ledPin,0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,self.ledPin,1) sleep(0.1) else: print ("Invalid input!") def exit(self): self.obj_arduino.close_serial()
class D_FF_edge: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.D='0' self.dPin=5 #assuming initial state: self.Q='0' self.Qbar='1' self.qPin=9 self.qbarPin=10 self.clockPin=2 #external clock self.pinstate='0' self.lastpinstate='0' for _ in range(0,500): if self.Q=='0': self.obj_arduino.cmd_digital_out(1,self.qPin,0) self.obj_arduino.cmd_digital_out(1,self.qbarPin,1) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,self.qPin,1) self.obj_arduino.cmd_digital_out(1,self.qbarPin,0) sleep(0.1) self.pinstate=self.obj_arduino.cmd_digital_in(1,self.clockPin) #Reads clock if self.pinstate!=self.lastpinstate: #Edge detection if(self.pinstate=='0'): #Negative edge self.D=self.obj_arduino.cmd_digital_in(1,self.dPin) #Reads input D if self.D=='0': self.Q='0' else: self.Q='1' sleep(0.05) self.lastpinstate=self.pinstate def exit(self): self.obj_arduino.close_serial()
class SR_FF: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.S='0' self.R='0' self.sPin=5 #Input S is given to Pin 5 self.rPin=6 #Input R is given to Pin 6 #assuming initial state: self.Q='0' self.Qbar='1' self.qPin=9 self.qbarPin=10 self.clockPin=2 #external clock for _ in range(0,500): if self.Q=='0': self.obj_arduino.cmd_digital_out(1,self.qPin,0) #Gives low output at Q elif self.Q=='1': self.obj_arduino.cmd_digital_out(1,self.qPin,1) #Gives high output at Q else: pass sleep(0.1) if self.Qbar=='0': self.obj_arduino.cmd_digital_out(1,self.qbarPin,0) #Gives low output at Qbar elif self.Qbar=='1': self.obj_arduino.cmd_digital_out(1,self.qbarPin,1) #Gives high output at Qbar else: pass sleep(0.1) self.S=self.obj_arduino.cmd_digital_in(1,self.sPin) #Reads the input S self.R=self.obj_arduino.cmd_digital_in(1,self.rPin) #Reads the input R if self.obj_arduino.cmd_digital_in(1,self.clockPin)=='1': if self.S=='0' and self.R=='1': self.Q='0' self.Qbar='1' elif self.S=='1' and self.R=='0': self.Q='1' self.Qbar='0' elif self.S=='1' and self.R=='1': #we assume this case doesn't occur self.Q='0' self.Qbar='0' else: pass def exit(self): self.obj_arduino.close_serial()
class T_FF_edge: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.T = '0' self.tPin = 5 #assuming initial state: self.Q = '0' self.Qbar = '1' self.qPin = 9 self.qbarPin = 10 self.clockPin = 2 #external clock self.pinstate = '0' self.lastpinstate = '0' for _ in range(0, 500): if self.Q == '0': self.obj_arduino.cmd_digital_out(1, self.qPin, 0) self.obj_arduino.cmd_digital_out(1, self.qbarPin, 1) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, self.qPin, 1) self.obj_arduino.cmd_digital_out(1, self.qbarPin, 0) sleep(0.1) self.pinstate = self.obj_arduino.cmd_digital_in( 1, self.clockPin) #Reads clock if self.pinstate != self.lastpinstate: #Edge detection if (self.pinstate == '0'): #negative edge self.T = self.obj_arduino.cmd_digital_in( 1, self.tPin) #Reads input T if self.T == '1': temp = self.Q self.Q = self.Qbar self.Qbar = temp else: pass sleep(0.05) self.lastpinstate = self.pinstate def exit(self): self.obj_arduino.close_serial()
class ADDER: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.sumPin = 9 #Sum self.coutPin = 10 #Carry out self.aPin = 5 #input A self.bPin = 6 #input B self.cPin = 3 #input Cin (Caryy in) for _ in range(0, 100): vala = self.obj_arduino.cmd_digital_in(1, self.aPin) print "A= " + vala valb = self.obj_arduino.cmd_digital_in(1, self.bPin) print "B= " + valb valc = self.obj_arduino.cmd_digital_in(1, self.cPin) print "Cin= " + valc #As acoording to the logic circuit of full adder #to get Pi: A XOR B if vala == '0' and valb == '0': P = '0' elif vala == '1' and valb == '1': P = '0' else: P = '1' #to get Gi: A AND B if vala == '1' and valb == '1': G = '1' else: G = '0' #to get Sum: Pi XOR Cin if P == '0' and valc == '0': self.obj_arduino.cmd_digital_out(1, self.sumPin, 0) sleep(0.1) elif P == '1' and valc == '1': self.obj_arduino.cmd_digital_out(1, self.sumPin, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, self.sumPin, 1) sleep(0.1) #To get Carry out #Pi AND Cin if P == '1' and valc == '1': temp = '1' else: temp = '0' # Gi OR temp if G == '0' and temp == '0': self.obj_arduino.cmd_digital_out(1, self.coutPin, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, self.coutPin, 1) sleep(0.1) def exit(self): self.obj_arduino.close_serial()
class JK_FF_edge: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.J='0' self.K='0' self.S='0' self.R='1' self.jPin=5 self.kPin=6 self.prePin=3 self.clrPin=4 #assuming initial state: self.Q='0' self.Qbar='1' self.qPin=9 self.qbarPin=10 self.clockPin=2 #external clock self.pinstate='0' self.lastpinstate='0' for _ in range(0,500): if self.Q=='0': self.obj_arduino.cmd_digital_out(1,self.qPin,0) #Gives low output at Q self.obj_arduino.cmd_digital_out(1,self.qbarPin,1) #Gives high output at Qbar sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,self.qPin,1) #Gives high output at Q self.obj_arduino.cmd_digital_out(1,self.qbarPin,0) #Gives low output at Qbar sleep(0.1) self.pre=self.obj_arduino.cmd_digital_in(1,self.prePin) #Reads preset input self.clr=self.obj_arduino.cmd_digital_in(1,self.clrPin) #Reads clear input if self.pre=='0' and self.clr=='1': self.S='1' self.R='0' self.Q='1' elif self.pre=='1' and self.clr=='0': self.S='0' self.R='1' self.Q='0' #Normal functioning when both are HIGH elif self.pre=='1' and self.clr=='1': self.pinstate=self.obj_arduino.cmd_digital_in(1,self.clockPin) #Reads clock input if self.pinstate!=self.lastpinstate: #edge detection if self.pinstate=='1': #Positive edge #MASTER #JK FF Code self.J=self.obj_arduino.cmd_digital_in(1,self.jPin) self.K=self.obj_arduino.cmd_digital_in(1,self.kPin) if self.J=='0' and self.K=='1': self.S='0' self.R='1' elif self.J=='1' and self.K=='0': self.S='1' self.R='0' elif self.J=='1' and self.K=='1': temp=self.S self.S=self.R self.R=temp else: pass else: pass if self.pinstate=='0': #SLAVE #JK FF code only for state 01 and 10 if self.S=='0' and self.R=='1': self.Q='0' elif self.S=='1' and self.R=='0': self.Q='1' else: pass else: pass sleep(0.05) self.lastpinstate=self.pinstate else: pass def exit(self): self.obj_arduino.close_serial()
class POT: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): self.pot=2 self.blue=9 self.green=10 self.red=11 l = 100 # length x = range(l) # x axis T = [420 for i in range(l)] # initial value for i in range(35): val=self.obj_arduino.cmd_analog_in(1,self.pot) print val if (int(val) >= 0 and int(val)< 320): self.obj_arduino.cmd_digital_out(1,self.blue,1) sleep(.25) self.obj_arduino.cmd_digital_out(1,self.blue,0) elif (int(val) >= 320 and int(val) < 900): self.obj_arduino.cmd_digital_out(1,self.green,1) sleep(.25) self.obj_arduino.cmd_digital_out(1,self.green,0) else: self.obj_arduino.cmd_digital_out(1,self.red,1) sleep(.25) self.obj_arduino.cmd_digital_out(1,self.red,0) plt.ion() plt.show() t = int(val) T.pop(0) # pop first value T.append(t) # push at the end keeping list of same size plt.title("Reading Potentiometer from Arduino...!") plt.grid(True) plt.yticks([0,100,200,300,400,500,600,700,800,900,1000,1100]) plt.xticks([0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100]) plt.ylabel('Potentiometer Readings') plt.legend(loc='upper left') plt.axis([0, l, 0.55 * min(T), 1.2 * max(T)]) plt.plot(x, T, linewidth=1) plt.draw() plt.pause(.0001) plt.clf() def exit(self): self.obj_arduino.close_serial()
class SR_FF_edge: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.S = '0' self.R = '0' self.sPin = 5 #Input S is given to Pin 5 self.rPin = 6 #Input R is given to Pin 6 #assuming initial state: self.Q = '0' self.Qbar = '1' self.qPin = 9 self.qbarPin = 10 self.clockPin = 2 #external clock self.pinstate = '0' self.lastpinstate = '0' for _ in range(0, 500): #Runs the program 500 times in a loop if self.Q == '0': self.obj_arduino.cmd_digital_out(1, self.qPin, 0) #Gives low output at Q elif self.Q == '1': self.obj_arduino.cmd_digital_out(1, self.qPin, 1) #Gives high output at Q sleep(0.1) else: pass if self.Qbar == '0': self.obj_arduino.cmd_digital_out(1, self.qbarPin, 0) #Gives low output at Qbar elif self.Qbar == '1': self.obj_arduino.cmd_digital_out(1, self.qbarPin, 1) #Gives high output at Qbar sleep(0.1) else: pass self.pinstate = self.obj_arduino.cmd_digital_in(1, self.clockPin) if self.pinstate != self.lastpinstate: #edge detection if (self.pinstate == '0'): #negative edge self.S = self.obj_arduino.cmd_digital_in( 1, self.sPin) #Reads the input S self.R = self.obj_arduino.cmd_digital_in( 1, self.rPin) #Reads the input R if self.S == '0' and self.R == '1': self.Q = '0' self.Qbar = '1' elif self.S == '1' and self.R == '0': self.Q = '1' self.Qbar = '0' elif self.S == '1' and self.R == '1': #we assume this case doesn't occur self.Q = '0' self.Qbar = '0' else: pass sleep(0.05) self.lastpinstate = self.pinstate def exit(self): self.obj_arduino.close_serial()
class PISO: def __init__(self,baudrate): self.baudrate=baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino=Arduino() self.port=self.obj_arduino.locateport() self.obj_arduino.open_serial(1,self.port,self.baudrate) def run(self): pinstate='0' lastpinstate='0' sl='0' #shift/!load D2='0' #MSB input D1='0' #middle bit input=MSB output D0='0' #LSB input=middle bit output Q='0' #LSB output #Parallel inputs inPin1=5 #LSB input inPin2=6 #middle bit inPin3=7 #MSB #Serial output outPin=9 #LSB = Q #external clock pulse clockPin=2 #Shift/!Load input slPin=3 for _ in range(0,500): #pin 9=Q=LSB output if D0=='0': self.obj_arduino.cmd_digital_out(1,outPin,0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1,outPin,1) sleep(0.1) #reads the state of Shift/!Load sl=self.obj_arduino.cmd_digital_in(1,slPin) #reads the state of clock pinstate=self.obj_arduino.cmd_digital_in(1,clockPin) #clock is common for all FFs #thus only 1 if statement for detecting positive edge of clock if pinstate!=lastpinstate: if pinstate=='1': #order of FFs: serial input-FF2-FF1-FF0 if sl=='0': #parallel load mode D0=self.obj_arduino.cmd_digital_in(1,inPin1) D1=self.obj_arduino.cmd_digital_in(1,inPin2) D2=self.obj_arduino.cmd_digital_in(1,inPin3) else: #sl==1 i.e. serial shift mode #FF0 (LSB FF, i.e. third FF) if D0=='0': Q='0' else: Q='1' #FF1 (middle bit FF i.e. second FF) if D1=='0': D0='0' else: D0='1' #FF2 (MSB FF i.e first FF) if D2=='0': D1='0' else: D1='1' D2='0' sleep(0.05) lastpinstate=pinstate def exit(self): self.obj_arduino.close_serial()
class SUBTRACTER: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.diffPin = 9 #Difference self.boutPin = 10 #Borrow out self.aPin = 5 #input A self.bPin = 6 #input B self.binPin = 3 #input Bin (Borrow in) for _ in range(0, 100): vala = self.obj_arduino.cmd_digital_in(1, self.aPin) print "A= " + vala valb = self.obj_arduino.cmd_digital_in(1, self.bPin) print "B= " + valb valbin = self.obj_arduino.cmd_digital_in(1, self.binPin) print "Bin= " + valbin #As acoording to the logic circuit of full subtracter #First half subtracter #Difference #A XOR B if vala == '0' and valb == '0': fsdiff = '0' elif vala == '1' and valb == '1': fsdiff = '0' else: fsdiff = '1' #borrow out #A NOT if vala == '1': fsnot = '0' else: fsnot = '1' #B AND fsnot if valb == '1' and fsnot == '1': fsb = '1' else: fsb = '0' #second half subtacter #difference #fsdiff XOR Bin if fsdiff == '0' and valbin == '0': self.obj_arduino.cmd_digital_out(1, self.diffPin, 0) sleep(0.1) elif fsdiff == '1' and valbin == '1': self.obj_arduino.cmd_digital_out(1, self.diffPin, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, self.diffPin, 1) sleep(0.1) #borrow out #fsdiff NOT if fsdiff == '1': ssnot = '0' else: ssnot = '1' #Bin and ssnot if valbin == '1' and ssnot == '1': ssand = '1' else: ssand = '0' #ssand OR fsb if ssand == '0' and fsb == '0': self.obj_arduino.cmd_digital_out(1, self.boutPin, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, self.boutPin, 1) sleep(0.1) def exit(self): self.obj_arduino.close_serial()
class SIPO: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): pinstate = '0' lastpinstate = '0' clockPin = 5 # Pulse to be counted D2 = '0' #serial data input, input by user is given to FF2 (MSB FF) #output of FF2=input of FF1 D1 = '0' #D1=Q2, FF1 (middle bit FF), output of FF1=input of FF0 D0 = '0' #D0=Q1, FF0 (LSB FF), output of FF0 = Q0 Q = '0' #output of FF0 #Serial input inPin = 5 #Parallel output outPin1 = 9 #LSB = Q outPin2 = 10 #middle bit outPin3 = 11 #MSB #external clock pulse clockPin = 2 for _ in range(0, 100): #pin 9=Q=LSB if D0 == '0': self.obj_arduino.cmd_digital_out(1, outPin1, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, outPin1, 1) sleep(0.1) #pin 10=Q1=D0=middle bit if D1 == '0': self.obj_arduino.cmd_digital_out(1, outPin2, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, outPin2, 1) sleep(0.1) #pin 11=Q2=D1=MSB if D2 == '0': self.obj_arduino.cmd_digital_out(1, outPin3, 0) sleep(0.1) else: self.obj_arduino.cmd_digital_out(1, outPin3, 1) sleep(0.1) #reads the state of clock pinstate = self.obj_arduino.cmd_digital_in(1, clockPin) #clock is common for all FFs #thus only 1 if statement for detecting positive edge of clock if pinstate != lastpinstate: if pinstate == '1': #order of FFs: serial input-FF2-FF1-FF0 #FF0 (LSB FF, i.e. third FF) if D0 == '0': Q = '0' else: Q = '1' #FF1 (middle bit FF i.e. second FF) if D1 == '0': D0 = '0' else: D0 = '1' #FF2 (MSB FF i.e first FF) if D2 == '0': D1 = '0' else: D1 = '1' D2 = self.obj_arduino.cmd_digital_in( 1, inPin) #input is given to D of FF2 (MSB FF) sleep(0.05) lastpinstate = pinstate def exit(self): self.obj_arduino.close_serial()
class JK_FF: def __init__(self, baudrate): self.baudrate = baudrate self.setup() self.run() self.exit() def setup(self): self.obj_arduino = Arduino() self.port = self.obj_arduino.locateport() self.obj_arduino.open_serial(1, self.port, self.baudrate) def run(self): self.J = '0' self.K = '0' self.jPin = 5 self.kPin = 6 self.prePin = 3 self.clrPin = 4 #assuming initial state: self.Q = '0' self.Qbar = '1' self.qPin = 9 self.qbarPin = 10 self.clockPin = 2 #external clock for _ in range(0, 500): if self.Q == '0': self.obj_arduino.cmd_digital_out(1, self.qPin, 0) #Gives low output at Q sleep(0.05) elif self.Q == '1': self.obj_arduino.cmd_digital_out(1, self.qPin, 1) #Gives high output at Q sleep(0.05) else: pass if self.Qbar == '0': self.obj_arduino.cmd_digital_out(1, self.qbarPin, 0) #Gives low output at Qbar sleep(0.05) elif self.Qbar == '1': self.obj_arduino.cmd_digital_out(1, self.qbarPin, 1) #Gives high output at Qbar sleep(0.05) else: pass self.J = self.obj_arduino.cmd_digital_in( 1, self.jPin) #Reads the input J self.K = self.obj_arduino.cmd_digital_in( 1, self.kPin) #Reads the input K self.pre = self.obj_arduino.cmd_digital_in( 1, self.prePin) #Reads the input Preset self.clr = self.obj_arduino.cmd_digital_in( 1, self.clrPin) #Reads the input Clear if self.pre == '0' and self.clr == '1': self.Q = '1' self.Qbar = '0' elif self.pre == '1' and self.clr == '0': self.Q = '0' self.Qbar = '1' #Preset and clear are active low inputs, thus normal functioning of flip flop happens when both are HIGH elif self.pre == '1' and self.clr == '1': if self.obj_arduino.cmd_digital_in(1, self.clockPin) == '1': if self.J == '0' and self.K == '1': self.Q = '0' self.Qbar = '1' elif self.J == '1' and self.K == '0': self.Q = '1' self.Qbar = '0' elif self.J == '1' and self.K == '1': #toggle state temp = self.Q self.Q = self.Qbar self.Qbar = temp else: pass def exit(self): self.obj_arduino.close_serial()