Go Down

Topic: etch-a-sketch continuous servo code issue (Read 1 time) previous topic - next topic

sirbow2

ok so i have two parallax continuous servos, and i easily made them turn using the sweep sample program. then i hooked them up to a etch-a-sketch and they make a continuous repeated pattern(still using sweep program).

but then i tired to write a program to control them over serial. i would send some thing like: <U,0500,> over serial to go up on the etch-a-sketch for 500ms. after i program the arduino and start the program but before i send that line(<U,0500,>) to the arduino over serial, one of the two servos start turning by its self. i added a serial.print line before when the servos(s) are actually supposed to turn to see if the code is some how "skipping" the serial stuff.

thanks

CODE:
Code: [Select]
#include <Servo.h>

Servo Xaxis;
Servo Yaxis;

int started = 0;
int inData[9];
int ended = 0;
char index = 0;
int final = 0;

void setup()
{
 Xaxis.attach(5);
 Yaxis.attach(6);

 Serial.begin(9600);
}

void loop()
{
 while(Serial.available() > 0)
 {
   char aChar = Serial.read();
   if(aChar == '<')
   {
     started = true;
     index = 0;
     inData[index] = '\0';
   }
   else if(aChar == '>')
   {
     ended = true;
   }

   else if(started)
   {
     inData[index] = aChar;
     index++;
     inData[index] = '\0';            
   }

   else if (aChar =='*')
   {
     final = true;
   }
 }

 if(started && ended)
 {
   const char*  strDelimiter = ",";

   char* p;
   char str[7] = {index}; //U,0500,
   int time;
   char dir;

   if ( p = strtok(str, strDelimiter) )
   {
     dir = atoi(p);
   }
   if ( p = strtok(str, strDelimiter) )
   {
     time = atoi(p);
   }

   // Get ready for the next time
   started = false;
   ended = false;
   index = 0;
   inData[index] = '\0';  

   Move(dir, time);
 }
}

//Servos move the dot on etch-a-sketch screen Xin/sec
void Move(char dir, int time)
{
 if (dir == 'U')
 {
   Yaxis.write(180);  
   delay(time);  
  Serial.print("Yaxis U for " time "ms");
 }
 
 if (dir == 'D')
 {
   Yaxis.write(0);
   delay(time);
   Serial.print("Yaxis D for " time "ms");
 }
 
 if (dir == 'L')
 {
   Xaxis.write(0);
   delay(time);
   Serial.print("Xaxis L for " time "ms");
 }
 
 if (dir == 'R')
 {
   Xaxis.write(180);
   delay(time);
   Serial.print("Xaxis R for " time "ms");
 }
}
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

PaulS

Code: [Select]
  Xaxis.attach(5);
  Yaxis.attach(6);

Before attaching the servo, you should write some known value to it.

Quote
i added a serial.print line before when the servos(s) are actually supposed to turn to see if the code is some how "skipping" the serial stuff.

And?

Code: [Select]
    char str[7] = {index}; //U,0500,
Why are you initializing this variable to index? Doesn't make sense.

Code: [Select]
    if ( p = strtok(str, strDelimiter) )
You should be parsing inData. The str variable is junk.

Code: [Select]
    if ( p = strtok(str, strDelimiter) )
    {
      time = atoi(p);
    }

The two calls to strtok will return the same value. To continue parsing the same array, to get the next value, subsequent calls to strtok have NULL as the first argument.


sirbow2

Quote
Before attaching the servo, you should write some known value to it.

ahhh thats why my servos twitch alittle when the start. i moved:
Code: [Select]
  Yaxis.write(90);
  Xaxis.write(90);

before attaching them, now no twitching :) thanks.

Quote
Why are you initializing this variable to index? Doesn't make sense.
You should be parsing inData. The str variable is junk.

because if i use inData i get:
error: cannot convert 'int*' to 'char*' for argument '1' to 'char* strtok(char*, const char*)'

Quote
The two calls to strtok will return the same value. To continue parsing the same array, to get the next value, subsequent calls to strtok have NULL as the first argument.

ahh ok so like:
Code: [Select]
    if ( p = strtok(str, strDelimiter) )
    {
      dir = atoi(p);
    }
    if ( p = strtok(NULL, strDelimiter) )
    {
      time = atoi(p);
    }

do i even need the if statements? can i just:
Code: [Select]
   
p = strtok(str, strDelimiter);
dir = atoi(p);

p = strtok(NULL, strDelimiter);
time = atoi(p);

also is atoi needed? it just converts from acii to integer. and dir isnt an integer so it would work, right? so just:
Code: [Select]
time = p;
and
dir = p;


thanks!
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

PaulS

Quote
because if i use inData i get:
error: cannot convert 'int*' to 'char*' for argument '1' to 'char* strtok(char*, const char*)'

Code: [Select]
int inData[9];
This variable should of of type char.

Quote
can i just:

You need the if tests. If the strtok operation fails, because of bad input, it returns a NULL pointer. Passing a NULL pointer to atoi() is not a good idea.

Quote
also is atoi needed? it just converts from acii to integer. and dir isnt an integer so it would work, right?

dir isn't an int, but time is.

sirbow2

#4
Oct 16, 2011, 04:38 pm Last Edit: Oct 16, 2011, 04:43 pm by sirbow2 Reason: 1
Quote
This variable should of of type char.

ok i see, thanks.

ok well having a letter in the serial "command" is causing issues(the U in <U,0500,>) i put  a serial.print to return the value of dir after getting it from serial and writing to a char, and it was some weird square shape symbol. so i just made it a number instead so its <1,0500,>

in the line char inData[9]; should the number(currently 9) be the number of characters with or without the "< >" like: <1,0500,> without the <>, so it would be 5, right?

but it does work... probably because of the dir  was causing issues. thanks!
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

Go Up