Show Posts
Pages: 1 [2] 3
16  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 23, 2009, 11:10:35 am
Thanks!  Yes, you're right.  The code works perfectly in ###C format.  I don't want to appear ungrateful, but it isn't C### format.  I don't think I ever said it didn't work, just not in that format.  And please, I don't want to waste anyone's time!  I'm VERY grateful for all the help in getting a working solution.  Should the delays cause other problems I'd just switch to Mem's solutions.  
17  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 23, 2009, 10:57:48 am
If I enter P100 it doesn't work the first time.  It sends a command of 0.  It works with the second entry, however.  But then it won't take the next command.  If I type P100, enter, then P110 right after it still reads P100.  It will only read a new command after the second entered line.   It takes two tries to get a new position sent to the servos.
18  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 23, 2009, 10:08:49 am
Thanks Groove.  You've earned your 4 stars by me.  I'm sure without your insight about the held "enter" I'd be wondering why the sketch doesn't work through my PC program!  Thanks again!
19  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 23, 2009, 10:02:17 am
I put in P100. and wait.  Nothing.  When I hit enter it echoes the string back and the "break" word.

BTW, did you just turn into a 4-star Arduino Forum General?  I could have sworn you were a colonel this morning smiley-wink
20  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 23, 2009, 09:49:12 am
Code:
void readSerialString () {
  while(Serial.available() > 0)  // will do until gets full line
  {
    int iBuffer = Serial.read(); // if something comes in save it
 
  if(iBuffer == '.')
          break;  // but we may never get here!
 
    cString[iBufferIndex]  = iBuffer; // add it to string
    iBufferIndex++;
    cString[iBufferIndex]= '\0'; // make sure our last element is nothing
  
    Serial.print( iBuffer , BYTE); // echo back
    
    //delay(5); // for whatever reason, Arduino needs this or read doesn't finish
  }
}

Thanks! Can you explain why the sketch works with the delay and not without it?
21  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 23, 2009, 09:43:46 am
I just tried it, but as I suspected, it doesn't get fired.  I now believe if you want to process characters you need to put the character test in another function outside a Serial.available loop.  

That to me would an over-complication.  I'd rather just stick with the 5 ms.  I've been on this for a couple of days now and my head is swimming.  Perhaps after a few days I can think straight about it again or someone can send me my last sketch modified to work without the delay.

delay(5) is Arduino's sleep command I assume.  It stops processing for 5 milliseconds.  

I just read your modification.  I wouldn't be surprised if it doesn't work in Hyperterminal either.  But I'll be sending from a C# (.NET) program so should be able to adjust.
22  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 23, 2009, 09:15:43 am
My sketch does not wait 5 ms and exit readSerialString.   readSerialString is only called when I press enter through the Serial Monitor.  Perhaps that's not what it should do, but that's what I experience.  If you look at my sketch you can see there is no trapping for exit characters and yet the functions work, theory be damned.  

As an aside, I believe this is undocumented on Arduino.  Serial.available is set false when it gets a LF and/or CR

I understand how you might think I'm dense.  When I don't have the servos attached I don't need the 5 ms delay.  

But with the servos attached if I don't have a 5 ms delay the sketch behaves more like you think it should behave.  That is, it seems to write P to the buffer right away and calls the processServo function.  

However, the trapping for exit characters does NOT fix that.  I tried your code and others.  All with the same result.  

I believe if you attach 2 servos and run my sketch you will have a lot to think about too smiley-wink

I don't mean to be disrespectful.  I really appreciate your help!!  But a few people have been working on this and I'm not the only one thinking about it.  We still remain at the same place.  No one has sent me code, or a sketch, that works without the 5 ms delay.  

I wouldn't be surprised if it's something silly I'm missing.  Again, I just don't have the code to prove it.
23  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 22, 2009, 06:33:26 pm
I don't know if they're not getting transmitted, or they are, but Arduino seems to interpret them as end of transmission.

I've tried all sorts of things but nothing I do works without the delay.  I believe there is some issue with the handshaking, whatever, that doesn't allow Arduino to break out of a serial.read through LF or CR (I think it does that automatically when those are detected).

As you can see, I've taken out the checking for LF CR and it still works, but not without the 5 ms delay, even at higher baud rates.  

What I meant before is that yes, the LF or CR is never detected because the print code in that routine never fires.  They apparently stop the read on their own.

A 5 ms delay is fine.  Even if it's an oddity in Arduino I'd imagine that might be the internal fix so whether you see it or not I suspect that's the amount of time you'd have to wait anyway.

Code:
#include <Servo.h>

Servo panServo;
Servo tiltServo;

// SERIAL PORT VARS
char cString[80];  // array that will hold command string (though we'll only need length 3)
int iBufferIndex = 0; // index of buffer characters rec'd

// SERVO VARS
int icount = 0;
long pval = 0; // pan val to send to servo
long tval = 0; // tilt val
int servoPanPin = 9;     // Control pin for servo motor
int servoTiltPin = 10;     // Control pin for servo motor

