0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

概要

micropythonでshrike-lite、やってみた。
oscilloscope 見つけたので、やってみた。
ch1がadc0です。

参考にしたページ

写真

image.png

サンプルコード

import machine
import time

LED_PIN = 4
VIN_PIN = 26	   
start_time = time.ticks_ms()
temp_time = time.ticks_ms()
prev_led_state = False
time_value = 70
led = machine.Pin(LED_PIN, machine.Pin.OUT)
led.value(0)
adc = machine.ADC(VIN_PIN)
wdt = machine.WDT(timeout=4000)

class OSCObject:
	def __init__(self):
		self.ch1 = "null"
		self.ch2 = "null"
		self.msg = ""
	def float_to_string(self, flt: float, precision_num: int) -> str:
		fmt = "{:." + str(precision_num) + "f}"
		str_val = fmt.format(flt)
		return str_val.replace('.', ',')
	def set_ch1(self, val: float):
		self.ch1 = self.float_to_string(val, 2)
	def set_ch2(self, val: float):
		self.ch2 = self.float_to_string(val, 2)
	def build_packet(self):
		time_ms = str(time.ticks_ms())
		self.msg = f"T {time_ms} CH1 {self.ch1} CH2 {self.ch2} "
	def oscill_send(self):
		self.build_packet()
		print(self.msg)  

def reverse_led_state():
	"""LEDの状態を反転"""
	global prev_led_state
	prev_led_state = not prev_led_state
	led.value(1 if prev_led_state else 0)

def timer_blink_led(max_time: int):
	"""ノンブロッキングでLEDを点滅"""
	global temp_time
	if time.ticks_diff(time.ticks_ms(), temp_time) > max_time:
		temp_time = time.ticks_ms()
		reverse_led_state()

def get_sample(sample_count: int) -> float:
	"""アナログ入力の平均値を取得 (MicroPythonのADCは基本16bit: 0-65535)"""
	if sample_count <= 0:
		return 0.0
	total = 0
	for _ in range(sample_count):
		total += adc.read_u16()
	return total / sample_count

def get_voltage_dv() -> float:
	"""分圧回路からの電圧を計算"""
	measure_volt = get_sample(10)
	max_volt = 3.3  
	enc_max_val = 65535.0
	r1 = 60000.0
	r2 = 15000.0
	vs = (measure_volt * (r1 + r2)) / r2
	volt = vs * (max_volt / enc_max_val)
	return volt

while True:
	wdt.feed()
	osc = OSCObject()
	volt = get_voltage_dv()
	osc.set_ch1(volt)
	osc.oscill_send()
	timer_blink_led(250)
	time.sleep_ms(time_value)


以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?