Arduino Mega with motor shield servo problem

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mr magoo
 
Posts: 3
Joined: Tue Jan 17, 2012 3:48 pm

Arduino Mega with motor shield servo problem

Post by mr magoo »

I have a Mega2560 with an AdaFruit motor shield connected to a servo on pin 9 and a Parallax ping sensor connected to pin 38 on the Mega2560. My code works fine until I add an array variable a[x] to the ping section of the code then the servo stops moving even though the information is still being sent to the servo. I have confirmed that the servo has +5Vdc and GND and the signal appears to be present also. I have tried a different servo with the same results.

If I rem the line a[x]=cm in the Ping function everything works as advertised.

I have the Mega2560 plugged into a 9Vdc (6 x 1.5Vdc AA) battery pack and USB. I have tried a 12Vdc battery with no change.

I also tried moving the servo to pin 10 with no change

Any help would be greatly appreciated.
Cheers!
#include <Servo.h>
Servo myservo;
int pos;
int lim1=60;
int lim2=120;
int wait=50;
const int pingPin = 38;
long duration;
long cm;
long a[3];
int res=1;

void setup()

  myservo.attach(9);
 Serial.begin(9600);
}

void loop()
{  
  for(pos = lim1; pos < lim2; pos +=1)
  {                                  
   servoScan();
   Ping();
   Report();
  }
  for(pos = lim2; pos>lim1; pos-=1)
  {                                
   servoScan();
   Ping();
   Report();
   }

long microsecondsToCentimeters(long microseconds){
  return microseconds / 29 / 2;
}
void servoScan(){
 myservo.write(pos);
    delay(wait);
  // Serial.println(pos);
}
void Ping(){

 for (int x = 0; x <= 3 ; x++) {
  delay(wait);
   pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = microsecondsToCentimeters(duration);
 a[x]=cm; // this makes the servo stop moving
}
}

void Report(){
  Serial.print(pos);
Serial.print(" degrees ");
 Serial.print(cm);
  Serial.println(" cm");
     delay(wait);
}

User avatar
mr magoo
 
Posts: 3
Joined: Tue Jan 17, 2012 3:48 pm

Re: Arduino Mega with motor shield servo problem

Post by mr magoo »

UPDATE

I dug out my old oscilloscope and checked the signal going to the servo;

With the a[x]=cm line remmed:

There is a pulse that is at an amplitude of 5.5 Vdc with period of that varies from 1 mS to 2 mS (depending upon where the servo is pointing) that repeats every 25 mS.


With the a[x]=cm line un-remmed:

No pulse - 0 Vdc amplitude....


Any ideas how I can get around this?

User avatar
mr magoo
 
Posts: 3
Joined: Tue Jan 17, 2012 3:48 pm

Re: Arduino Mega with motor shield servo problem

Post by mr magoo »

I figured out my problem. I am used to BASIC. Should have read a little closer as it is written on the Arduino website:

QUOTE:

Accessing an Array

Arrays are zero indexed, that is, referring to the array initialization above, the first element of the array is at index 0, hence

mySensVals[0] == 2, mySensVals[1] == 4, and so forth.

It also means that in an array with ten elements, index nine is the last element. Hence:

int myArray[10]={9,3,2,4,3,2,7,8,9,11};
// myArray[9] contains 11
// myArray[10] is invalid and contains random information (other memory address)


For this reason you should be careful in accessing arrays. Accessing past the end of an array (using an index number greater than your declared array size - 1) is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.

I increased my array to 4 and all is well.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”