Created
December 24, 2015 08:56
-
-
Save uozias/6b771fab75503d1d6550 to your computer and use it in GitHub Desktop.
arduino gets values from serialport
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
int brightness = 0; // LEDの明るさを入れる | |
int fadeAmount = 5; // 明るさの変更スピードを設定 | |
int received = 0; // 受け取った値(asciiコード) | |
int final_value = 0; // 変換後の数字 | |
void setup() { | |
Serial.begin(9600); // シリアルポートを9600bpsで開く | |
pinMode(9, OUTPUT); //9番のピンを使う | |
} | |
void loop() { | |
while(Serial.available()){ | |
received= Serial.read(); // 明るさ(数字)をシリアル通信で受け取る | |
if (isDigit(received)) { | |
final_value=(final_value*10)+(received-48);} | |
else if(received==10){ | |
Serial.println(final_value); | |
if (final_value >= 255){ | |
final_value = 255; | |
} | |
if (final_value < 0){ | |
final_value = 0; | |
} | |
analogWrite(9, final_value); //明るさを設定 | |
final_value=0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment