Pages: 1 [2] 3   Go Down
Author Topic: Gamepad connected to computer controls servos connected to Arduino board?  (Read 2989 times)
0 Members and 1 Guest are viewing this topic.
Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3769
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And does it work?
You want to use servos, so whatever in this code is being used to control the DC motors, you need to replace with the servo library.

Have you tried the servo library?
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Full Member
***
Karma: 3
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I put the robot with servos on the side (I'm not using it for now) and use the motor shield to control two DC motors. The motors didn't work properly. I think the map(a,b,c,d) command needs to be changed.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3769
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

post your code
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Full Member
***
Karma: 3
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead =127;//define the spead of motor
byte checkByte = 0;         // incoming serial byte
byte lMotorSpeed = 0;
byte rMotorSpeed = 0;
byte lMotorDir = 0;
byte rMotorDir = 0;
int ledPin = 4;
int counter = 0;
int timeout = 15; // about half a second
int second = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(pinI1,OUTPUT);
  pinMode(pinI2,OUTPUT);
  pinMode(speedpinA,OUTPUT);
  pinMode(pinI3,OUTPUT);
  pinMode(pinI4,OUTPUT);
  pinMode(speedpinB,OUTPUT);
}
void loop()

  if(Serial.available())
  {
    if (Serial.available() > 2)
    {
      checkByte = Serial.read();
      if(checkByte == '#')
      {
        counter = 0;
        digitalWrite(ledPin, HIGH);
        lMotorSpeed = Serial.read();
        rMotorSpeed = Serial.read();
        //Serial.println("checkByte OK!");
        lMotorSpeed = (Serial.read()-'0')*100;
        lMotorSpeed += (Serial.read()-'0')*10;
        lMotorSpeed += (Serial.read()-'0');
        //Serial.print("L Value Read: ");
        //Serial.println(lMotorSpeed, BIN);
        rMotorSpeed = (Serial.read()-'0')*100;
        rMotorSpeed += (Serial.read()-'0')*10;
        rMotorSpeed += (Serial.read()-'0');
        //Serial.print("R Value Read: ");
        //Serial.println(rMotorSpeed, BIN);
        lMotorDir = bitRead(lMotorSpeed, 7);
        rMotorDir = bitRead(rMotorSpeed, 7);
        //Serial.print("L Motor Dir: ");
        //Serial.println(lMotorDir, BIN);
        //Serial.print("R Motor Dir: ");
        //Serial.println(rMotorDir, BIN);
        if(lMotorDir)
        {
          //Serial.println("L Motor Dir: Backwards");
          digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
          digitalWrite(pinI3,HIGH);
        } 
        else
        {
          //Serial.println("L Motor Dir: Forwards");
          digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
          digitalWrite(pinI3,LOW);
        }
        if(rMotorDir)
        {
          //Serial.println("R Motor Dir: Backwards");
          digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
          digitalWrite(pinI1,LOW);
        } 
        else
        {
          //Serial.println("R Motor Dir: Forwards");
          digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
          digitalWrite(pinI1,HIGH);
        }
        lMotorSpeed = map(lMotorSpeed, 0, 127, 0, 127);
        rMotorSpeed = map(rMotorSpeed, 0, 127, 0, 127);
        analogWrite(speedpinA,lMotorSpeed);//input a simulation value to set the speed
        analogWrite(speedpinB,rMotorSpeed);
      }
    }
  }
}


I think that these commands need to be changed:
map(lMotorSpeed, 0, 127, 0, 127);
map(rMotorSpeed, 0, 127, 0, 127);
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried to follow the instructions at http://principialabs.com/joystick-control-of-a-servo/ but the person who created the article with video used Linux instead of Windows. Any suggestions?
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9686
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried to follow the instructions at http://principialabs.com/joystick-control-of-a-servo/ but the person who created the article with video used Linux instead of Windows. Any suggestions?

Find a windows programming language you can use and write a control program for use with the joystick. below is for a simple programming language called justbasic.

Code:
open "joystick demo" for window as #joy
'open "joystick demo" for graphics as #joy
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
'#joy "down" 'put the pen down
#joy "trapclose [quit]"
timer 10, [readStick] 'every ten ms read the stick
wait

[readStick]
    scan
    readjoystick 1 'either 1 or 2
    '#joy "place 50 50"
    '#joy "\Reading X = "; Joy1x
    '#joy "\Reading Y = "; Joy1y
    '#joy "\Reading Z = "; Joy1z 'throttle slide
    '#joy "\Reading X = "; (Joy1x - 1200)/195
    '#joy "\Reading Y = "; (Joy1y - 1200)/195
    x = (Joy1x - 1200)/195
    y = (Joy1y - 1200)/195
    x1 = int(x)
    y1 = int(y)
    if x1 > 255 then x1 = 255
    if y1 > 255 then y1 = 255
    '#joy "\Reading X1 = "; x1;"  "
    '#joy "\Reading Y1 = "; y1;"  "
    '#joy "\Reading jb1 = "; Joy1button1;"  "
    '#joy "\Reading jb2 = "; Joy1button2;"  "

    if Joy1button1 = 1 then
        PRINT #2, CHR$(00); CHR$(128); CHR$(x1);
        PRINT #2, CHR$(01); CHR$(128); CHR$(y1);
    end if

     if Joy1button2 = 2 then
        PRINT #2, CHR$(00); CHR$(128); CHR$(x1);
        PRINT #2, CHR$(01); CHR$(128); CHR$(y1);
    end if

    wait

