problems servomotor

the problem is
when the command is sent to the Arduino Duemilanove using the keyboard
goes to the servo motor turns 180degrees and then the servo turns
without I dispar command how to fix this flaw. I've checked several
times and so far without success

thanks!

#include <Servo.h> 
Servo servo1;      
 
 int position = 0;   
int input = 0;  
void setup()
{
Serial.begin(115200);
  servo1.attach(8);
}
  
void loop()
{
 if (Serial.available() > 0) {
   input=Serial.read();
   if(input!=-1){
  switch(input){
   case '1':
   position=180;
   servo1.write(position);    
    delay(3);                       
   break;
   case '2':
   position= (180-=180);//I'm trying to stay in the same state that was found in option 1 before servo motor to rotate 180 degrees
   servo1.write(position);            
    delay(3);                       
   break;                        
    
  }
 } 
}
}

Hi,

I'm not sure what you're trying to do exactly, but this line looks rather strange... and the compiler is sure to complain loudly.

position= (180-=180);//I'm trying to stay in the same state that was found in option 1 before servo motor to rotate 180 degrees

If you are trying to return to the previous position you need to remember that previous position. Something along the lines of this...

int previous, current = 0;

void setup()
{
...
servo1.write( current );
}

void loop()
{
...
case '1':
previous = current;
current = 180;
servo1.write( current );
...
case '2':
current = previous;
servo1.write( current );
}

Hope that helps,

G.

the problem is
when the command is sent to the Arduino Duemilanove using the keyboard
goes to the servo motor turns 180degrees and then the servo turns
without I dispar command how to fix this flaw. I've checked several
times and so far without success

Could you reword your problem to make the symptom more clear?

Also, are you using a standard servo or a modified servo sometimes called a continous rotation servo or a 360 degree servo?

Lefty

retrolefty:

the problem is
when the command is sent to the Arduino Duemilanove using the keyboard
goes to the servo motor turns 180degrees and then the servo turns
without I dispar command how to fix this flaw. I've checked several
times and so far without success

Could you reword your problem to make the symptom more clear?

Also, are you using a standard servo or a modified servo sometimes called a continous rotation servo or a 360 degree servo?

Lefty

the servo motor is simple
model:hextronik Hxt900

Features:
-Dimension: 21x12x22mm
-Torque: 1.6kg/cm (4.8V)
-Speed: 0.12sec/60 degree
-Voltage: 3V to 6V
turns 180degree

From your description it's very hard to figure out what your servo is doing. Beside the junk with (180 -=180) mentioned above, your program looks ok, although the delay (3) won't really have any effect, the servo is far too slow for this to make a difference. Try delay(300) to start giving the servo to catch up.

Also, try adding in your setup() function a Serial.println("Hello, I just restarted") to check whether the movements are due to resets of your Arduino and the restart of the servo.

Korman

Korman:
From your description it's very hard to figure out what your servo is doing. Beside the junk with (180 -=180) mentioned above, your program looks ok, although the delay (3) won't really have any effect, the servo is far too slow for this to make a difference. Try delay(300) to start giving the servo to catch up.

Also, try adding in your setup() function a Serial.println("Hello, I just restarted") to check whether the movements are due to resets of your Arduino and the restart of the servo.

Korman

good
the goal is after the servo motor turning 180 degrees is to make the servo motor back to the same state it was before turning 180 degrees
I tried that way but I had no success!
if someone can help me I thank

case 2:

position = (position-180);

when you wrote 180 -= 180, it means 180 = 180 - 180, hence, your function became for the compiler

position = 180 = 180 - 180, and since 180 is not equal to 0 (zero) it won't work.

you can also try

position1 = 180;
position2 = position1;

positition2 -= position1 // position2 = position 2-position1

I have never written code for Arduino before, so please correct me if I'm of the chart wrong here.

Did you try implementing the pseudo-code I provided above? I'm pretty sure it will do what you want...

G.

pocketscience:
Did you try implementing the pseudo-code I provided above? I'm pretty sure it will do what you want...
G.

sorry for slow response!
but did not work
entered the loop unending

and i lost control of the servomotor
thanks!
code

#include <Servo.h> 
Servo servo1;      
 
int position1 = 0;   
int input = 0;  

void setup() {
  Serial.begin(115200);
  servo1.attach(8);
}
  
void loop() {
 if (Serial.available() > 0) {
   input=Serial.read();
   if(input!=-1){
     switch(input){
       case '1':  //open port electronica        
         position1=179;
         servo1.write(position1);    
         delay(10);                       
         break;
       case '2':// close port electronica
       int position2=0;
       if(position1!=position2){
          position1 = 179;
          position2 = position1;
         position2 -= position1; // with or without this line, I do not have any control
         servo1.write(position2);            
          delay(10);                       
          break;                        
       
      }}
   } 
 }
}

That's not quite what I had in mind, but I have another question - does anything modify the return position? ie you move to 180, and then back to 0? Is that correct? A more detailed description of what you're trying to achieve would help. If you're only moving between 180 and 0 and back again you don't need to keep track of anything...

Cheers,

pocketscience:
That's not quite what I had in mind, but I have another question - does anything modify the return position? ie you move to 180, and then back to 0? Is that correct? A more detailed description of what you're trying to achieve would help. If you're only moving between 180 and 0 and back again you don't need to keep track of anything...

Cheers,

No need to keep track of anything. A command to say 90 degrees will go to that specific position and then stop no matter what it's present position is.

Only variation is how long it will take, as a move from 0 to 90 takes longer then 45 to 90. This may not seem to be a big deal, but different servos move at different speeds and there is no feedback from the servo that it has reached it's commanded position, so your program may have to try and keep with some timing values to wait if it's important the the servo reaches it's final position before commanding it to move again. Simple time delays may be all that you require, if anything.

Lefty

Yup, I'm well aware of that... :~;)

the idea of controlling the servo motor is to make one control a door
through angles of 180 degrees open the door and close door option 2
with the servo motor position back to 0 to return to a position of command 1

thanks!

Right, so you don't need to keep track of anything except take into account the time it takes for the servo to move from 0 -> 180, and then from 180 -> 0. Alternatively if you can include a switch at either end of the doors movement you will know when it's open or closed and can handle commands when the door is in a known or unknown state appropriately.

Cheers,

pocketscience:
Right, so you don't need to keep track of anything except take into account the time it takes for the servo to move from 0 -> 180, and then from 180 -> 0. Alternatively if you can include a switch at either end of the doors movement you will know when it's open or closed and can handle commands when the door is in a known or unknown state appropriately.

Cheers,

someone program python s60 with arduinoBT for servo motor?
I am with problems of communication arduinoBT not respond commands from the python s60!

thanks!