void setup() {
  Serial.begin(9600);         // connect to the serial port
  panServo.attach(servoPanPin);  
  tiltServo.attach(servoTiltPin);  
  Serial.println("servos_ready");
}

void loop() {
    readSerialString();  // read, wait for command
    processSerialString(); // process command, either pan or tilt
}

void readSerialString () {
  while(Serial.available() > 0)  // will do until gets full line
  {
    int iBuffer = Serial.read(); // if something comes in save it
 
    cString[iBufferIndex]  = iBuffer; // add it to string
    iBufferIndex++;
    cString[iBufferIndex]= '\0'; // make sure our last element is nothing
  
    Serial.print( iBuffer , BYTE); // echo back
    
    delay(5); // for whatever reason, Arduino needs this or read doesn't finish
  }
}
 
void processSerialString() {
   if( iBufferIndex > 0) {
  
      if(cString[0] == 'P') // first char should be P or T
      {
        cString[0] = ' ';
        pval = atoi(cString); // pan value is number after P
  
      //   Serial.println();
      //   Serial.print("Value to PAN servo: ");
      //   Serial.print(pval, DEC);
 
      panServo.write(pval);      
      }
  
      if(cString[0] == 'T')
      {
      cString[0] = ' ';
      tval = atoi(cString);
  
      Serial.println();
      //   Serial.println();
      //   Serial.print("Value to TILT servo: ");
      //   Serial.print(tval, DEC);
      
      tiltServo.write(tval);      
      }      
      
      Serial.println();
      // Re-init
      iBufferIndex = 0;
      icount = 0;
      pval = 0;
      tval = 0 ;
      
   } // if string isn't 0

}




24  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 22, 2009, 01:28:18 pm
ending with a . is fine.  or putting T at beginning.  But I"m happy with delay because the other things don't solve what's happening.  My theory at the moment is that the Arduino gets both a line feed and carriage return, acts on the line feed, but then does nothing because of the CR, or something like that.  I have to run out for a moment, but when I get back I'll try just trapping on just CR, or just LF and see if that does anything.
25  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 22, 2009, 12:06:54 pm
I wouldn't dream of leaving you guys hanging smiley

I did what you suggested.  I remmed out all the prints except the one after the read.  With the delay I would get what I typed in.

without it I would get for P100
P
100

That seems consistent for whatever P value I typed in.  I'd get the P on one line, and the number immediately below it.

For T074

I'd get

T

74

Again, any T value would have a line feed between T and the number, no matter what value I put in.

26  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 22, 2009, 11:22:02 am
I've fiddled around with it a bit and find that I can put a very short delay of 5 milliseconds and everything works (1 ms won't fire the first command properly).  Here's where we're at.   It seems very fast so I can live with it.  But if you're a purist you hate that delay.


Code:
void readSerialString () {

  while(Serial.available() > 0)  // We've got the first char
  {
    byte inByte = Serial.read(); // read it.
    if(inByte == '\n' || inByte == '\r')  // eventually(!), we'll empty the buffer
    {
    Serial.print("break: ");
    Serial.print(inByte);
    break;  // but we may never get here!
    }

    // Do something with the character just read...
    serInString[serInIndx]  = inByte;
    serInIndx++;
    serInString[serInIndx]= '\0';
    delay(5);
  }
}


27  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 22, 2009, 11:09:13 am
Yes, as you predicted the print during break doesn't happen.
Code:
void readSerialString () {

  while(Serial.available() > 0)  // We've got the first char
  {
    byte inByte = Serial.read(); // read it.
   delay(500);  // the other chars arrive during this time
    if(inByte == '\n' || inByte == '\r')  // eventually(!), we'll empty the buffer
    {
    Serial.print("break: ");
    Serial.print(inByte);
    break;  // but we may never get here!
    }

    // Do something with the character just read...
    serInString[serInIndx]  = inByte;
    serInIndx++;
    serInString[serInIndx]= '\0';
  }
}
28  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 22, 2009, 11:00:54 am
Hi Groove, I think the boxes are from scrambled/incomplete values going over the com port.  I get similar type characters when I slow the baud down on the serial monitor.  I can't paste them here.  but they look like [] only completely enclosed.  

Just saw the break thing.  I've tried to put the delay there but that didn't work.  I'll try what you say now
29  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 22, 2009, 10:50:21 am
I checked the serial matching in both and they are the same.  I made it higher but it just got the boxes back higher.  If lower, they got garbled.  

I put a delay in but just got the boxes one at a time slowly.  

Went to other code, and yes, with delay works, without doesn't.

30  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Serial text command to pan tilt servos on: December 22, 2009, 10:38:09 am
I put the sketch back to Mem's idea (while we wait on other "multitasking" smiley-wink )  

I get 4 boxes from this in the serial read function
Serial.print(serInString[serInIndx], BYTE);

But the printSerialString seems to be never called.
Pages: 1 [2] 3