Lego Servo

Posted by prayag

prayag's picture

I approached this assignment with the idea that we should be able to move the motor like a turtle in the lego programming laguage. I was successful in make the motor go forward and backward but the problem is that since goes in both direction it doesn't have a rewind and the range of movement is very small. I would like to try this with two servo motors which would also allow us to turn right and left. 

The code I used is given below

 

 

d int servoPin = 7;      // Control pin for servo motor
 
int pulseWidth = 0;    // Amount to pulse the servo
long lastPulse = 0;    // the time in millisecs of the last pulse
int refreshTime = 20;  // the time in millisecs needed in between pulses
int val;               // variable used to store data from serial port
 
int minPulse = 500;   // minimum pulse width
int maxPulse = 2250;  // maximum pulse width
char input[5];
int currentWidth = minPulse;
boolean changed;
 
void setup()
{
 
  pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
  pulseWidth = (maxPulse - minPulse)/2 + minPulse;      // Set the motor position to the minimum
  Serial.begin(9600);         // connect to the serial port
  Serial.println("Servo control program ready");
  Serial.begin(9600);
  updateServo();
  changed= false;
 
}
 
void loop()
{
  memset(input,0,4);
  readSerialString(input);
  
    char dir = getDirection(input);
    int length = getLength(input);
    length = length/10;
    
    Serial.print("Moving ");
    Serial.print(dir);
    Serial.print(" by ");
    Serial.print(length);
    
 
  
    if(dir == 'F')
    {
      length = 9;
    }
    
    if(dir == 'B')
    {
      length = 1;
    }
    
    
    if (length >= 1 && length <= 9) {
     length = length - 1;          
     pulseWidth = (val * (maxPulse-minPulse) / 8) + minPulse;  // convert val to microseconds
     Serial.print("Moving servo to position ");
     Serial.println(pulseWidth,DEC);
   }
   Serial.print("Moving servo to position ");
   Serial.println(pulseWidth,DEC);
   updateServo();   // update servo position
  
 
}
 
char getDirection(char *input)
{
  if(input[0] == 'F' && input[1] == 'D')
    return 'F';
  else if(input[0] == 'B' && input[1] == 'K')
    return 'B';
  else
    return '0';
}
 
int getLength(char *input)
{
  int length = (input[2] - '0') * 10 + (input[3] - '0');
  
  return length;
}
 
void readSerialString (char *strArray) {
  int i = 0;
  if(!Serial.available()) {
    changed = false;
    return;
  }
  while (Serial.available()) {
    strArray[i] = Serial.read();
    i++;
    changed = true;
  }
}
 
void updateServo() {
  // pulse the servo again if rhe refresh time (20 ms) have passed:
   Serial.println("PulseWidth = ");
   Serial.print(pulseWidth);
   
  if (millis() - lastPulse >= refreshTime) {
    digitalWrite(servoPin, HIGH);   // Turn the motor on
    delayMicroseconds(pulseWidth);  // Length of the pulse sets the motor position
    digitalWrite(servoPin, LOW);    // Turn the motor off
    lastPulse = millis();           // save the time of the last pulse
  }
}
 
There was some problem with my assignment and my earlier assignment was lost in the website.
The picture of my servo motor turtle is attached
1
Your rating: None Average: 1 (1 vote)