Hello,
The usual apologies in advance for infantile questions from a
complete beginner (to life some would say). It's my first time on this
forum (any forum for that matter) so please be patient. This is the
closest discussion to what I am looking for, but that may be simply due
to the fact I have no clue as to the correct terminology when it comes
to programming. So another apology for diverting this discussion
off-course.
I bought my daughter a Raspberry Pi and rashly
promised to build her a robot, the robot bit not a problem (so far). I'm
using and arduino nano for the servo control, but I want it to have a
level of interaction and fun (voice recognition, face
recognition,text-to-speech, rude answering back, blowing raspberries at
my mother-in-law etc..) so am using the Raspberry Pi for the brain.
These aspects are roughly working or on the way by the judicious
thievery of various python scripts
The robot controller on the other hand I'm stumbling at the first hurdle.
I
want to control it from a web interface, and also have other sensor
inputs and data outputs, hence the choice of Node-red to bring it all
together.
On the UI i wanted a pair of buttons to control the rotation of
each servo. whereby on pressing the button the number was increased (or decreased depending on direction) until the button was released. So like Kay, I couldn't really get any of the standard UI widgets to do it (I know sliders would do the basic function I am looking for, but they can only be horizontal and I am wanting the button/slider oriented in the direction it will move the robot).
I was looking for a what I called a "continuous press button" whereby when pressed it sent one string and when released sent a different string, the string can then be used by a function node to increase (or decrease) and output the numbers. A quick search pointed me in the direction of what Dave has suggested above, i.e. template using mousedown and mouseup. and the button does exactly what I want.
It's the function node that's the problem, I've got it so that on mousedown it receives a payload from the button template, and it starts to increase the numbers every half second (I will speed this up once I've got it working) up to 180 by using setInterval. However I can't get it to stop on mouseup when it receives the word "stop". I think it is beacause I can't seem to get clearTimeout to work, but it could be that my code is completely wrong.
I've tried putting the clearTimeout in a different function, outside the "increase" function, and all other possibilities I can imagine (which goes to demonstrate how little I understand the code) all to no avail.
Any suggestions regarding clearTimeout much appreciated, if you think there's a better way of controlling the robot from the UI then I'm open to suggestions.
Function pasted below
var shoulder = global.get("position.shoulder");
function increase(){
shoulder = (shoulder + 1) % 181; // SET { 1-180 }
global.set("position.shoulder",shoulder);
var msg =
{
payload: { countTotal:shoulder }
};
node.send(msg);
}
if (msg.payload =="stop"){
clearTimeout(up);
}
var up = setInterval(increase, 500);
if (msg.payload =="up"){
increase();
}
Many many many thanks in advance