[quit]
    CLOSE #2
    close #joy
    end
Logged

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

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3769
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
map(lMotorSpeed, 0, 127, 0, 127);
map(rMotorSpeed, 0, 127, 0, 127); 

Yes these should be,
MotorL= map(lMotorSpeed, 0, 127, 0, 255);
MotorR= map(rMotorSpeed, -127, 0, 255, 0);

Try that.
You should make new variables MotorL and MotorR, you should not use the same variables names inside the map as the ones outside.

Plus, it is easier to debug the code.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Full Member
***
Karma: 3
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to try again with a Parallax 2-Axis Joystick connected to another Arduino Uno board. Their values are from 0 to about 1000 with the middle at 514. The values on servos are from 0 to 180 with the middle at 90. Should the map command look like this:

value = map(value, 0, 1024, 0, 180);

How do I convert a value from char to int?
« Last Edit: December 09, 2012, 06:42:37 am by jeffmorris » Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3769
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is what I made to do just that, this gets in X,Y,Z and a state.
Code:
       
 void loop() {
   if( Serial.available())       // if data is available to read
   {
    digitalWrite(ledpin, HIGH); //my way of knowing the robot is getting the data
    char val = Serial.read();
    if (val == ','){
      currentCommand++;
    }
    else {
      //Serial.println(val);
      switch (currentCommand) {
      case 0:
        X += val;
        val = "";
        break;
      case 1:
        Y += val;
        val = "";
        break;
       case 2:
        Z += val;
        val = "";
        break;
      case 3:
      //Serial.println("X: "+X);    //see what is being stored on serial monitor
      //Serial.println("Y: "+Y);    //see what is being stored on serial monitor
      //Serial.println("Z: "+Z);    //see what is being stored on serial monitor
        state = val;                   // this last one ONLY gets 1 char, you need to modify this or take it out.
        currentCommand = 0;    // reset case statements back to zero
        val = "";                         // clears string
        //Serial.println("state: "+state);  //see what is being stored on serial monitor
        //Serial.println();
        x=X.toInt();               //this right here converts the string of character into an INT, .toINT()
        y=Y.toInt();               //this right here converts the string of character into an INT, .toINT()
        z=Z.toInt();               //this right here converts the string of character into an INT, .toINT()
        s=state.toInt();         //this right here converts the string of character into an INT, .toINT()
        X=""; Y=""; Z=""; state="";   // clears strings for new data
        //move( z, y, s);                  sends the int data to my wheels
        break;
      }
    }
  }
}
« Last Edit: December 12, 2012, 10:48:07 am by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Full Member
***
Karma: 3
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found code on http://bildr.org/2011/04/arduino-xbee-wireless/ that lets me adjust the brightness on a LED wirelessly. I tried to modify the code to let me control a servo wirelessly but the servo doesn't work properly. Continuous servo doesn't run continuously. Regular servo doesn't run the way that a regular servo should run. Any suggestions?

Code:
#include <SoftwareSerial.h>
#include <Servo.h>
#define potPin 3
#define ledPin 11
Servo servoLeft;
Servo servoRight;
char val1;
char val2;
int value1;
int value2;
int inByte = -1;
char inString[6];
int stringPos = 0;
int lastValue = 0;
const int xb_rx = 2;
const int xb_tx = 3;
SoftwareSerial Xbee(xb_rx,xb_tx);
void setup() 
{
  servoLeft.attach(10);       // Left servo to pin 10
  servoRight.attach(9);       // Right servo to pin 9
  servoLeft.write(90);
  servoRight.write(90);
  Serial.begin(9600);
  Xbee.begin(9600);
  pinMode(ledPin, OUTPUT);
}
void loop()
{
  inByte = Xbee.read();
  int potVal = analogRead(potPin);
  if( abs(potVal - lastValue) > 5)
  {
     Xbee.println(potVal);
     lastValue = potVal;
  }
  if((inByte >= '0') && (inByte <= '9'))
  {
    inString[stringPos] = inByte;
    stringPos ++;
  }
  if(inByte == '\r')
  {
    int value1 = atoi(inString);
    Serial.println(value1);
    value1 = map(value1, 0, 1023, 0, 180);
    analogWrite(ledPin, value1);
    servoLeft.write(value1);
    for (int c = 0; c < stringPos; c++)
    {
      inString[c] = 0;
    }
    stringPos = 0;
  }
}
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3769
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is it doing, what are you getting out on the serial monitor?
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3769
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try something like this to make the servo rotate continuously.
Example:

myservo1.writeMicroseconds(N);       

N<1500: usually backwards
N>1500: usually forwards
N=1500: usually stop

your values may need to be adjusted to suit your servo.
« Last Edit: December 11, 2012, 12:28:12 pm by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Full Member
***
Karma: 3
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I still can't get the servos to work properly. When using continuous servos, they behave like regular servos and they run clockwise only. The regular servos run slightly back and forth jerkily. I think that the last few lines of code were affecting the servos.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3769
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this code in another sketch.
http://www.bajdi.com/continuous-rotation-servos-and-arduino/
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9686
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple servo test code you can try to see if the servos are ok.

Code:
// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

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

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-22-dual-input"); // 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);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    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

Pages: 1 [2] 3   Go Up
Jump to: