Loading...
Pages: 1 2 [3]   Go Down
Author Topic: Gamepad connected to computer controls servos connected to Arduino board?  (Read 1418 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 198
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried zoomkat's code and it worked. I tried to modify his code for wireless communication and it didn't work. I found out that the Println command adds carriage return and line feed characters to end of string being printed. Do I have to modify the code to look for these characters?

Code:
#include <SoftwareSerial.h>
#include <Servo.h>
String readString;
Servo myservo;  // create servo object to control a servo
const int xb_rx = 2;
const int xb_tx = 3;
SoftwareSerial Xbee(xb_rx,xb_tx);
void setup()
{
  Serial.begin(9600);
  Xbee.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(9);  //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 (Xbee.available())
  {
    char c = Xbee.read();  //gets one byte from serial buffer
    Serial.println(c);
    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
    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

Queens, New York
Offline Offline
Edison Member
*
Karma: 29
Posts: 1563
"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 are you sending and what are you getting back, lets start there.

You have this, n = readString.toInt(); but you need to convert it AFTER you get everything
so right I think it is only getting 1 char then it goes to the IF/ELSE and then gets cleared. You don't give it time to get multiple chars.
you need a stop char for it to look for like ":" or  ";" or "."

If the incoming char is anyone of these, then it know that you have everthing and it should then convert the string to an INT.
Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Offline Offline
Full Member
***
Karma: 2
Posts: 198
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have code that reads joystick input and sends it to another Arduino board.

Code:
#include <SoftwareSerial.h>
const byte PIN_ANALOG_X = 0;
const byte PIN_ANALOG_Y = 1;
int x_position;
int y_position;
int x_direction;
int y_direction;
const int xb_rx = 2;
const int xb_tx = 3;
SoftwareSerial Xbee(xb_rx,xb_tx);
void setup()
{
  Serial.begin(9600);
  Xbee.begin(9600);
}
void loop ()
{

  x_direction = 0;
  y_direction = 0;
  x_position = analogRead(PIN_ANALOG_X);
  y_position = analogRead(PIN_ANALOG_Y);
  map(x_position,0,1024,0,180);
  Serial.println(x_position);
  Xbee.println(x_position);
}


Serial Monitor displays values properly. Should I try the code from http://arduino.cc/en/Tutorial/VirtualColorMixer ?
Logged

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

The controller puts out values 0 - 180?
OK, what are you getting on the other end?

Yea about the println, I found that out too, just do:

Serial.print(n);
Serial.println();
« Last Edit: December 12, 2012, 04:26:21 pm by HazardsMind » Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

0
Offline Offline
Tesla Member
***
Karma: 50
Posts: 6546
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I found out that the Println command adds carriage return and line feed characters to end of string being printed. Do I have to modify the code to look for these characters?

I think the readString.toInt() will ignore trailing non neumeric characters. You may be transmitting faster than you are receiving, flooding the receiving buffer. Try a delay like below to slow the transmission to see if the receiving does better. If that works, then you could do some more tweeking.

Code:
  map(x_position,0,1024,0,180);
  delay(50);  //slow looping
  Serial.println(x_position);
  Xbee.println(x_position);
Logged

Why I like my 2005 rio yellow Honda S2000 with the top down, and more!
GOOGLE ADVANCED FORUM SEARCH BELOW!  
Go to:  http://www.google.com/advanced_search?hl=en
put in key search words,
use site or domain:  http://arduino.cc/forum
or in a google search box put key words site:http://arduino.cc/forum

Offline Offline
Full Member
***
Karma: 2
Posts: 198
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code for Processing won't work on Arduino. How can I convert the code so that it will work on Arduino?

Code:
void serialEvent(Serial myPort)
{
  String inString = myPort.readStringUntil('\n');
  if (inString != null)
  {
    inString = trim(inString);
    float[] colors = float(split(inString, ","));
    if (colors.length >=3)
    {
      redValue = map(colors[0], 0, 1023, 0, 255);
      greenValue = map(colors[1], 0, 1023, 0, 255);
      blueValue = map(colors[2], 0, 1023, 0, 255);
    }
  }


Logged

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

I can't help you with processing, I don't like it and I try to stay away from it.
Sorry.
Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

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

I wasn't sure what you wanted to do with Processing, but now I see. Arduino might be made from processing but it does not have the same functions as processing. One function in particular is Split(). Arduino does not have it, and that make things harder to break apart incoming strings. So what you(everyone in general) have to do is MAKE a split function using IF statements and Case statements.

This should do exactly what you want to do, get the incoming data, and convert it to an integer.

Code:
String inString;
int Data;

void setup()
{
Serial.begin(9600);
}

void loop() {
   if( Serial.available() )       
   {
    char val = Serial.read();

    if (val == '.'){  // looks for the period in the incoming data then prints it.
    Serial.print(inString);
    Serial.print(" :: "); 
    Serial.println(Data);
    inString = "";
    }
    else {
      inString += val;    // stores the characters in a string
      Data=inString.toInt();    // converts the string into an integer         
          }
     }
 }


Not compiled but it should work.
Make sure when you send out the data, you include a period at the end, otherwise it will continue to add to the string and never print.

Also I gave you a simple Split function in a previous post, did you analyze it.
« Last Edit: December 13, 2012, 11:49:46 am by HazardsMind » Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

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