当前位置: 首页>>代码示例>>Python>>正文


Python Arduino.analogWrite方法代码示例

本文整理汇总了Python中arduino.Arduino.analogWrite方法的典型用法代码示例。如果您正苦于以下问题:Python Arduino.analogWrite方法的具体用法?Python Arduino.analogWrite怎么用?Python Arduino.analogWrite使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在arduino.Arduino的用法示例。


在下文中一共展示了Arduino.analogWrite方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import analogWrite [as 别名]

#.........这里部分代码省略.........
            ii += 1
        # then, we need to update duration of intersessions
        inter_pos = self.CS_config[:, 0] < 0  # take out the positions of intersessions, a boolean array
        inter_dur = self.CS_config[inter_pos, 0]
        self.CS_config[inter_pos, 0] = np.random.randint(40, 70, size=len(inter_dur))

        print(self.CS_config)
        self.duration = self.CS_config[:, 0].sum() / 60.0
        print("Total experimental time: %4.1f min" % self.duration)

    def blink(self, duration, freq=2):
        # blink the LED
        # duration: the duration of blinking
        nblink = int(duration * freq)
        thp = 1 / (freq * 2.0)  # half-period

        for ii in range(nblink):
            self.ard.setHigh(pin_LED)
            time.sleep(thp)
            self.ard.setLow(pin_LED)
            time.sleep(thp)

    def run_step(self, sblink=False):
        millis = int(round(time.time() * 1000))
        self.step += 1
        self.time_flag[self.step] = millis
        self.notify_observers(self.step)
        config_list = self.CS_config[self.step]
        print(config_list[0])
        # take the row
        # turn on or off all the stimuli

        # turn on or off the delivery
        if config_list[2] == 0:
            if self.infuse == True:
                new_era.stop_pump(self.pser, self.pump)
                self.infuse = False
        elif config_list[2] == 1:
            if self.infuse == False:
                new_era.set_rate(self.pser, self.pump, flowrate)
                new_era.run_all(self.pser)
                self.infuse = True

        if config_list[1] == 0:
            self.ard.setLow(pin_LED)  # keep the LED off
            time.sleep(config_list[0])
        elif config_list[1] == 1:
            if sblink:
                self.blink(config_list[0])
            else:
                self.ard.setHigh(pin_LED)
                time.sleep(config_list[0])

        # insert or retract the feeding tube

    #         if config_list[3] == 0:
    #             self.ard.analogWrite(pin_Servo, 0)
    #         elif config_list[3] == 0:
    #             self.ard.analogWrite(pin_Servo, 80)

    #         time.sleep(config_list[0])
    #         self.terminate_step()

    def terminate_step(self):
        # terminate a step

        self.ard.setLow(pin_LED)
        self.ard.analogWrite(pin_Servo, 0)
        new_era.stop_pump(self.pser, self.pump)

    def run_through(self, sblink=False):
        self.blink_start()
        self.step = -1
        ii = 0
        for ii in range(self.n_session):
            self.run_step(sblink)
            print("The" + str(ii) + "th session.")

    #     def shuffle_steps(self):
    #         # shuffle the steps of
    #         CS_total = np.inner(self.CS_config[:,0], self.CS_config[:,1]) # inner product of the first two columns, total duration of CS
    #         US_total = np.inner(self.CS_config[:,0], self.CS_config[:,2]) # total duration of US
    #         time_total = self.CS_config[:,0].sum() # The total amount of time should be equal
    #
    #         # use 10 seconds as a unit of the
    #         NC_chunk = np.round(CS_total/10.).astype('int')
    #         NU_chunk = np.round(US_total/10.).astype('int')
    #         Total_chunk = np.round(time_total/10.).astype('int')
    #
    #         shuff_LED = random_split(NC_chunk)
    #

    def close(self):
        # destruct the class
        self.ard.setLow(pin_LED)
        self.ard.close()

        np.save("../Data/" + self.fname, self.time_flag)
        pd.Series.to_csv(self.time_flag, "../Data/" + self.fname + ".csv")
        print("Arduino port closed. ")
开发者ID:danustc,项目名称:Behavior,代码行数:104,代码来源:LED_para.py

示例2: Arduino

# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import analogWrite [as 别名]
from arduino import Arduino
import time

b = Arduino('/dev/ttyUSB0')
pin = 9

#declare output pins as a list/tuple
b.output([pin])

brightness = 0
fadeAmount = 3

b.output([pin])

while (True):
    b.analogWrite(pin, brightness)
    time.sleep(0.05)
    print b.analogRead(pin)
    brightness = brightness + fadeAmount

    if (brightness == 0 or brightness == 255):
        fadeAmount = -fadeAmount
开发者ID:Bocin58,项目名称:Python-Arduino-Proto-API-v2,代码行数:24,代码来源:sample_analogwrite.py


注:本文中的arduino.Arduino.analogWrite方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。