Can anyone lend an Arduino noob a hand?

Hey, so I recently got involved in Arduino, and have been trying to build a robot arm.

I am trying to control a servo (Linked in through microcontroller of course.) through my laptop. I want to make it move 90 degrees when 'X' is pressed on my laptop and -90 degrees when 'Z' is pressed.

if anyone can steer me in the right direction that would be much appreciated.

-xXProdigalXx

What sketches have you already written to perform other tasks? If this is your very first attempt at writing a c/c++ sketch for an arduino you are probably better off following one of the many programming tutorials that will give you the basic programming knowlege to proceed. Once you have that then just a little studying of the arduino servo class library and you will be on your way.

Lefty

Servo code using characters sent via the serial monitor. For using simple keyboard key presses, you will need to make an application for the pc to capture the key strokes and send them out the serial port.

// zoomkat 11-14-11 serial servo test
// type servo position 1f, 1r, xx, etc. in serial monitor and enter
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h> 
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.writeMicroseconds(1500); //set initial servo position if desired
  myservo2.writeMicroseconds(1500); //set initial servo position if desired
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7); 
  Serial.println("servo-test-22"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  // allow buffer to fill with next character
    }

if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    
    if (readString == "1f") myservo1.writeMicroseconds(2000);
    if (readString == "1r") myservo1.writeMicroseconds(1000);
    if (readString == "1x") myservo1.writeMicroseconds(1500);    
    
    if (readString == "2f") myservo2.writeMicroseconds(2000);
    if (readString == "2r") myservo2.writeMicroseconds(1000);
    if (readString == "2x") myservo2.writeMicroseconds(1500);    

    if (readString == "xx"){
      myservo1.writeMicroseconds(1500);
      myservo2.writeMicroseconds(1500); 
      }
    if (readString == "ff"){
      myservo1.writeMicroseconds(2000);
      myservo2.writeMicroseconds(2000); 
      }
    if (readString == "rr"){
      myservo1.writeMicroseconds(1000);
      myservo2.writeMicroseconds(1000); 
      }
    readString=""; //empty for next input
  } 
}

EDIT:

wait is it possible for me to soft code the position I want it to go to?

wait is it possible for me to soft code the position I want it to go to?

Of course it is.

and of course by this I mean how would I do so

and of course by this I mean how would I do so

What do you mean by soft-code? To me, this means that you want to change, at run time, the servo position. That's what zoomkat's code does.

If you want to send the servo to a specific position, that is hard-coding the position.

int pos = 78;
myServo.write(pos);

PaulS:

and of course by this I mean how would I do so

What do you mean by soft-code? To me, this means that you want to change, at run time, the servo position. That's what zoomkat's code does.

If you want to send the servo to a specific position, that is hard-coding the position.

int pos = 78;

myServo.write(pos);

I would like to take the current location of the servo, and change the position, then be able to do this again, so that I can hit one key to make it move forward, and one key to make it move backward. (I want to have it move in degrees, not over a certain period of time like the code zoomkat gave me.)

EDIT:

I already tried to declare an int and and have serial read the current angle of the servo. Then add one to the angle when a certain String was read in the Serial Monitor. I hope that clears up what that means. (I am still getting used to the syntax sorry)

I would like to take the current location of the servo, and change the position, then be able to do this again, so that I can hit one key to make it move forward, and one key to make it move backward. (I want to have it move in degrees, not over a certain period of time like the code zoomkat gave me.)

So, you want to increment/decrement a value, based on the character read from the serial port. You can do that.

Servo myServo;

int pos = 90;

void setup()
{
   // set serial option and attach the servo
}

void loop()
{
   if(Serial.available() > 0)
   {
      char aChar = Serial.read();
      if(aChar == 'p')
        val += 5;
      else if(aChar == 'm')
        val -= 5;
      myServo.write(val);
}

I already tried to declare an int and and have serial read the current angle of the servo. Then add one to the angle when a certain String was read in the Serial Monitor. I hope that clears up what that means. (I am still getting used to the syntax sorry)

And the code looks like?

Thank you for all your help and patience, as I said I'm still pretty new to this. I am used to the syntax of java, C#, and Basic. If I have any further questions I will continue to post them here.

-Prodigal

Here there's a short code snippet I made, that moves a servo accordigly to the received string: "less" in one direction, "more" in the other. Hope it can be of any utility if you decide to use a string instead of a single char to control the servo

#include <Servo.h> 
#include <string.h>

int rx_pos=0;
char rx_char='0';

char rx_string[50]="";

Servo myservo;
int pos=0;

boolean oneTime=true;

void setup() {
  Serial.begin(9600);
  Serial.println("READY: ");
  
  myservo.attach(9);

}

void loop() {

  if(Serial.available()){
    
    if( (rx_char=Serial.read()) !='\n'){
      
      rx_string[rx_pos++]=rx_char; //put rx char in the string
      
    }
    
    else{
      
      rx_string[rx_pos]='\0'; //add string delimiter
      
      rx_pos=0; //reset string pointer
      
      Serial.println(rx_string); //for debug, print cmd received
      
      oneTime=true; //new string received, so new command possible
      
    }
  }
     
    if(oneTime && (strcmp(rx_string,"more")==0) && pos<180)
        pos+=10;
    else if(oneTime && (strcmp(rx_string,"less")==0) && pos>0)
        pos-=10;
        
    myservo.write(pos);
    
    oneTime=false; //command has already been executed

    delay(10); 
}