I am coming from python and usually you can just write for example input(int("What's your age?")), but here I don't know how to do it, I researched a bit but I didn't understand, can someone help, please! Here is the code(and I need help at math too):
#include<math.h>
#include<Servo.h>
Servo test;
Servo test1;
int xy = 50;
float y;
float x;
float post; //the angle of the first servo
float post1; //the angle of the second servo
float h; //lenght of the needed height for the arm to be able to touch the object
float d; //the distance to the object
int b = 50; //lenght of the arm
void setup() {
Serial.begin(9600);
test.attach(4);
test1.attach(5);
test.write(0);
test1.write(0);
}
void loop() {
x = input(float("X=? "));
y = input(float("Y=? "));
d = sqrt(sq(xy-x)-sq(xy-y));
post = asin((xy-x)/d);
I think I would approach something like what you're after by starting simple, using wasd to move the servo in increments of maybe 1 degree either direction for a and d, and maybe 10 degrees for w and s.
What follows is tested only in Serial and should work with servos that in my testing, I didn't bother to use.
Set your serial monitor to 115200 baud, no line ending. Good luck.
#include <math.h>
#include <Servo.h>
Servo test;
Servo test1;
char mode;
int pos; // to store servo position
int pos1; // to store other servo position
int xy = 50;
float y;
float x;
float post; //the angle of the first servo
float post1; //the angle of the second servo
float h; //lenght of the needed height for the arm to be able to touch the object
float d; //the distance to the object
int b = 50; //lenght of the arm
void setup() {
Serial.begin(115200); // 9600 like you had is meh. 115200 is less meh
test.attach(4);
test1.attach(5);
Serial.println(F("charDrivenServoStateMachine\n\n"));
Serial.println(F("Type 0 to set servos to zero"));
Serial.println(F("Type w,a,s,d to rotate servo shaft"));
Serial.println(F("Use space bar to center servos\n"));
mode = '0'; // start state variable at mode 0
modeZero(); // let us know and init devices to off
}
void loop() {
if (Serial.available() > 0) {
mode = Serial.read();
switch (mode) {
case 'a':
pos -= 1;
pos1 -= 1;
modeOne();
break;
case 'd':
pos += 1;
pos1 += 1;
modeTwo();
break;
case 'w':
pos += 10;
pos1 += 10;
modeThree();
break;
case 's':
pos -= 10;
pos1 -= 10;
modeFour();
break;
case ' ': // space bar to zero (center) servos at 90 degrees
pos = 90;
pos1 = 90;
modeCenter();
break;
case '0':
pos = 0;
pos1 = 0;
zero();
break;
}
// next four lines are to constrain (protect) your servos
if (pos >= 175) pos = 175; // set 175 to 180 for full range with slight risk
if (pos1 >= 175) pos1 = 175;
if (pos <= 5) pos = 5; // set 5 to 0 for full range with slight risk
if (pos1 <= 5) pos1 = 5;
Serial.print("servo 1:");
Serial.print(pos);
Serial.print(" servo 2:");
Serial.print(pos1);
Serial.print(" degrees\n");
}
}
void modeCenter() {
Serial.print("CENTER SERVOS ");
centerServos();
}
void modeZero() {
zero();
}
void modeOne() {
Serial.print("ONE DEG LEFT ");
oneDegLeft();
}
void modeTwo() {
Serial.print("ONE DEG RIGHT ");
oneDegRight();
}
void modeThree() {
Serial.print("TEN DEG LEFT ");
tenDegLeft();
}
void modeFour() {
Serial.print("TEN DEG RIGHT ");
tenDegRight();
}
void centerServos() {
test.write(pos);
test1.write(pos1);
}
void zero() {
test.write(pos);
test1.write(pos1);
}
void oneDegLeft() {
test.write(pos);
test1.write(pos1);
}
void oneDegRight() {
test.write(pos);
test1.write(pos1);
}
void tenDegLeft() {
test.write(pos);
test1.write(pos1);
}
void tenDegRight() {
test.write(pos);
test1.write(pos1);
}
@Robin2's tutorial is definitely a must-read. However, there are libraries for everything else — is there no library for simple serial input tasks? I am familiar with more advanced libraries like cmdArduino, but is there a library that just implements something equivalent to scanf(), gets() and getc()?