Guest User

Untitled

a guest
Oct 31st, 2023
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | Source Code | 0 0
  1. #define   ARR_SIZE   10
  2. double values[ARR_SIZE];    // float/doubles are the same on AVR8
  3. int index;
  4. int led = 4;
  5. int ledl = 5;
  6. int led3 = 3;
  7. int led6 = 6;
  8. int led7 = 7;
  9. int led10 = 10;
  10. int led8 = 8;
  11.  
  12. // returns true when a new array of values has been
  13. // received and is ready for further processing.
  14. // Otherwise returns false.
  15. bool check_serial() {
  16.     static bool is_receiving = false;
  17.  
  18.     while (Serial.available()) {
  19.       digitalWrite(ledl, HIGH);
  20.         String str = Serial.readStringUntil('\n');
  21.         str.trim();
  22.  
  23.         if (str.length() == 0) {
  24.             digitalWrite(led3, HIGH);
  25.            
  26.             continue;  // Ignore empty lines.
  27.         }
  28.         else{
  29.           digitalWrite(led10, HIGH);
  30.         }
  31.  
  32.         if (str == "Start") {
  33.             is_receiving = true;
  34.             index = 0;
  35.             digitalWrite(led6, HIGH);
  36.             continue;
  37.         }
  38.         else{
  39.           digitalWrite(led10, HIGH);
  40.         }
  41.  
  42.         if (is_receiving) {
  43.             digitalWrite(led, HIGH);
  44.             values[index++] = atof(str.c_str());
  45.             ++index;
  46.  
  47.             if (index >= 10) {
  48.                 digitalWrite(led7, HIGH);
  49.                 // We received the final value.
  50.                 is_receiving = false;  // Reset the flag.
  51.                 index = 0;
  52.                 return true;
  53.             }
  54.         }
  55.         else{
  56.           digitalWrite(led10, HIGH);
  57.         }
  58.     }
  59.  
  60.     return false;
  61. }
  62.  
  63. void setup() {
  64.     Serial.begin(115200);
  65.     pinMode(led, OUTPUT);
  66.     pinMode(ledl, OUTPUT);
  67.     pinMode(led3, OUTPUT);
  68.     pinMode(led6, OUTPUT);
  69.     pinMode(led7, OUTPUT);
  70.     pinMode(led10, OUTPUT);
  71.     pinMode(led8, OUTPUT);
  72. }
  73.  
  74.  
  75.  
  76. void loop() {
  77.     if (check_serial()) {
  78.         // we received a new array of values. process them all now:
  79.         // ...
  80.     }
  81.     else{
  82.       digitalWrite(led8, HIGH);
  83.     }
  84.  
  85.     // do other stuff..
  86. }
Advertisement
Add Comment
Please, Sign In to add comment