Code: Using serial to set servo position (Arduino)

 //Test by Dingo_aus of simple Serial comms 28 Nov 2010


/*
 ---- basic communication example ----
 Control Arduino board functions with the following messages:

 r a -> read analog pins
 r d -> read digital pins
 w d [pin] [value] -> write digital pin
 w a [pin] [value] -> write analog pin
 p m [pin] [value] -> set pin mode


 Base: Thomas Ouellet Fredericks
 Additions: Alexandre Quessy

 */


#include
#include


//GLOBAL VALUES
// Instantiate Messenger object with the message function and the default separator (the space character)
Messenger message = Messenger();
Servo servo1;
int servo1pos = 0;


// Define messenger function
void messageCompleted() {


 
  //serial commands (all need carriage return after them)
  /*  "r a"    -    Read back all analog values
      "r d"    -    Read back all digital values
      "w d [pin] [value]" -> write digital pin
      "w a [pin] [value]" -> write analog pin
       "p m [pin] [value]" -> set pin mode
      "test"    -    Print test message
      "servo1 "  -  Set servo1 to a position in degrees (between 0 and 180)
  



  */

  if(message.checkString("servo1"))
  {
      //Debug
      Serial.print("Setting value for servo1...");
      servo1pos = message.readInt();
      //Clamp values to 0<180
      if(servo1pos < 0)
      {
        servo1pos = 0;
      }
      if(servo1pos > 180)
      {
        servo1pos = 180;
      }
 
      servo1.write(servo1pos);
    
      Serial.print(servo1pos);
      Serial.print("\n\r");
  }
 
  if( message.checkString("test"))
  {
    Serial.print("Test protocol engaged...test successful\n\r");
  }

  if ( message.checkString("r") ) { // Read pins (analog or digital)
    if ( message.checkString("a") ) {
      Serial.print("a ");
      for (char i=0;i<6;i++) { // Read pins 2 to 13
        Serial.print(analogRead(i),DEC); // Send the pin value
        Serial.print(" "); // Add a space separator
      }
      Serial.println(); // Terminate message
    }
    else if ( message.checkString("d") ) {
      Serial.print("d ");
      for (char i=2;i<14;i++) { // Read pins 2 to 13
        Serial.print(digitalRead(i),DEC); // Send the pin value
        Serial.print(" "); // Add a space separator
      }
      Serial.println(); // Terminate message
    }
  }
  else if ( message.checkString("w") ) { // Write pin (analog or digital)
    if ( message.checkString("a") ) {
      int pin = message.readInt();
      int value = message.readInt();
      analogWrite(pin,value); //Sets the PWM of the pin
    }
    else if ( message.checkString("d") ) {
      int pin = message.readInt();
      int state = message.readInt();
      digitalWrite(pin,state); //Sets the state of the pin
    }
  } else if ( message.checkString("p") &&  message.checkString("m") ) { // Pin mode
      int pin = message.readInt();
      int mode = message.readInt();
      pinMode(pin,mode);
  }


}

void setup() {
  // Initiate Serial Communication
  Serial.begin(9600);
  Serial.print("Serial Messenger Test \n");
  message.attach(messageCompleted);
 
  servo1.attach(7);


}

void loop() {
  // The following line is the most effective way of
  // feeding the serial data to Messenger
  while ( Serial.available( ) ) message.process(Serial.read( ) );


}

Comments