Go Down

### Topic: [HELP] Servos and comparators (Read 1 time)previous topic - next topic

#### Gin319 ##### Nov 21, 2012, 04:18 pm
Guys, I'm currently developing a code for my project, that basically turns a servo a certain number of degrees, by a corresponding pulse from the comparator circuit that i built. It is working fine, along with the reactions, but there is one problem, as there is a continuous pulse on the input of the arduino, it turns the servo, but, it goes back and forth to the desired position, making it move quite a lot, rather than staying put on the position. How to eliminate this? Here's a copy of the code.

Code: [Select]
`#include <Servo.h>Servo MainS;int pos = 90;int L = 12;int C = 11;int R = 10;void setup() {  MainS.attach(8);  pinMode(L, INPUT);  pinMode(C, INPUT);  pinMode(R, INPUT);}void loop() {  int Ls = digitalRead(L);  int Cs = digitalRead(C);  int Rs = digitalRead(R);      if (Ls == 1) {      for(pos = 90; pos <= 179; pos += 5)          {                                          MainS.write(pos);                       delay(15);          }          }        if (Rs == 1) {      for(pos = 90; pos >=0; pos -= 5)          {                                          MainS.write(pos);                       delay(15);        }          }   if (Cs == 1) {        pos = 90;        MainS.write(pos);      }      delay(15);}`

#### johnwasser #1
##### Nov 21, 2012, 09:14 pm
When the L or R pin is on you tell it to move to 90 and then in steps to either 0 or 179.  If you don't want it to move to 90 you could track the current position and move from there:

Code: [Select]
`int position = 90;void loop() {  int Ls = digitalRead(L);  int Cs = digitalRead(C);  int Rs = digitalRead(R);      if (Ls == 1) {      for(pos = position; pos <= 179; pos += 5)          {                                          MainS.write(pos);                       delay(15);          }      position = 179;      }        if (Rs == 1) {      for(pos = position; pos >=0; pos -= 5)          {                                          MainS.write(pos);                       delay(15);        }      position = 0;      }   if (Cs == 1) {        pos = 90;        MainS.write(pos);        position = 90;      }      delay(15);}`
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats #### Gin319 #2
##### Nov 24, 2012, 02:23 pm
Thanks for help. It is working! #### Gin319 #3
##### Nov 26, 2012, 02:51 pm
I have modified the code, and i would like to get the last state of the sensor, in this case, when it turns 1 1 1 1 1, the uC will follow the last state it was in...

Here's the code

Code: [Select]
`//Code by O.L.#include <Servo.h>Servo MainS;int pos = 90;int LM = 13;int L = 12;int C = 11;int R = 10;int RM = 9;int position = 90;void setup() {  MainS.attach(8);  pinMode(LM, INPUT);  pinMode(L, INPUT);  pinMode(C, INPUT);  pinMode(R, INPUT);  pinMode(RM, INPUT);}void loop() {  int LMs = digitalRead(LM);int Ls = digitalRead(L);int Cs = digitalRead(C);int Rs = digitalRead(R);int RMs = digitalRead(RM);if (LMs == 0 && Ls == 0 && Cs == 0 && Rs == 0 && RMs == 0){   pos = 90;        MainS.write(pos);        position = 90;      }      if (LMs == 0 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) {      for(pos = position; pos <= 179; pos += 5)          {                                          MainS.write(pos);                       delay(15);          }      position = 179;      }if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 0) {      for(pos = position; pos >=0; pos -= 5)          {                                          MainS.write(pos);                       delay(15);        }      position = 0;      } if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 0 && RMs == 1) {      for(pos = position; pos >=45; pos -= 5)          {                                          MainS.write(pos);                       delay(15);        }      position = 45;      } if (LMs == 1 && Ls == 0 && Cs == 1 && Rs == 1 && RMs == 1) {      for(pos = position; pos >=135; pos -= 5)          {                                          MainS.write(pos);                       delay(15);        }      position = 135;      } if (LMs == 1 && Ls == 1 && Cs == 0 && Rs == 1 && RMs == 1) {        pos = 90;        MainS.write(pos);        position = 90;      }if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) {        pos = position;        MainS.write(pos);        position = 90;}}`

#### johnwasser #4
##### Nov 26, 2012, 03:26 pm
Code: [Select]
`if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) {        pos = position;        MainS.write(pos);        position = 90;}`

Two things:
1) If you want it to maintain the same position, don't change position to 90.
2) The servo will maintain position until you tell it otherwise.

If you eliminate the un-needed assignment (1):
Code: [Select]
`if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) {        pos = position;        MainS.write(pos);}`

If you then eliminate the un-needed write (2):
Code: [Select]
`if (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) {  }`

Now that the if-statement is empty you can eliminate that, too:
Code: [Select]
`// In the case of (LMs == 1 && Ls == 1 && Cs == 1 && Rs == 1 && RMs == 1) we don't need to do anything.`
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats Go Up