def codice_errato(): print("Codice errato") #display.display_access(0) pwm.write(pin_buzzer,10000,5000,MICROS) sleep(500) pwm.write(pin_buzzer,0,0,MICROS) sleep(2000)
def impostaStatoTre(): print("Passaggio di stato: " + str(state) + " -> 3" ) global state state = 3 pwm.write(Serratura,PERIOD,SERRATURA_SERVO_APERTO,MICROS) timer_porta = timers.timer() timer_porta.start()
def pwm_control(): global duty duty= duty+10 pwm.write(pwmPin, 100, duty,MICROS) if duty>=100: duty=0 print("Duty:", duty, "millis")
def buzzerTrigger(pinBuzzer=D22.PWM, frequency=2000): print("Buzzer ON") pinMode(pinBuzzer, OUTPUT) period = 1000000 // frequency duty = period // 2 pwm.write(pinBuzzer, period, duty, MICROS) sleep(1000) pwm.write(pinBuzzer, 0, 0, 0)
def attach(self): """ .. method:: attach() Writes **default_width** to the servo associated PWM, enabling the motor and moving it to the default position. """ pwm.write(self.pin, self.period, self.defaultPosition, MICROS)
def detach(self): """ .. method:: detach() Writes 0 to the servo associated PWM disabling the motor. """ pwm.write(self.pin, self.period, 0, MICROS)
def codice_corretto(): global access_granted access_granted = True print("Codice corretto") #Suono apertura corretta pwm.write(pin_buzzer,2000,1000,MICROS) sleep(500) pwm.write(pin_buzzer,0,0,MICROS)
def moveToDegree(self,degree): width=int(self.map_range(degree,0,180,self.minWidth,self.maxWidth)) if width != self.currentPosition: self.currentPosition=width pwm.write(self.pin, self.period,self.currentPosition,MICROS) """
def __init__(self,pin,min_width=500,max_width=2500,default_width=1500,period=20000): self.pin=pin self.minWidth=min_width self.maxWidth=max_width self.defaultPosition=default_width self.currentPosition=default_width self.period=period pwm.write(self.pin,self.period,0,MICROS)
def impostaStatoQuattro(): print("Passaggio di stato: " + str(state) + " -> 4" ) global lock_requested global state state = 4 lock_requested = False #display.display_door_closing() pwm.write(Porta,PERIOD,PORTA_SERVO_CHIUSO,MICROS)
def set_intensity( self, percent ): """ .. method:: set_inensity( percent) Set the intensity of the led display from 0 to 100 percent. """ new_period = ( percent / 100.0 ) * 10 try: pwm.write( self.pwm, 10, int(new_period) ) except Exception as e: return e
def sendRaw(self,x): state = 1 for pulse in x: npulse=pulse//self.period #print("npulse",npulse) if state: pwm.write(self.senderPin,self.period,self.duty,MICROS,npulse) else: pwm.write(self.senderPin,self.period,0,MICROS) sleep(pulse,MICROS) #to be changed with new pwm strategy state = state^1
def __init__(self, pin, client): self._pin = pin pinMode(self._pin, OUTPUT) self._client = client self._period = 20000 # the positions (in pulse width) to which the motor has to turn # in order to cover/uncover the device self._cover_pw = 1700 self._uncover_pw = 700 # starts in "uncover" position by default self._position = self._uncover_pw pwm.write(self._pin, self._period, self._uncover_pw, MICROS)
def moveToPulseWidth(self,width): if width< self.minWidth: width=self.minWidth elif width > self.maxWidth: width=self.maxWidth if width != self.currentPosition: self.currentPosition=int(width) pwm.write(self.pin, self.period,self.currentPosition,MICROS) """
def buzz(input_for_period,input_for_length,buzzer_pin): while True: # Typical piezoelectric buzzer frequencies range from 500-4000Hz, so period has to range from 250 us to 2000 us period = helpers.map_range(input_val[input_for_period],1,1000,250,2000) # Set the period of the buzzer and the duty to 50% of the period through pwm.write # pwm.write is the correct way to use pwm in VIPER. It is similar at analogWrite in Arduino Wiring, but sounds better # Note that in pwm.write we will use MICROS so every sec is 1000000 micros # // is the int division, pwm.write period doesn't accept floats pwm.write(buzzer_pin,period,period//2,MICROS) # Set the length of the sleep to create a "beat" effect (from 1 to 300 ms). The default time unit of sleep function is MILLIS length = helpers.map_range(input_val[input_for_length],1,1000,1,300) sleep(length)
def buzz(input_for_period,input_for_length,buzzer_pin): while True: # Typical piezoelectric buzzer frequencies range from 500-4000Hz, so period has to range from 250 us to 2000 us period = helpers.map_range(input_val[input_for_period],1,1000,250,2000) # Set the period of the buzzer and the duty to 50% of the period through pwm.write # pwm.write is the correct way to use pwm in Zerynth. It is similar at analogWrite in Arduino Wiring, but sounds better # Note that in pwm.write we will use MICROS so every sec is 1000000 micros # // is the int division, pwm.write period doesn't accept floats pwm.write(buzzer_pin,period,period//2,MICROS) # Set the length of the sleep to create a "beat" effect (from 1 to 300 ms). The default time unit of sleep function is MILLIS length = helpers.map_range(input_val[input_for_length],1,1000,1,300) sleep(length)
def impostaStatoUno(): print("Passaggio di stato: " + str(state) + " -> 1" ) global access_granted global state global lock_requested state = 1 access_granted = False lock_requested = False #display.display_password_prompt() pwm.write(Serratura,PERIOD,SERRATURA_SERVO_CHIUSO,MICROS)
def sendRaw(self, x): state = 1 for pulse in x: npulse = pulse // self.period #print("npulse",npulse) if state: pwm.write(self.senderPin, self.period, self.duty, MICROS, npulse) else: pwm.write(self.senderPin, self.period, 0, MICROS) sleep(pulse, MICROS) #to be changed with new pwm strategy state = state ^ 1
def impostaStatoDue(): print("Passaggio di stato: " + str(state) + " -> 2" ) global state timer_tastierino.clear() state = 2 #display.display_access(1) pwm.write(Serratura,PERIOD,SERRATURA_SERVO_APERTO,MICROS) timer_serratura = timers.timer() timer_serratura.start() #timer_serratura.one_shot(TEMPO_SERRATURA_CHIUSURA,notifica_tempo_serratura) print('Timer attivato')
def moveToDegree(self, degree): """ .. method:: moveToDegree(degree) Moves the servo motor to the desired position expressed in degrees (float). """ width = int( self.map_range(degree, 0, 180, self.minWidth, self.maxWidth)) if width != self.currentPosition: self.currentPosition = width pwm.write(self.pin, self.period, self.currentPosition, MICROS)
def __init__(self, pin, min_width=500, max_width=2500, default_width=1500, period=20000): self.pin = pin self.minWidth = min_width self.maxWidth = max_width self.defaultPosition = default_width self.currentPosition = default_width self.period = period pwm.write(self.pin, self.period, 0, MICROS)
def moveToPulseWidth(self, width): """ .. method:: moveToPulseWidth(width) Moves the servo motor to the desired position expressed as pulse width (int) microseconds. The input has to be in min_width:max_width range. """ if width < self.minWidth: width = self.minWidth elif width > self.maxWidth: width = self.maxWidth if width != self.currentPosition: self.currentPosition = int(width) pwm.write(self.pin, self.period, self.currentPosition, MICROS)
def sendRaw(self,x): """ .. method:: sendRaw(data) Sends raw data by taking as input a list of pulses duration in microseconds. The first represents the duration of IR firing phase (state 1) while the the second is the IR LED OFF phase (state 0) and so on. """ state = 1 for pulse in x: npulse=pulse//self.period #print("npulse",npulse) if state: pwm.write(self.senderPin,self.period,self.duty,MICROS,npulse) else: hwtimers.sleep_micros(pulse) state = state^1
def sendRaw(self, x): """ .. method:: sendRaw(data) Sends raw data by taking as input a list of pulses duration in microseconds. The first represents the duration of IR firing phase (state 1) while the the second is the IR LED OFF phase (state 0) and so on. """ state = 1 for pulse in x: npulse = pulse // self.period #print("npulse",npulse) if state: pwm.write(self.senderPin, self.period, self.duty, MICROS, npulse) else: hwtimers.sleep_micros(pulse) state = state ^ 1
def main(): global val samples = [] pwm.init() for sample in PeakMonitor(SINK_NAME, METER_RATE): # samples range from 0 to 127 scaled_sample = (sample - 20)/40.0 scaled_sample = min(1.0, max(0.0, scaled_sample)) # Filter out crackles and other spikes samples.append(scaled_sample) samples = samples[-FILTER_LENGTH:] calc = samples[:] calc.sort() scaled_sample = sum(calc[0:FILTER_GOOD]) / (1.0 * FILTER_GOOD) val = val * ((SMOOTHING-1)/SMOOTHING) + scaled_sample * (1/SMOOTHING) pwm.write(val)
def play(self,pin,callback=None,restart=False): """ .. method:: play(pin,callback=None,restart=False) Start playing the melody actuating the PWM on the selected pin. It is also possible to pass a function as callback that will be called every time a note is played. The callback passes the played note to the called function. Moreover, loop play is also possible by setting the restart parameter to True. * pin: Dx.PWM, it is the pin where the buzzer is connected. * callback: the function to be called every time a note is played. Played note will be passed to the called function. * restart: it activates the playloop. """ self.stopped=False onebeat = 60000//(self.tempo//4) note1 = 0 if restart: note1=self.lastnote for i in range(note1,len(self.notes)): if self.stopped: self.lastnote=i return if callback: callback(self.notes[i]) if tunes[self.notes[i]]==0: duty=0 else: freq = 1000000//tunes[self.notes[i]] duty=freq//2 pwm.write(pin,freq,duty,MICROS) dur=onebeat//self.times[i] sleep(dur) self.stopped=True pwm.write(pin,freq,0,MICROS)
def play(self, pin, callback=None, restart=False): """ .. method:: play(pin,callback=None,restart=False) Starts playing the melody actuating the PWM on the selected pin. It is also possible to pass a function as callback that will be called every time a note is played. The callback passes the played note to the called function. Moreover, loop play is also possible by setting the restart parameter to True. * pin: Dx.PWM, it is the pin where the buzzer is connected. * callback: the function to be called every time a note is played. Played note will be passed to the called function. * restart: it activates the playloop. """ self.stopped = False onebeat = 60000 // (self.tempo // 4) note1 = 0 if restart: note1 = self.lastnote for i in range(note1, len(self.notes)): if self.stopped: self.lastnote = i return if callback: callback(self.notes[i]) if tunes[self.notes[i]] == 0: duty = 0 else: freq = 1000000 // tunes[self.notes[i]] duty = freq // 2 pwm.write(pin, freq, duty, MICROS) dur = onebeat // self.times[i] sleep(dur) self.stopped = True pwm.write(pin, freq, 0, MICROS)
def playSong(self): while (True): self.playMusic.wait() self.lock.acquire() i = 0 while self.playMusic.is_set() and i < len(musicSheets): note, size, dotted = musicSheets[i] if dotted != True: pwm.write(self.buzzerPin, note, note // 2, MICROS) sleep(int(size * self.length)) else: pwm.write(self.buzzerPin, note, note // 2, MICROS) sleep(int(1 / 2 * size * self.length)) pwm.write(self.buzzerPin, 0, 0, MICROS) sleep(int(1 / 2 * size * self.length)) i += 1 i %= len(musicSheets) pwm.write(self.buzzerPin, 0, 0, MICROS) self.lock.release()
def playTurnOff(self): self.playMusic.clear() self.lock.acquire() if not self.modeHandler.muted: p = self.highNotePeriod pwm.write(self.buzzerPin, p, p // 2, MICROS) sleep(200) p = self.lowNotePeriod pwm.write(self.buzzerPin, p, p // 2, MICROS) sleep(300) pwm.write(self.buzzerPin, 0, 0, MICROS) self.lock.release()
def playTurnOn(self): self.playMusic.clear() self.lock.acquire() # Imposto il periodo del buzzer ed il duty cycle a 50% if not self.modeHandler.muted: p = self.lowNotePeriod pwm.write(self.buzzerPin, p, p // 2, MICROS) sleep(200) p = self.highNotePeriod pwm.write(self.buzzerPin, p, p // 2, MICROS) sleep(300) pwm.write(self.buzzerPin, 0, 0, MICROS) self.lock.release()
def attach(self): pwm.write(self.pin,self.period,self.defaultPosition,MICROS) """
def detach(self): pwm.write(self.pin,self.period,0,MICROS) """
def buzz(): pwm.write(toishield.buzzer_pin,2272,1000,MICROS,440)
def changeLEDIntensity(obj): global led_pin percentage = 1 - obj.currentSample() print(int(2040 * percentage)) #~ 490 Hz pwm.write(led_pin,2040,int(2040*percentage),MICROS)
# Created by VIPER Team 2015 CC # Authors: G. Baldi, D. Mazzei ################################################################################ import streams import pwm #create a serial port stream with default parameters streams.serial() # the pin where the buzzer is attached to BUZZER =D8.PWM pinMode(BUZZER,OUTPUT) #declare pin 'BUZZER' to be an output: frequency=100 #define a variable to be incremented for changing the played tone frequency while True: period=1000000//frequency #we are using MICROS so every sec is 1000000 of micros. // is the int division, pwm.write period doesn't accept floats print("frequency is", frequency,"Hz") #set the period of the buzzer and the duty to 50% of the period pwm.write(BUZZER,period,period//2,MICROS) # increment the frequency every loop frequency = frequency + 20 # reset period if frequency >= 5000: frequency=100 sleep(100)
def buzz(): pwm.write(toishield.buzzer_pin, 2272, 1000, MICROS, 440)
def _move_to(self, pulse): """ Turns the motor to the desired position expressed as pulse width. """ pwm.write(self._pin, self._period, pulse, MICROS) self._position = pulse
pwmPin=A4.PWM #On Particle boards #set the pin as input with PullUp, the button will be connected to ground pinMode(buttonPin, INPUT_PULLUP) #define a function for printing capture results on the serial port def print_results(y): print("Time ON is:", y[0],"micros") print("Time OFF is:",y[1],"micros") print("Period is:", y[0]+y[1], "micros") print() #define a global variable for PWM duty cycle and turn on the PWM on board LED (Pin 13) duty=10 pwm.write(pwmPin,100, duty,MICROS) #pwm.write needs (pn, period, duty, time_unit) #define the function to be called for changing the PWM duty when the button is pressed def pwm_control(): global duty duty= duty+10 pwm.write(pwmPin, 100, duty,MICROS) if duty>=100: duty=0 print("Duty:", duty, "millis") #Attach an interrupt on the button pin waiting for signal going from high to low when the button is pressed. #The interrupt if triggered call the pwm_control function onPinFall(buttonPin, pwm_control) while True:
#In the example a frequency ramp going from 100 Hz to 5 KHz is generated as drive. #The frequency is converted in period to be used as input of the pwm.rite function that require period and pulse to be expressed in milli or micro seconds (measure unit can be selected as extra parameter of the pwm.write function). #The PWM duty cycle is set to 50% driving the buzzer with a symmetric square wave. import streams import pwm #create a serial port stream with default parameters streams.serial() # the pin where the buzzer is attached to buzzerpin = D22.PWM pinMode(buzzerpin, OUTPUT) #set buzzerpin to output mode frequency = 100 #define a variable to hold the played tone frequency while True: period = 1000000 // frequency #we are using MICROS so every sec is 1000000 of micros. // is the int division, pwm.write period doesn't accept floats print("frequency is", frequency, "Hz") #set the period of the buzzer and the duty to 50% of the period pwm.write(buzzerpin, period, period // 2, MICROS) # increment the frequency every loop frequency = frequency + 20 # reset period if frequency >= 5000: frequency = 100 sleep(500)
# Created by VIPER Team 2015 CC # Authors: G. Baldi, D. Mazzei ################################################################################ import streams import pwm #create a serial port stream with default parameters streams.serial() # the pin where the buzzer is attached to BUZZER = D8.PWM pinMode(BUZZER, OUTPUT) #declare pin 'BUZZER' to be an output: frequency = 100 #define a variable to be incremented for changing the played tone frequency while True: period = 1000000 // frequency #we are using MICROS so every sec is 1000000 of micros. // is the int division, pwm.write period doesn't accept floats print("frequency is", frequency, "Hz") #set the period of the buzzer and the duty to 50% of the period pwm.write(BUZZER, period, period // 2, MICROS) # increment the frequency every loop frequency = frequency + 20 # reset period if frequency >= 5000: frequency = 100 sleep(100)
import streams # import pwm for testing import pwm # CONNECT pin D3 to PIN D2 for this example to work! streams.serial() def on_touch_up(): print("touched UP") def on_touch_dn(): print("touched DN") try: # D2 will call touch_up on rise and touch_dn on fall with different debounce times onPinRise(D2, on_touch_up, debounce=500) onPinFall(D2, on_touch_dn, debounce=300) except Exception as e: print(e) while True: for x in [100, 200, 300, 400, 500, 600, 700, 800, 900]: print("--->", x, 1000 - x) # start pwm on D3 with the current period pwm.write(D3.PWM, 1000, x) # now wait and check if debounce is working sleep(5000)
# cw02_led_pwm # Created at 2019-06-30 16:31:14.480855 import pwm duty = 0 pinMode(D26, OUTPUT) while True: for i in range(-100, 100, 1): duty = 100 - abs(i) pwm.write(D26.PWM, 100, duty, MICROS) sleep(10)
def changeLEDIntensity(obj): global led_pin percentage = 1 - obj.currentSample() print(int(2040 * percentage)) #~ 490 Hz pwm.write(led_pin, 2040, int(2040 * percentage), MICROS)