Hello,
I want to have a code that executes a while loop for a specified amount of time that is given to the serial monitor.
This is my current code:
#include <DynamixelShield.h>
DynamixelShield dxl;
const uint8_t DXL_ID_1 = 1;
const uint8_t DXL_ID_2 = 2;
const uint8_t DXL_ID_3 = 3;
const float DXL_PROTOCOL_VERSION = 2.0;
float distance_to_time = 60 / (34.2 * 2 * PI * 10);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(10);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
String mess = Serial.readStringUntil('.'); // input values are time "__,__,__,__."
Serial.print("Input values: ");
Serial.println(mess);
delay(10);
int mess_1 = mess.indexOf(','); // this value represents the amount of time you want to activate motor 1
String distance1 = mess.substring(0, mess_1); // positive value: clockwise | negative value: counterclockwise
delay(10);
int mess_2 = mess.indexOf(',', mess_1 + 1); // this value represents the amount of time you want to activate motor 2
String distance2 = mess.substring(mess_1 + 1, mess_2); // positive value: clockwise | negative value: counterclockwise
delay(10);
int mess_3 = mess.indexOf(',', mess_2 + 1); // this value represents the amount of time you want to activate motor 3
String distance3 = mess.substring(mess_2 + 1, mess_3); // positive value: clockwise | negative value: counterclockwise
delay(10);
int mess_4 = mess.indexOf(',', mess_3 + 1); // this value represents the amount of time you want to activate linear servo
String position4 = mess.substring(mess_3 + 1, mess_4); // positive value: clockwise | negative value: counterclockwise
delay(10);
// convert string to integer
int moving_distance1 = distance1.toInt();
int moving_distance2 = distance2.toInt();
int moving_distance3 = distance3.toInt();
int target_position4 = position4.toInt();
Serial.print("Moving distance 1 (mm) = ");
Serial.println(moving_distance1);
Serial.print("Moving distance 2 (mms = ");
Serial.println(moving_distance2);
Serial.print("Moving distance 3 (mm) = ");
Serial.println(moving_distance3);
Serial.print("target position for linear servo (mm) = ");
Serial.println(target_position4);
if (moving_distance1 > 0) {
float time1 = moving_distance1 * 1000 * distance_to_time; // unit is in milliseconds
Serial.print("Motor 1 rotation time: ");
Serial.println(time1/1000);
unsigned long runtime1 = millis();
if ((millis() - runtime1) < time1) {
dxl.setGoalVelocity(DXL_ID_1, 300);
runtime1 ++;
Serial.print("Motor 1 rotation time: ");
Serial.println(millis()); // check how much time it took to execute the while loop above
}
}
else if (moving_distance1 < 0) {
int time2 = (-1) * 1000 * moving_distance1 * distance_to_time;
unsigned long runtime2 = millis();
unsigned long totalruntime2 = millis();
while ((totalruntime2 - runtime2) < time2) {
dxl.setGoalVelocity(DXL_ID_1, 1324);
runtime2 ++;
}
Serial.print("Motor 1 rotation time: ");
Serial.println(totalruntime2 - runtime2);
}
else {
dxl.setGoalVelocity(DXL_ID_1, 0, UNIT_RPM);
Serial.println("Motor 1 did not rotate.");
}
I have eliminated some irrelevant code for simplicity.
After i upload my code to the arduino uno and input (10,0,0,0.) into the serial monitor (the "10" is the value that is supposed to be used for the first while loop). Once I did that, my serial monitor outputted
Moving distance 1 (mm) = 10
Moving distance 2 (mms = 0
Moving distance 3 (mm) = 0
target position for linear servo (mm) = 0
Motor 1 rotation time: 0.28 (expected rotation time)
Motor 1 rotation time: 4290 (actual time it took for the code to execute? im not sure how to measure that either...)
which are not the values that i envisoned initially.
I expected that the expected roation time, which is 0.28 seconds, to match the actual rotation time.
So my questions are
-
How would i correctly write a code that executes for a specified amount of time, in this case "moving_distance1 * 1000 * distance_to_time (ms)" ?
-
How would I find how long it took for the while loop to execute in total?
Thank you so much!