Go Down

Topic: Gamepad connected to computer controls servos connected to Arduino board? (Read 3336 times) previous topic - next topic

jeffmorris

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: [Select]

#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
  }
}


HazardsMind

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

jeffmorris

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

Code: [Select]

#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 ?

HazardsMind

#33
Dec 12, 2012, 10:05 pm Last Edit: Dec 12, 2012, 10:26 pm by HazardsMind Reason: 1
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();
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

zoomkat

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: [Select]

  map(x_position,0,1024,0,180);
  delay(50);  //slow looping
  Serial.println(x_position);
  Xbee.println(x_position);
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

jeffmorris

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

Code: [Select]

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



HazardsMind

I can't help you with processing, I don't like it and I try to stay away from it.
Sorry.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

HazardsMind

#37
Dec 13, 2012, 02:25 pm Last Edit: Dec 13, 2012, 05:49 pm by HazardsMind Reason: 1
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: [Select]

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Go Up