just one thing: nf()
i cant find an arduino alternative nor can i find an equation for it, but its needed for some code. Anyone know an alternative or the equation?
Depending on the Arduino that you are using look at Serial.printf() or sprintf()/snprintf()
Are you referring to java.text.NumberFormat?
That does a bunch of stuff, what to you need it to do?
im using nf() from processing
its defined here
im not exactly sure what it does but i was converting some processing code code i found online that i need.
See post #2
...I dont understand. im not using the that to print something into the serial, im trying to do math...
https://processing.org/reference/nf_.html
I am not a Java programmer but perhaps if you posted an example of what you are doing...
Math with strings? Do you have an example on how you use it?
@UKHeliBob
@Rintin
im converting a some code for a stewarts platform that was originally for processing
https://processing.org/reference/nf_.html
heres the main part that uses nf()
nf() is the only part that is not converted. atleast i think, everything seems ok
// clean Z & Yaw
float z = Z // Z and Yaw vars are already assinged earlier in the script
float yaw = Yaw
String valZ = nf(z, 3, 0); //3 bits at the time
String valYaw = nf(yaw, 3, 0); //3 bits at the time
String cleanZ = valZ.substring(0, 3); //substring of 000.
String cleanYaw = valYaw.substring(0, 3); //substring of 000.
int YAW = parseInt(cleanYaw) - N; //get YAW int to calculate
int ZAX = parseInt(cleanZ); //get Z int to calculate
int POS = ZAX + YAW; //make it int to calcultae with
int NEG = 180 - ZAX + YAW; //calculate neg
int positive = constrain(POS, 0, 180);
int negative = constrain(NEG, 0, 180);
jeez so many edits
rookie
and heres the entire script
//libraries
#include <Servo.h>
//servo stuff
//MIN and MAX PWM pulse sizes, they can be found in servo documentation
#define MAX 2300
#define MIN 700
int zeroAll = 90; // variable to store the servo position
// define position values
float ar = 12.8; //rounding number used to set analog read resolution
int time;
int wait = 0;
int N = 90; //neutral for calculations
void setup() {
// initialize serial communication
// The rate is up to you depending on your project)
Serial.begin(115200);
//servo stuff
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
//attachment of servos to PWM digital pins of arduino
servo1.attach(3, MIN, 2200); //change MIN & MAX to calibrate servo
servo2.attach(5, MIN, 2100);
servo3.attach(6, MIN, 1900);
servo4.attach(9, MIN, MAX);
servo5.attach(10, MIN, 2000);
servo6.attach(11, MIN, 2100);
}
void loop()
{
int X = analogRead(2);
ar = round(X);
int Xcalc1 = X;
int Xcalc2 = X;
int Xcalc3 = X;
int Xcalc4 = X;
int Xcalc5 = X;
int Xcalc6 = X;
int Y = analogRead(3);
ar = round(Y);
int Ycalc1 = Y;
int Ycalc2 = Y;
int Ycalc3 = Y;
int Ycalc4 = Y;
int Ycalc5 = Y;
int Ycalc6 = Y;
int Z = analogRead(4);
ar = round(Z);
int Zcalc1 = Z;
int Zcalc2 = Z;
int Zcalc3 = Z;
int Zcalc4 = Z;
int Zcalc5 = Z;
int Zcalc6 = Z;
int Yaw = analogRead(5);
ar = round(Yaw);
int Yawcalc1 = Yaw;
int Yawcalc2 = Yaw;
int Yawcalc3 = Yaw;
int Yawcalc4 = Yaw;
int Yawcalc5 = Yaw;
int Yawcalc6 = Yaw;
int Roll = analogRead(6);
ar = round(Roll);
int Rollcalc1 = Roll;
int Rollcalc2 = Roll;
int Rollcalc3 = Roll;
int Rollcalc4 = Roll;
int Rollcalc5 = Roll;
int Rollcalc6 = Roll;
int Pitch = analogRead(7);
ar = round(Pitch);
int Pitchcalc1 = Pitch;
int Pitchcalc2 = Pitch;
int Pitchcalc3 = Pitch;
int Pitchcalc4 = Pitch;
int Pitchcalc5 = Pitch;
int Pitchcalc6 = Pitch;
Xcalc1 = round(180 - X - N); //pos=0
Xcalc2 = round(0.5 * X + 45 - N); //pos=45 but reads neg=135 -> inverse
Xcalc3 = round(0.5 * X + 45 - N); //pos=135
Xcalc4 = round(90 - N); //pos=90 but reads neg=90 -> inverse
Xcalc5 = round(0.5 * X + 45 - N); //pos=135
Xcalc6 = round(180 - X - N); //pos=180 but reads 0 -> inverse
Ycalc6 = round(-0.5 * Y + 135 - N); //pos=0
Ycalc5 = round(Y - N); //pos=45 but reads neg=135 -> inverse
Ycalc4 = round(Y - N); //pos=135
Ycalc3 = round(180 - Y - N); //pos=90 but reads neg=90 -> inverse
Ycalc2 = round(180 - Y - N);//pos=135
Ycalc1 = round(0.5 * Y + 45 - N); //pos=180 but reads 0 -> inverse
Rollcalc6 = round(90 - N); //pos=0
Rollcalc5 = round(Roll - N); //pos=45 but reads neg=135 -> inverse
Rollcalc4 = round(180 - Roll - N); //pos=135
Rollcalc3 = round(180 - Roll - N); //pos=90 but reads neg=90 -> inverse
Rollcalc2 = round(Roll - N);//pos=135
Rollcalc1 = round(90 - N);//pos=180 but reads 0 -> inverse
int Rollcon0 = constrain(Rollcalc1, -90, 0);
int Rollcon5 = constrain(Rollcalc5, 0, 90);
Pitchcalc6 = round(180 - Pitch - N); //pos=0
Pitchcalc5 = round(90 - N); //pos=45 but reads neg=135 -> inverse
Pitchcalc4 = round(Pitch - N); //pos=135
Pitchcalc3 = round(180 - Pitch - N); //pos=90 but reads neg=90 -> inverse
Pitchcalc2 = round(90 - N);//pos=135
Pitchcalc1 = round(Pitch - N);//pos=180 but reads 0 -> inverse
// clean Z & Yaw
float z = Z
float yaw = Yaw
String valZ = nf(z, 3, 0); //3 bits at the time
String valYaw = nf(yaw, 3, 0); //3 bits at the time
String cleanZ = valZ.substring(0, 3); //substring of 000.
String cleanYaw = valYaw.substring(0, 3); //substring of 000.
int YAW = parseInt(cleanYaw) - N; //get YAW int to calculate
int ZAX = parseInt(cleanZ); //get Z int to calculate
int POS = ZAX + YAW; //make it int to calcultae with
int NEG = 180 - ZAX + YAW; //calculate neg
int positive = constrain(POS, 0, 180);
int negative = constrain(NEG, 0, 180);
//calculate values
int calc1 = constrain(negative + Xcalc1 + Ycalc1 + Rollcon0 + Pitchcalc1, 0, 180); //string is ready for arduino;
int calc2 = constrain(positive + Xcalc2 + Ycalc2 + Rollcalc1 + Pitchcalc2, 0, 180);
int calc3 = constrain(negative + Xcalc3 + Ycalc3 + Rollcalc2 + Pitchcalc3, 0, 180); //string is ready for arduino
int calc4 = constrain(positive + Xcalc4 + Ycalc4 + Rollcalc3 + Pitchcalc4, 0, 180);
int calc5 = constrain(negative + Xcalc5 + Ycalc5 + Rollcalc4 + Pitchcalc5, 0, 180); //string is ready for arduino
int calc6 = constrain(positive + Xcalc6 + Ycalc6 + Rollcon5 + Pitchcalc6, 0, 180);
Serial.print(calc1);
Serial.print(calc2);
Serial.print(calc3);
Serial.print(calc4);
Serial.print(calc5);
Serial.print(calc6);
delay(100);
}
As I said, I am not a Java programmer, but I don't see that your code does any maths
It looks to me as though
String valZ = nf(z, 3, 0);
will take the value of the float z and output a String named valZ with 3 digits to the left of the decimal point and 0 digits to the right of the decimal point
Incidentally, the full script looks like it could benefit from using arrays
okay thanks! and ive been wondering how to do arrays since i didnt know what theyre called . thanks
edit: i understand how nf() works i just dont know how to put into arduino code. like i dont know how to add a specific amount of zeros to each side
also
im not sure if arrays will work since i have to assign each value in the array to the axis which will look the same if not worse.
i might be doing something wrong though
Some code I recently used had dtostrf() to convert a float into string. That might do what you need.
Im not trying convert a float into a string, im trying to format some numbers by adding a set amount of 0s before or after a variable
String valZ = nf(z, 3, 0); //3 bits at the time
String valYaw = nf(yaw, 3, 0); //3 bits at the time
This is converting a float into a string with some specific parameters. Once it's a string you can easily add digits to the start or end of the variable. You might not find a function already made to do exactly what you want and may need to use a few together or write your own.
oh, sorry i just started arduino coding for this project. but thats the problem, how exactly do i do that? how do i add 0s?
Can you give a few examples of numbers you have and how you want them to appear when printed?
Careful how you phrase yourself…
0 + 0 + 0
Will always be zero.
I’m sure you know more about your problems than we do, but cutting corners won’t get you there any faster.
i mean how do i add zeros to a variable, like turning 120 into 000120
I haven't done string manipulation for a while, but it will likely be something along the lines of myString = "0" + myString to add a zero in front. Use an if statement if you don't always need one.