def main(): # 操作するArduinoを決定 board = Arduino(Arduino.AUTODETECT) # サンプリング周期を10msに固定 board.samplingOn(10) # pin8 をアナログピンのinputに設定 digital_8 = set_pin(board, pin=8, mode="i", data="digital") time.sleep(2) while True: # センサから取得した値を0~1024(int)で出力 print(digital_8.read()) time.sleep(0.5) # アナログピンのサンプリングを終了する board.samplingOff()
class AnalogPrinter: def __init__(self): # sampling rate: 50Hz self.samplingRate = 100 self.timestamp = 0 self.board = Arduino(PORT) def start(self): self.board.analog[0].register_callback(self.myPrintCallback) self.board.samplingOn(1000 / self.samplingRate) self.board.analog[0].enable_reporting() def myPrintCallback(self, data): print("%f,%f" % (self.timestamp, data)) self.timestamp += (1 / self.samplingRate) def stop(self): self.board.samplingOff() self.board.exit()
class Reader: class Analog: def __init__(self, pin, _board): ### base ---------------- self._board = _board self.timestamp = 0 ### data----------------- self.pin = pin self.value = 0 self.callback = None ### --------------------- def print_value(self, data): print("%f,%f" % (self.timestamp, data)) self.timestamp += (1 / self.samplingRate) self.set_value(data) def set_value(self, data): self.value = data def __init__(self, port='/dev/ttyUSB0'): ### base ---------------------- self.port = port self.board = Arduino(self.port) ### data --------------------- self.analog = {} self.samplingRate = 10 ### setup -------------------- self.board.samplingOn(100 / self.samplingRate) ### void setup ------------------------------------------------------------- def start(self, pin=0, callback=None): self.analog[pin] = self.Analog(pin, self.board) if callback: self.analog[pin].callback = callback else: self.analog[pin].callback = self.analog[pin].set_value self.board.analog[pin].register_callback(self.analog[pin].callback) self.board.analog[pin].enable_reporting() ### void draw -------------------------------------------------------------- def stop(self): self.board.samplingOff() self.board.exit()
class AnalogPrinter: def __init__(self): # sampling rate: 10Hz self.samplingRate = 10 self.timestamp = 0 self.gas = 0 self.flama = 0 self.luz = 0 def start_gas(self): self.board = Arduino(PORT) self.board.analog[0].register_callback(self.myPrintCallback) self.board.samplingOn(1000 / self.samplingRate) self.board.analog[0].enable_reporting() def start_flama(self): self.board = Arduino(PORT) self.board.analog[1].register_callback(self.myPrintCallback_2) self.board.samplingOn(1000 / self.samplingRate) self.board.analog[1].enable_reporting() def start_luz(self): self.board = Arduino(PORT) self.board.analog[2].register_callback(self.myPrintCallback_3) self.board.samplingOn(1000 / self.samplingRate) self.board.analog[2].enable_reporting() def myPrintCallback(self, data): data1 = data * (5 / 1023) * 1000 self.timestamp += 1 / self.samplingRate if data1 <= 0.2: print(f"{round(self.timestamp, 2)}, {round(data1,2)} El valor del sensor es menor al muestreo",) self.board.digital[2].write(0) print("no hay gas") self.gas = 0 else: print(f"{round(self.timestamp, 2)}, {round(data1,2)} El valor del sensor es menor al muestreo",) self.board.digital[2].write(1) print("aqui hubo gas!") self.gas = 1 def myPrintCallback_2(self, data): data1 = data * (5 / 1023) * 1000 self.timestamp += 1 / self.samplingRate if data1 >= 2: print(f"{round(self.timestamp, 2)}, {round(data1,2)} El valor del sensor es mayor al muestreo",) self.board.digital[3].write(0) self.flama = 1 print("no hay fuego") else: print(f"{round(self.timestamp, 2)}, {round(data1,2)} El valor del sensor es menor al muestreo",) self.board.digital[3].write(1) print("fuego!!") self.flama = 1 def myPrintCallback_3(self, data): data1 = data * (5 / 1023) * 1000 self.timestamp += 1 / self.samplingRate if data1 >= 1: print(f"{round(self.timestamp, 2)}, {round(data1,2)} El valor del sensor es menor al muestreo",) self.board.digital[4].write(0) if self.gas == 1 : self.board.digital[2].write(1) if self.flama == 1: self.board.digital[3].write(1) if self.luz == 1: self.board.digital[4].write(1) if self.luz == 1 and self.gas == 1 and self.flama == 1: self.board.digital[5].write(1) else: print(f"{round(self.timestamp, 2)}, {round(data1,2)} El valor del sensor es mayor al muestreo",) self.board.digital[4].write(1) self.luz = 1 if self.gas == 1 : self.board.digital[2].write(1) if self.flama == 1: self.board.digital[3].write(1) if self.luz == 1 and self.gas == 1 and self.flama == 1: self.board.digital[5].write(1) def stop(self): self.board.samplingOff() self.board.exit()