I'm working on a project where I am receiving data from an application. This data is processed on NodeRed, and then NodeRed basically communicates with my Arduino using the Firmata protocol loaded onto the board. However, I want to implement the functions (which are running servos) to be loaded onto the Arduino along with the Firmata sketch, so that the only thing NodeRed does is give signals to the designated pins, while the functions in the Arduino run the motors based on the input to said pins.
In short, I want to communicate just the HIGH/LOW using the Firmata protocol to pins A1 and A2 from NodeRed, and the code above should execute based on that. However, to communicate between NodeRed and Arduino, I need the Firmata sketch. I don't know how to integrate my sketch into the Firmata sketch.
So basically this bit takes input from the pins which are connected to a switch. Whenever the switch goes high, the value of switch#Smoothed increases (depending on which switch it is) very slowly and when I let go of the switch, the value of switch#Smoothed begins to reduce slowly. It's basically smoothing out the servo movement.
My goal is to somehow receive input to the pins A1 and A2 from NodeRed rather than the physical switches I have right now.
I needed Firmata to communicate with NodeRed. Apparently, because of its restricted use (being compatible only with the StandardFirmata sketch), it is recommended to write some code to communicate via Serial instead, as it ends up being simpler. There was a discussion about this here: Arduino Node Sketch Compatibility - General - Node-RED Forum for NodeRed specifically. Hope this helps.
Just write serial code to implement a State Machine. Construct your own commands to handle all needed functions. As an example (crude) of how such a code may function, envision a serial calculator with verbs" Add, Sub, Div, Mul..."
#define Err 2
#define operations 23
byte DigPinNo;
char* sStack[ ] = {
"DIV","MUL","ADD","SUB","Y^X","LOG","NLG","10X","1/X","e^X",
"SQR","X^2","SIN","COS","TAN","ASN","ACS","ATN","DEG","RAD",
"DEC", "RTT","DPR"};
...
void setup()
pinMode(3, INPUT); // sets the digital pin 3 as input
pinMode(6, INPUT); // sets the digital pin 6 as input
pinMode(7, INPUT); // sets the digital pin 7 as input
pinMode(8, INPUT); // sets the digital pin 8 as input
pinMode(9, INPUT); // sets the digital pin 9 as input
pinMode(10, INPUT); // sets the digital pin 10 as input
pinMode(11, INPUT); // sets the digital pin 11 as input
pinMode(12, INPUT); // sets the digital pin 12 as input
...
case 22: //DPR DigitalPin Read # valid 2 - 13 Return 0, 1 for state or 2 for Error
if (verbose) {Serial.print(F("Prompting for Digital Pin Number 2 - 13: ")); }
DigPinNo = Serial.parseInt();
if (DigPinNo <2 || DigPinNo > 13) {
if (verbose) { Serial.print(DigPinNo);
Serial.print(" Pin# Error"); break; }
Serial << Err; break; }
if (verbose) { Serial.print(DigPinNo); }
if (verbose) {Serial.print(F(" Logic State = ")); }
Serial << digitalRead(DigPinNo);
if (verbose) {Serial.println(); }
break;