Go Down

Topic: My Servo won't move [SOLVED] (Read 2 times) previous topic - next topic

InventiousTech

Nov 14, 2012, 02:41 am Last Edit: Nov 14, 2012, 09:03 pm by InventiousTech Reason: 1
I have a program that receives data over Serial1 of the mega 2560. The data is a series of bytes that represent a number in english from about -10 to 10, followed by an 'E'. anyway, what this is supposed to do is map the number to 0 to 179 for the servo, and write it to the motor. The problem is that after initially moving the servo to 90, nothing happens to the servo. I know from the Serial output that the values are being manipulated correctly, so it is definitely a problem with the implementation of Servo. The hardware is right because the servo sweep example works. Any ideas? Also, if anyone knows an easier way to cast from a character array to a float, that would be much appreciated.

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

char byteIn;
char numberChar[6];
int sVal;
int x;
int y;
float number = 0.000;
Servo myservo;

void setup() {
 Serial1.begin(9600);       // start serial communication at 9600bps
 Serial.begin(9600);
 myservo.attach(9);
 myservo.write(90);
}

void loop()
{
 if (Serial1.available())
 {
   byteIn = Serial1.read();
   if (byteIn == 'E')
   {
    x = 0;
    y = 0;
    if (numberChar[0]=='-')\
    {
     y = 1;
    }
    number = numberChar[0+y]-'0';
    number += ((numberChar[2+y]-'0')/10.0);
    number += ((numberChar[3+y]-'0')/100.0);
    number += ((numberChar[4+y]-'0')/1000.0);
    if (numberChar[0]=='-')\
    {
     number = number*-1;
    }
    sVal = (int)map(number, -10, 10, 0, 179);
    myservo.write(sVal);
    Serial.println(number);
    Serial.println(sVal);
    delay(10);
   }
   else
   {
    numberChar[x] = byteIn;
    x++;
   }
 }
}

PeterH

Those Serial print statements are rather anonymous so it'd be easy to be mistaken about which value is which. I suggest you prefix each output with the name of the value you're printing. If you still think the number processing part is correct, then post the input and output from running the sketch and describe what the servo does.
I only provide help via the forum - please do not contact me for private consultancy.

InventiousTech

Wow, that was fast. Ok. how about this:

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

char byteIn;
char numberChar[6];
int sVal;
int x;
int y;
float number = 0.000;
Servo myservo;

void setup() {
  Serial1.begin(9600);       // start serial communication at 9600bps
  Serial.begin(9600);
  myservo.attach(9);
  myservo.write(90);
}

void loop()
{
  if (Serial1.available())
  {
    byteIn = Serial1.read();
    if (byteIn == 'E')
    {
     x = 0;
     y = 0;
     if (numberChar[0]=='-')\
     {
      y = 1;
     }
     number = numberChar[0+y]-'0';
     number += ((numberChar[2+y]-'0')/10.0);
     number += ((numberChar[3+y]-'0')/100.0);
     number += ((numberChar[4+y]-'0')/1000.0);
     if (numberChar[0]=='-')\
     {
      number = number*-1;
     }
     sVal = (int)map(number, -10, 10, 0, 179);
     myservo.write(sVal);
     Serial.print("Raw value: ");
     Serial.println(number);
     Serial.print("Servo angle: ");
     Serial.println(sVal);
     Serial.println();
     delay(10);
    }
    else
    {
     numberChar[x] = byteIn;
     x++;
    }
  }
}


The input is actually coming from a python script running on scripting layer for android (SL4A) over bluetooth through a JY-MCU module to the Serial1 input, so it's a little hard to tell you exactly what is coming in, given that the data goes through a couple layers of android data manipulation then the bluetooth module, but I can tell you with reasonable certainty that the value coming in (for this example) is "5.057" as a series of bytes through Serial1. The output is

Raw value: 5.06
Servo angle: 134


So yes, I am pretty sure the data is right. The servo essentially does nothing. Hence the problem, since it started at 90 degrees and should be at 134.

AWOL

Can I introduce you to a good friend of mine?

How is the servo connected and powered?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Code: [Select]
     number = numberChar[0+y]-'0';
     number += ((numberChar[2+y]-'0')/10.0);
     number += ((numberChar[3+y]-'0')/100.0);
     number += ((numberChar[4+y]-'0')/1000.0);

0, 2, 3, 4? Are you missing a finger?

Go Up