Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
スケッチ: "Arduino LeonardoによるBluetoothシリアル制御のLチカ" Arduinoのシリアル通信 および Bluetoothによるシリアル通信などから制御可能。 (0 = LED消灯, 1 = LED点灯, 2 = お遊び) http://masanoriprog.blogspot.jp/2013/08/arduino-bluetooth-serial-led.html
#include <SoftwareSerial.h>
#define LED_PORT 13
#define BT_RX 11
#define BT_TX 10
// TX(RBT-001) <-> 11(Arduino)
#define PWM_WIDTH 500
// roop = 2ms
SoftwareSerial btSerial(BT_RX, BT_TX);
int mode = 0;
int flag1 = 0;
int flag2 = 100;
void setup()
{
// Initialize LED port
pinMode(LED_PORT, OUTPUT);
digitalWrite(LED_PORT, LOW);
// Initialize internal serial port (with PC)
Serial.begin(9600);
// Initialize software serial port (with Bluetooth)
btSerial.begin(9600);
}
void loop()
{
char c;
// Read from software serial port (with Bluetooth)
if (btSerial.available()){
c = btSerial.read();
Serial.write( c );
}
// Read from internal serial port (with PC)
if (Serial.available()){
c = Serial.read();
btSerial.write(c);
}
switch(c){
case '0':
Serial.write("Led = disabled\n");
mode = 0;
break;
case '1':
Serial.write("Led = enabled\n");
mode = 1;
break;
case '2':
Serial.write("Led = enabled (2)\n");
mode = 2;
}
switch(mode){
case 0:
digitalWrite(LED_PORT, LOW);
break;
case 1:
digitalWrite(LED_PORT, HIGH);
break;
case 2:
/* Omake (PWM-like) */
if( flag1 < flag2 ){
digitalWrite(LED_PORT, LOW);
} else if (PWM_WIDTH < flag1){
flag1 = 0;
flag2 += 10;
if(PWM_WIDTH < flag2){
flag2 = 0;
}
} else {
digitalWrite(LED_PORT, HIGH);
}
flag1++;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.