Arduino Forum

Using Arduino => Motors, Mechanics, Power and CNC => Topic started by: sierrahorangic40 on Oct 19, 2018, 05:17 pm

Title: How to get servo angle to change based on a mathematical function
Post by: sierrahorangic40 on Oct 19, 2018, 05:17 pm
Hi,
I'm aware that you can program servos to move to a certain angle like this:
Code: [Select]
servo1.write(180); // set servos to original position
servo2.write(0);
delay (500);
 servo1.detach();
 servo2.detach();

I was wondering if there is a way to get the servo to change continuously based on a mathematical function you could declare at the beginning?
Title: Re: How to get servo angle to change based on a mathematical function
Post by: jremington on Oct 19, 2018, 05:49 pm
Code: [Select]
void loop() {
   servo.write(your_function());  //function returns values 0 to 180
}

There is rarely a need to use servo.detach();
Title: Re: How to get servo angle to change based on a mathematical function
Post by: sierrahorangic40 on Oct 19, 2018, 06:30 pm
Can you help me with this part?
I want my servo angle be [ arctan(5t/8.255) ] based on the variable of t (seconds)
I've been trying to declare different variable and even downloaded a couple libraries, but it's not working and I'm having problems.
This is the base code that I keep trying to get to work:
Code: [Select]
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <Servo.h>
#include <math.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Servo servo1;
Servo servo2;


void setup() {
 
Serial.begin(9600);
Serial.println("wrist");
AFMS.begin();


servo1.attach(10);
servo2.attach(9);
servo1.write(180);
servo2.write(0);
}

void loop() {
  {

 servo1.write(double  atan2 (double __y, double __x);
}}
}


Title: Re: How to get servo angle to change based on a mathematical function
Post by: jremington on Oct 19, 2018, 06:59 pm
You are attempting to run before you can crawl. There is nothing in your code that keeps track of time, and you haven't yet learned how to use built in functions.

I suggest that you start from the beginning and work through some of the example projects that come with the Arduino software: blink an LED, time events, read a pushbutton, etc.

If you don't want to do that and instead want someone to write the code for you, post on the Gigs & Collaborations forum section. You may be asked to pay for the help.