Last active
October 13, 2021 14:34
-
-
Save KeitetsuWorks/7d2948d30125dfe8e39f8492a5f16c80 to your computer and use it in GitHub Desktop.
Simple LED Switch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file arduino.ino | |
* @brief LED Controller | |
* @author Keitetsu | |
* @date 2021/10/13 | |
* @copyright Copyright (c) 2021 Keitetsu | |
* @par License | |
* This software is released under the MIT License. | |
*/ | |
#define LED_PIN 13 /**< LEDを接続したデジタルピン */ | |
#define LED_OFF LOW /**< LEDはLOWで消灯 */ | |
#define LED_ON HIGH /**< LEDはHIGHで点灯 */ | |
#define CMD_LEN 3 /**< Processingから受信するコマンドの長さ */ | |
/** | |
* @brief セットアップ関数 | |
*/ | |
void setup() | |
{ | |
Serial.begin(9600); | |
// LEDの初期化 | |
pinMode(LED_PIN, OUTPUT); | |
digitalWrite(LED_PIN, LED_OFF); | |
} | |
/** | |
* @brief ループ関数 | |
*/ | |
void loop() | |
{ | |
uint8_t rx_buf[CMD_LEN]; | |
// シリアル通信の受信処理 | |
if (Serial.available() >= CMD_LEN) | |
{ | |
Serial.readBytes(rx_buf, CMD_LEN); | |
if ((rx_buf[0] == 'L') && (rx_buf[1] == '1')) | |
{ | |
// 1 -> 点灯, 0 -> 消灯 | |
digitalWrite(LED_PIN, (rx_buf[2] == '1') ? LED_ON : LED_OFF); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
/// @file processing.pde | |
/// @brief Simple LED Switch GUI | |
/// @author Keitetsu | |
/// @date 2021/10/13 | |
/// @copyright Copyright (c) 2021 Keitetsu | |
/// @license This software is released under the MIT License. | |
/// | |
import processing.serial.*; | |
Serial comport; //< COMポート | |
int ledState; //< LED状態 | |
int prevLedState; //< 前回のLED状態 | |
final int LED_STATE_OFF = 0; //< LEDはOFF状態 | |
final int LED_STATE_ON = 1; //< LEDはON状態 | |
void setup() { | |
// ウィンドウサイズ | |
size(300,300); | |
// 背景色 | |
background(255, 253, 230); | |
// フォントサイズ | |
textSize(50); | |
// テキストは左右上下中央揃え | |
textAlign(CENTER,CENTER); | |
// 四角形の座標指定方法 | |
rectMode(CENTER); | |
// フレームレート | |
frameRate(30); | |
// LED状態を初期化 | |
ledState = LED_STATE_OFF; | |
prevLedState = LED_STATE_ON; | |
// COMポート名 (Windowsの場合は"COM1"など) | |
String selectedComportName = "/dev/ttyACM0"; | |
// COMポートを開く | |
comport = new Serial(this,selectedComportName,9600); | |
} | |
void draw() { | |
if (ledState != prevLedState) { // LED状態が変化したとき | |
switch(ledState) { | |
case LED_STATE_ON: | |
noStroke(); | |
fill(181, 230, 199); | |
ellipse(width / 2,height / 2,200,200); | |
fill(31, 30, 29); | |
text("ON",width / 2,height / 2); | |
comport.write("L11"); // LED1をON | |
break; | |
case LED_STATE_OFF: | |
default: | |
noStroke(); | |
fill(231, 37, 88); | |
ellipse(width / 2,height / 2,200,200); | |
fill(31, 30, 29); | |
text("OFF",width / 2,height / 2); | |
comport.write("L10"); // LED1をOFF | |
break; | |
} | |
prevLedState = ledState; // 前回のLED状態を更新 | |
} | |
} | |
void mousePressed() { | |
switch(ledState) { | |
case LED_STATE_OFF: | |
ledState = LED_STATE_ON; | |
break; | |
case LED_STATE_ON: | |
default: | |
ledState = LED_STATE_OFF; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment