Hi there everyone!
I have been using this sketch that works perfectly.
So as you can see, you send a value through the serial monitor, this value is stored and then send to a ESC (electronic speed controller)
This is the code:
#include <Servo.h>
Servo ESC; // create servo object to control the ESC
int inputVal = 90; // variable to store the input value
void setup() {
Serial.begin(9600);
// Attach the ESC on pin 9
ESC.attach(9, 1000, 2000); // (pin, min pulse width, max pulse width in microseconds)
}
void loop() {
if (Serial.available()>1) { // check if there is any input available from Serial Plot
inputVal = Serial.parseInt(); // read the input value and store it in the variable
Serial.print("Input value is: "); // print a message to confirm the input value
Serial.println(inputVal);
ESC.write(inputVal); // Send the signal to the ESC (value between 0 and 180)
}
else Serial.println("nothing to read");
delay(100); // wait for a short period to avoid excessive readings
}
I just change my needs for this script and run into issues.
As you can see in the following code, its more or less the same but introducing a float instead of an integer:
#include <VescUart.h>
/** Initiate VescUart class */
VescUart UART;
float inputVal = 0.0;
void setup() {
Serial.begin(9600);
/** Setup UART port (Serial1 on Atmega32u4) */
Serial1.begin(115200);
while (!Serial1) {;}
/** Define which ports to use as UART */
UART.setSerialPort(&Serial1);
}
void loop() {
if (Serial.available()>1.0)
{
inputVal = Serial.parseFloat();
Serial.print("Input value is: ");
Serial.println(inputVal);
UART.setRPM(inputVal);
}
else Serial.println("nothing to read");
delay(100);
}
What happens is that I introduce -let's say- 200.0- and then it works, it tells me "Input value is: 200.0" and actually i can see that the motor is moving, but just for a loop.
After that, the serial monitor puts again "nothing to read" and the motor stops (since i guess, the value returns to being 0)
So I guess what I need is the last value to remain until I put the next value
I hope someone can help me ![]()
Thanks in advance