Serial Data(Basic)- based on Blink Sketch:
Serial data can be read in several different formats- see https://www.arduino.cc/reference/en/language/functions/communication/serial/
Serial.parseInt reads in multi-digit integers from the serial buffer until a non-numeric character is seen, such as a comma or carriage return.
Int the following example, the blink rate can be controlled from the serial monitor. The same structure can be adapted to reading in data from other serial data sources, such as a bluetooth receiver.
Experiment with putting in various numbers of comma-separated integers.
Serial.parseInt reads in multi-digit integers from the serial buffer until a non-numeric character is seen, such as a comma or carriage return.
Int the following example, the blink rate can be controlled from the serial monitor. The same structure can be adapted to reading in data from other serial data sources, such as a bluetooth receiver.
Experiment with putting in various numbers of comma-separated integers.
int onTime = 200; //initial blink times
int offTime = 200;
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// parseInt reads in integers from the serial monitor, separated by commas or carriage return
if (Serial.available() > 0) {
onTime = Serial.parseInt(); //set blink on time
offTime =Serial.parseInt(); //set b,link off time
}
//blink built-in LED
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(onTime); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(offTime); // wait for a second
}
int offTime = 200;
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// parseInt reads in integers from the serial monitor, separated by commas or carriage return
if (Serial.available() > 0) {
onTime = Serial.parseInt(); //set blink on time
offTime =Serial.parseInt(); //set b,link off time
}
//blink built-in LED
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(onTime); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(offTime); // wait for a second
}