My Loops are out of sync.



  • Greetings,

    The following sketch is a variation of one where I simply allowed all eight child sensors to be read and serial printed. I am now working on parsing the data and I know I have loop issues but I am too close to see my own issues. What I would expect this to do is, upon requesting a transmission from a node, print the Node, command, and payload eight times but it only catches the first of eight sensor's worth of data. Would someone suggest a re-placement of my loops in order to realize this?

    Thank you to all - Baran

    /*
     * Copyright (C) 2013 Henrik Ekblad <henrik.ekblad@gmail.com>
     * 
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     * 
     * DESCRIPTION
     * The ArduinoGateway prints data received from sensors on the serial link. 
     * The gateway accepts input on seral which will be sent out on radio network.
     *
     * The GW code is designed for Arduino Nano 328p / 16MHz
     */
    
    #include <SPI.h>  
    #include <MySensor.h>  
    #include <MyGateway.h>  
    #include <stdarg.h>
    MySensor gw();
    MyGateway gw(DEFAULT_CE_PIN, DEFAULT_CS_PIN, INCLUSION_MODE_TIME, INCLUSION_MODE_PIN,  6, 5, 4);  //  I would like to simplify this line but I do not know what to put here.  
    //No longer using inclusion mode or ce/cs
    char inputString[MAX_RECEIVE_LENGTH] = "";    // A string to hold incoming commands from serial/ethernet interface
    int inputPos = 0;
    boolean commandComplete = false;  // whether the string is complete
    
    void setup()  
    { 
      gw.begin();
    }
    
    void loop()  
    { 
      gw.processRadioMessage();   
      if (commandComplete) {
        // A command wass issued from serial interface
        // We will now try to send it to the actuator
        gw.parseAndSend(inputString);
        commandComplete = false;  
        inputPos = 0;  //  I do not understand how this was ever incremented.
      }
    }
    
    
    /*
      SerialEvent occurs whenever a new data comes in the
     hardware serial RX.  This routine is run between each
     time loop() runs, so using delay inside loop can delay
     response.  Multiple bytes of data may be available.
     */
    void serialEvent() {
      while (Serial.available()) {
        for (int i = 0; i<8; i++){
         // look for the next valid integer in the incoming serial stream:
        int node = Serial.parseInt(); 
        // do it again, but throw it away:
        int w = Serial.parseInt(); 
        // do it again, but throw it away:
        int x = Serial.parseInt(); 
        // do it again, but throw it away:    
        int y = Serial.parseInt(); 
        // do it again, this is the Payload:
        int z = Serial.parseInt(); 
        // do it again, this is the current reading:
        int current = Serial.parseInt();
    
          // print the node and payload
          Serial.print("    The Node is :");
          Serial.print(node);
          Serial.print("    The Command is :");
          Serial.print(z);
          Serial.print("    The Payload is :");
          Serial.println(current);
          i = i+1;
        }
        }   
      }
    

  • Mod

    Hi,

    I edited your post to show the code as it should 🙂

    Remove ' i = i+1;'.

    It is obsolete. You already do this with 'i++' where you define your loop...


Log in to reply
 

Suggested Topics

  • 3
  • 5
  • 1
  • 2

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts