Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ARR_SIZE 10
- double values[ARR_SIZE]; // float/doubles are the same on AVR8
- int index;
- int led = 4;
- int ledl = 5;
- int led3 = 3;
- int led6 = 6;
- int led7 = 7;
- int led10 = 10;
- int led8 = 8;
- // returns true when a new array of values has been
- // received and is ready for further processing.
- // Otherwise returns false.
- bool check_serial() {
- static bool is_receiving = false;
- while (Serial.available()) {
- digitalWrite(ledl, HIGH);
- String str = Serial.readStringUntil('\n');
- str.trim();
- if (str.length() == 0) {
- digitalWrite(led3, HIGH);
- continue; // Ignore empty lines.
- }
- else{
- digitalWrite(led10, HIGH);
- }
- if (str == "Start") {
- is_receiving = true;
- index = 0;
- digitalWrite(led6, HIGH);
- continue;
- }
- else{
- digitalWrite(led10, HIGH);
- }
- if (is_receiving) {
- digitalWrite(led, HIGH);
- values[index++] = atof(str.c_str());
- ++index;
- if (index >= 10) {
- digitalWrite(led7, HIGH);
- // We received the final value.
- is_receiving = false; // Reset the flag.
- index = 0;
- return true;
- }
- }
- else{
- digitalWrite(led10, HIGH);
- }
- }
- return false;
- }
- void setup() {
- Serial.begin(115200);
- pinMode(led, OUTPUT);
- pinMode(ledl, OUTPUT);
- pinMode(led3, OUTPUT);
- pinMode(led6, OUTPUT);
- pinMode(led7, OUTPUT);
- pinMode(led10, OUTPUT);
- pinMode(led8, OUTPUT);
- }
- void loop() {
- if (check_serial()) {
- // we received a new array of values. process them all now:
- // ...
- }
- else{
- digitalWrite(led8, HIGH);
- }
- // do other stuff..
- }
Advertisement
Add Comment
Please, Sign In to add comment