Thursday, May 5, 2011

Lab 4 (2 of 2)

In this last post pertaining to Lab 4 we have included the code we used to control our servo motor while using the Serial Monitor. By inputting two specific inputs into the Serial Monitor, we were able to control the speed of rotation of the motor. Here's the code:

double x = 0;
double t;
char input;

void setup() {
pinMode(5, OUTPUT);
Serial.begin(9600);
}


void loop() {
  if (Serial.available() > 0 )
{
input = Serial.read();

if (input == 's') {
  if (x>= 0 && x<=180){
  x = x++;
  }

  if (x > 180)
  {
    x= 0;
  }

   t = ((100*x) / 18 ) + 1000;
  digitalWrite(5, HIGH);
  delay(t);
  digitalWrite(5, LOW);
  delay(30);
}

if (input == 'f') {
  if (x>= 0 && x<=360){
  x = x++;
  }

  if (x > 180)
  {
    x= 0;
  }

   t = ((100*x) / 36 ) + 1000;
  digitalWrite(5, HIGH);
  delay(t);
  digitalWrite(5, LOW);
  delay(30);
}
}
}

No comments:

Post a Comment