Last active
November 30, 2017 14:29
-
-
Save YukinaMochizuki/5723efee57b575935acff85c6349bd3d to your computer and use it in GitHub Desktop.
This file contains 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
// 學校作業 | |
// 資工二B 鍾騰 | |
// | |
#include <SoftwareSerial.h> | |
const byte ledCount = 2; | |
char val; | |
byte ledPins[] = {5, 6}; | |
byte led1 = 0; | |
byte led2 = 0; | |
SoftwareSerial BT(8, 9); | |
//----------------unit---------------------- | |
void light11() { | |
if (led1 == 0) { | |
for (int fadeValue = 0 ; fadeValue <= 100; fadeValue += 5) { | |
analogWrite(ledPins[0], fadeValue); | |
delay(30); | |
led1 = 2; | |
} | |
} else if (led1 == 1) { | |
for (int fadeValue = 255 ; fadeValue >= 100; fadeValue -= 5) { | |
analogWrite(ledPins[0], fadeValue); | |
delay(30); | |
led1 = 2; | |
} | |
} | |
if (led2 == 0) { | |
for (int fadeValue = 0 ; fadeValue <= 100; fadeValue += 5) { | |
analogWrite(ledPins[1], fadeValue); | |
delay(30); | |
led2 = 2; | |
} | |
} else if (led2 == 1) { | |
for (int fadeValue = 255 ; fadeValue >= 100; fadeValue -= 5) { | |
analogWrite(ledPins[1], fadeValue); | |
delay(30); | |
led2 = 2; | |
} | |
} | |
} | |
void light(byte num1, byte num2) { | |
if (num1 == 1) { | |
if (led1 == 0) { | |
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { | |
analogWrite(ledPins[0], fadeValue); | |
delay(30); | |
led1 = 1; | |
} | |
}else if(led1 == 2){ | |
for (int fadeValue = 100 ; fadeValue <= 255; fadeValue += 5) { | |
analogWrite(ledPins[0], fadeValue); | |
delay(30); | |
led1 = 1; | |
} | |
} | |
} else if (num1 == 0) { | |
if (led1 == 1) { | |
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { | |
analogWrite(ledPins[0], fadeValue); | |
delay(30); | |
led1 = 0; | |
} | |
}else if(led1 == 2){ | |
for (int fadeValue = 100 ; fadeValue >= 0; fadeValue -= 5) { | |
analogWrite(ledPins[0], fadeValue); | |
delay(30); | |
led1 = 0; | |
} | |
} | |
} | |
if (num2 == 1) { | |
if (led2 == 0) { | |
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { | |
analogWrite(ledPins[1], fadeValue); | |
delay(30); | |
led2 = 1; | |
} | |
}else if(led2 == 2){ | |
for (int fadeValue = 100 ; fadeValue <= 255; fadeValue += 5) { | |
analogWrite(ledPins[1], fadeValue); | |
delay(30); | |
led2 = 1; | |
} | |
} | |
} else if (num2 == 0) { | |
if (led2 == 1) { | |
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { | |
analogWrite(ledPins[1], fadeValue); | |
delay(30); | |
led2 = 0; | |
} | |
}else if(led2 == 2){ | |
for (int fadeValue = 100 ; fadeValue >= 0; fadeValue -= 5) { | |
analogWrite(ledPins[1], fadeValue); | |
delay(30); | |
led2 = 0; | |
} | |
} | |
} | |
} | |
void slight(char num) { | |
if (num == 48) { | |
light11(); | |
} else if (num == 49) { | |
light(0, 1); | |
} else if (num == 50) { | |
light(1, 0); | |
} else if (num == 51) { | |
light(0, 0); | |
} | |
} | |
//------------------------------------------ | |
void setup() { | |
Serial.begin(9600); // 與電腦序列埠連線 | |
Serial.println("BT is ready!"); | |
BT.begin(9600); | |
} | |
void loop() { | |
// 若收到「序列埠監控視窗」的資料,則送到藍牙模組 | |
if (Serial.available()) { | |
val = Serial.read(); | |
BT.print(val); | |
} | |
// 若收到藍牙模組的資料,則送到「序列埠監控視窗」 | |
if (BT.available()) { | |
val = BT.read(); | |
Serial.println(val); | |
slight(val); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment