const int analogOutPin = 9; // Analog output pin that the LED is attached to
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
pinMode(analogOutPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
String inString;
while(Serial.available()>0)
{
// 读取一个字符
int inChar = Serial.read();
// 判定是不是数值字符(0~9)
// [注意]该判定方法组会导致只提取字符串中的所有数字并将其构成一个数字字符序列
if (isDigit(inChar)) {
// 添加到字符串中(byte转换为char类型)
inString+= (char)inChar;
outputValue=inString.toInt();
}
}
analogWrite(analogOutPin, outputValue);
Serial.println(outputValue);
delay(1000);
}
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
pinMode(analogOutPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
String inString;
while(Serial.available()>0)
{
// 读取一个字符
int inChar = Serial.read();
// 判定是不是数值字符(0~9)
// [注意]该判定方法组会导致只提取字符串中的所有数字并将其构成一个数字字符序列
if (isDigit(inChar)) {
// 添加到字符串中(byte转换为char类型)
inString+= (char)inChar;
outputValue=inString.toInt();
}
}
analogWrite(analogOutPin, outputValue);
Serial.println(outputValue);
delay(1000);
}
举报