前提・実現したいこと
arduinoのシリアル通信によるセンサの情報をグラフ化したい。
http://denshi.blog.jp/arduino/temperature-graph
このページを参考にして、同じようなものを作ってみました。
それをjupyter notebookで実行しました。
するとpygameは起動してURLのようにセンサの情報が表示されるようになりました。
しかしグラフがでてきません。
なにがいけないのでしょうか?またどこを改善すればグラフが表示されるようになりますか?
発生している問題・エラーメッセージ
pyagameは起動して温度が表示されるが、
URLのようにグラフがでてこない。
該当のソースコード
import numpy as np import matplotlib.pyplot as plt import pygame from pygame.locals import * import serial import sys def main(): ser = serial.Serial("COM3") # COMポート(Arduino接続) temps = [0]*100 # 温度格納 t = np.arange(0,100,1) plt.ion() pygame.init() # Pygameを初期化 screen = pygame.display.set_mode((1000, 1000)) # 画面作成(100×100) pygame.display.set_caption("傾斜角度") # タイトルバー font = pygame.font.Font(None, 50) # 文字の設定 while True: screen.fill((0,0,0)) # 画面のクリア temp = ser.readline().rstrip() # \nまで読み込む(\nは削除) temp = temp.decode() text = font.render(temp + "[C]", False, (255,255,255)) # 表示する文字の設定 screen.blit(text, (10, 10)) # レンダ,表示位置 pygame.display.flip() # 画面を更新して、変更を反映 # 温度データのリスト更新 temps.pop(99) temps.insert(0,float(temp)) # グラフ表示設定 line, = plt.plot(t, temps, 'r-',label="Temperature[C]") # Y軸更新 line.set_ydata(temps) plt.title("Real-time temperature") plt.xlabel("Time [s]") plt.ylabel("Temperature [Celsius]") plt.legend() plt.grid() plt.xlim([1,100]) plt.ylim([0,40]) plt.draw() plt.clf() for event in pygame.event.get(): # 終了ボタンが押されたら終了処理 if event.type == QUIT: pygame.quit() ser.close() plt.close() sys.exit() if __name__ == '__main__': main()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。