Go Down

Topic: servo motor control with msTimer2 libraries (Read 756 times) previous topic - next topic

kisoonEom

When you click the mouse features a Ah back of servo motor and want to stop.

And servo motor, stored in a variable of each different servo motor in the assignment.

The problem is that the servo motors to turning to msTimer2 signal stop is about known.
What's the problem? Help me
Code: [Select]
#include <MsTimer2.h>
#include <Servo.h>

Servo servo1, servo2;

int prePos = 0;


void moveServo() {
 static int pos = 0;
 static int spd = 1;
 pos = pos + spd;
 if(pos > 180){  
   spd *= -1;  
 }
 if(pos < 0  ){  
   spd *= -1;  
 }

 servo1.write(pos);
 prePos = pos;
}

void setup() {
 Serial.begin(9600);

 pinMode(10, OUTPUT);
 servo1.attach(9);

 MsTimer2::set(15, moveServo); // 15ms period

}

void loop() {

 if( serialEvent() == 1){
   MsTimer2::stop();
   digitalWrite(10, LOW);
 }
 if(serialEvent() == 0){

   MsTimer2::start();
   digitalWrite(10, HIGH);

 }
 debugging(serialEvent());
 delay(10);

}


boolean serialEvent(){
 if(Serial.available() > 0){
   int val = Serial.read();

   if( val == 1){
     return 1;
   }
 }  
 else {
   return 0;
 }  
}

void debugging(int val){
 Serial.print("val: ");
 Serial.print(val, DEC);
 Serial.println();
}


Go Up