// Get the current value of A_r_prev from flow context - default to 0
// if not already set
var A_r_prev = flow.get("A_r_prev") || 0;
// do your check against it etc
// Update the value in context
flow.set("A_r_prev", A_r_prev);
You'll also need to change the abs(...) call to be Math.abs(...) as abs() isn't a function in JavaScript.
Unfortunately there is no output anymore; here is my updated code
var A_x = msg.payload.d.X;
var A_y = msg.payload.d.Y;
var A_z = msg.payload.d.Z;
var A_r = Math.sqrt(A_x*A_x + A_y*A_y + A_z*A_z);
var A_r_prev = flow.get("A_r_prev") || 0;
flow.set("A_r_prev", A_r_prev);
if ( math.abs(A_r - A_r_prev) > 300) {
msg.payload = "Passed";
return msg;
}
I would also like to implement an audio condition (in addition to the absolute value threshold, it must pass an audio decibel level threshold. How do I do that?
Thanks
Thanks,
I was able to fix it so that both the mic and the accelerometer is running. However, the mic says NaN
I think it is because the accelerometer variables are still looping through.
I may need to apply some filter. Could you provide some help so that I can get both the microphone level and the accelerometer to run?
currently, the mic has no function but the accelerometer does as printed above with minor changes.
var A_x = msg.payload.d.X;
var A_y = msg.payload.d.Y;
var A_z = msg.payload.d.Z;
var A_r = Math.sqrt(A_x*A_x + A_y*A_y + A_z*A_z);
var A_r_prev = context.get("A_r_prev") || 0;
context.set("A_r_prev", A_r);
msg.payload = ""+ Math.abs(A_r - A_r_prev);
// if ((A_r - A_r_prev) > 1000) {
// msg.payload = "Passed";
// }
// else {
// msg.payload = "Not Passed";
// }
return msg;
This is my state machine that I would like to implement in node red -- it has been coded in C and I do not know how to convert it to javascript and implement it into a function. Please let me know if you can provide any help!
Thank you, I have read that information and it is very helpful.
One quick question. I am trying to modify the microphone output ( ST imported) in decibels.
With the acceleration, I called x, y, and z using var A_x = msg.payload.d.X etc.
How do I modify/create a variable for microphone decibels?
this is the current code for the microphone
return msg;
all it does is return the decibels; I will need to create thresholds for the values but I need to find out how to create a variable equal to the decibel output
That will return the msg object that can contain whatever you want. If objects are new to you taking time to read an online javascript guide on objects and arrays is time well spent as you have already discovered they are fundamental in Node-RED. The w3schools javascript object and arrays guide is often recommended, easily findable with google