Hello all.
New to Arduino and programming but so far I've made most of my functions work.
I am making a laser pointer cat toy and hope to be able to control it over the internet as I already have a webcam in my room. That is WAY above me at this point.
Anyway, with the help of some 3d modeling software and a 3d printer, I'm making a fixture to hold the servos, laser, board and all other electronics. Until that is all printed up, I'm working on the code.
The problem I'm having is making the Arduino plot points around the circle and then assign those coordinates to the servos. Since the servos move from 0 to 180, I'm using 90 degrees as my zero point, so (90,90) would be the origin of the graph.
I would like to plot a circle of radius 20, so the range of each servo is from 70 to 110 degrees.
So to sum this up, a FOR loop starts at 0 degrees and stops at 359, increments by one degree.
At each value of i (degrees around the circle) take the cos/sin respectively, multiply by 20 (because thats the radius I want to use) and add 90 degrees to it for the offset of the servo.
When executed, they both kind of move back and forth MANY times.
I've tried some degree-> radian conversions with no luck either.
If it were me, I'd create a couple of variables equal to your X and Y functions and print them to see what values you are actually writing to the servos. I think that would tell you a lot.
Discussion
Angles are specified in radians and the result is a floating-point number.
The following example illustrates the trig functions:
float deg = 30; // angle in degrees
float rad = deg * PI / 180; // convert to radians
Serial.println(rad); // print the radians
Serial.println sin(rad)); // print the sine
Serial.println (cos(rad)); // print the cosine
This converts the angle into radians and prints the sine and cosine. Here is the output
with annotation added:
0.52 30 degrees is 0.5235988 radians, print only shows two decimal places
0.50 sine of 30 degrees is .5000000, displayed here to two decimal places
0.87 cosine is .8660254, which rounds up to 0.87