Pages: [1]   Go Down
Author Topic: Can anyone lend an Arduino noob a hand?  (Read 1025 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
// 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
  }
}
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

EDIT:

wait is it possible for me to soft code the position I want it to go to?
« Last Edit: February 10, 2012, 02:51:47 pm by xXProdigalXx » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
wait is it possible for me to soft code the position I want it to go to?
Of course it is.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Code:
int pos = 78;
myServo.write(pos);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Code:
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)
« Last Edit: February 10, 2012, 10:38:59 pm by xXProdigalXx » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Code:
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);
}

Quote
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 3
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:

#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);
}
Logged


Pages: [1]   Go Up
Jump to: