diff --git a/examples/sPSN_Client1/sPSN_Client1.ino b/examples/sPSN_Client1/sPSN_Client1.ino index 1ba99c0352c90ad45282cc5f3dbcb10f47b0f2a3..b432b0275fc527230f1adca641968ff0b6009617 100644 --- a/examples/sPSN_Client1/sPSN_Client1.ino +++ b/examples/sPSN_Client1/sPSN_Client1.ino @@ -76,11 +76,11 @@ void loop() { if(valueChanged(currentValue, THRESHOLD)){ itoa(currentValue, payload, 10); //wandle Wert in ASCII-Zeichen //Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen - newData = true; // neue Daten zum Senden + newData = true; //Flag: neue Daten zum Senden } - if(timeElapsed(SEND_DELAY) && newData){ + if(timeElapsed(SEND_DELAY) && newData){ //Sende neue Daten wenn Mindestwaretzeit abgelaufen psnClient.publish(topicPublish, payload); //bereite Topic und Nutzdaten zum senden vor - newData = false; //Daten wurden gesendet + newData = false; //Flag: keine neuen Daten vorhanden } } diff --git a/examples/sPSN_Client2/sPSN_Client2.ino b/examples/sPSN_Client2/sPSN_Client2.ino index a978799a0799151c1e712fce8c26761f9bb9798b..a01de966d48d77722c27888a63d741f0286072bf 100644 --- a/examples/sPSN_Client2/sPSN_Client2.ino +++ b/examples/sPSN_Client2/sPSN_Client2.ino @@ -73,14 +73,14 @@ void loop() { boolean buttonState = digitalRead(BUTTON_PIN); //lese Poti ein und speichere Wert if(edgeDetected(buttonState) == RISING){ - ledState = !ledState; //Invertiere zu sendenden Wert "LED-Zustand" - itoa(ledState, payload, 10); //wandle Wert in ASCII-Zeichen + ledState = !ledState; //Invertiere zu sendenden Wert "LED-Zustand" + itoa(ledState, payload, 10); //wandle Wert in ASCII-Zeichen //Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen - newData = true; // neue Daten zum Senden + newData = true; //Flag: neue Daten zum Senden } - if(timeElapsed(SEND_DELAY) && newData){ + if(timeElapsed(SEND_DELAY) && newData){ //Sende neue Daten wenn Mindestwaretzeit abgelaufen psnClient.publish(topicPublish, payload); //bereite Topic und Nutzdaten zum senden vor - newData = false; //Daten wurden gesendet + newData = false; //Flag: keine neuen Daten vorhanden } } diff --git a/src/didacticNet.cpp b/src/didacticNet.cpp index 0052e7279e47fef1fc95d7c41f044db4b5c864ce..f54cf0811e9fd9ef2f73aaeff323b2c7c5d8e771 100644 --- a/src/didacticNet.cpp +++ b/src/didacticNet.cpp @@ -11,41 +11,60 @@ //************************************************************************** //LITTLE HELPERS ;-) //************************************************************************** -int edgeDetected(bool currentState){ - static bool lastState = false; - int edge = 0; +int edgeDetected(bool edCurrentState){ + static bool edLastState = false; + int edEdge = 0; - if(currentState && !lastState){ - edge = RISING; + if(edCurrentState && !edLastState){ + edEdge = RISING; } - if(!currentState && lastState){ - edge = FALLING; + if(!edCurrentState && edLastState){ + edEdge = FALLING; } - lastState = currentState; - return edge; + edLastState = edCurrentState; + return edEdge; } -bool valueChanged(int value, int threshold){ - static int lastValue = 0; +bool valueChanged(int teValue, int teThreshold){ + static int vcLastValue = 0; - if(abs(value-lastValue) > threshold){ - lastValue = value; + if(abs(teValue-vcLastValue) > teThreshold){ + vcLastValue = teValue; return true; } return false; } -bool timeElapsed(long delayTime){ - static long lastTime = 0L; +bool timeElapsed(long teDelayTime){ + static long teLastTime = 0L; long currentTime = millis(); - if(lastTime + (delayTime-1) < currentTime){ - lastTime = currentTime; + if(teLastTime + (teDelayTime-1) < currentTime){ + teLastTime = currentTime; return true; } return false; } +int readSerialData(Stream& rsStream, char* rsDataArray, char rsEndSign) { + static int rsCounter = 0; + + if (rsStream.available()) { + char charBuffer = rsStream.read(); + rsDataArray[rsCounter] = charBuffer; + + if (charBuffer == rsEndSign) { + rsDataArray[rsCounter] = '\0'; + int nrOfChars = rsCounter-1; + rsCounter = 0; + return nrOfChars; + } else { + rsCounter++; + } + } + return 0; +} + //************************************************************************** //ROOT diff --git a/src/didacticNet.h b/src/didacticNet.h index 05b0990df369ebe52dd7c91c65f3f44215ae8a5f..1132420a4d5117eee576cb730074128d71540a55 100644 --- a/src/didacticNet.h +++ b/src/didacticNet.h @@ -43,6 +43,7 @@ int edgeDetected(bool); bool valueChanged(int, int); bool timeElapsed(long); +int readSerialData(Stream&, char*, char); class didacticPSNet {