conflict between sensors and servos

Kia ora koutou,

my first post so apologies if i'm going about this the wrong way.
I've made a robotic setup involving 8 tentacles (32 servos) and 4 ultrasonic sensors.
all the tentacles operate fine without the sensors but oddly when the sensors are going the 8th tentacle (operating from an arduino mega off pins 20, 21 ,22, 24) doesn't respond.
I assume this is my crap programming, this is my first arduino project.
Any leads would be greatly appreciated.

I totally realise the code is awful, I'm just an artist you see.

#include <Servo.h>

// define global variables
#define howmanyservos 35   // constant - how many servos to control 
#define howmanysensors 6   // constant - how many sensors to control
int uSmin=700;             // minimum is 640 / 800    range of uS for each servo at 0 dgrees                                                                             // minimum servo position in uS - adjust this value to suit your brand of servo.
int uSmax=2200;            // maximum is 1990 / 2100, halfway is about 1450 , range of servo at 180 degrees                                                                     // maximum servo position in uS - adjust this value to suit your brand of servo.
int servoposition[howmanyservos];                                                                // this array stores the current position of each servo in uS (typically 1000 - 2000 uS)
int servodirection[howmanyservos]; // this array stores the speed and direction of each servo 
int servooperation[howmanyservos]; // this array is affected by the sensors
int servolimitmin[howmanyservos];                      //uS limit min
int servolimitmax[howmanyservos];                      //uS limit max
int rangelimitN = 20; //uS divider for sensor
int rangelimitS = 20;
int rangelimitE = 20;
int rangelimitW = 20;
long previousMillis = 0;
// define servos
Servo servo[howmanyservos];   // define servos as an array for easy control

// ultra sonic sensors 

const int triggerpin = 13;

const int sensorW = 14;
const int sensorE = 16;
const int sensorS = 15;
const int sensorN = 17;
long distanceN;
long distanceS;
long distanceE;
long distanceW;


void setup()
{
   //Serial.begin(9600);           //sensor back to PC
   
  for (int i=0;i<howmanyservos;i++)                                                              // loop to initialize servos and arrays                            
  {  
    servolimitmin[i]=uSmin;
    servolimitmax[i]=uSmax;
    servoposition[i]=1450;                                                                       // start all servos at the center position
    servodirection[i]=1+((20-i)/8);     // each servo will start at a different speed and or direction
//    if (servodirection[i]=0) servodirection [i] = 1;
    servo[i].attach(53-i,uSmin,uSmax);                                                           // initialize servos starting at D53 and counting backward (limit travel between uSmin & uSmax)
    servo[i].writeMicroseconds(1450);        // set servos to center position
    delay(25);                                                                                   // delay limits power surge by initializing the servos one at a time.
  }
    servodirection[19]=1;
    servodirection[20]=2;
    servodirection[21]=-2;
    servodirection[24] = -3;

    servo[7].detach();
    servo[8].detach();
    servo[9].detach();
  
   pinMode(sensorN, INPUT);
   pinMode(sensorS, INPUT);
   pinMode(sensorE, INPUT);
   pinMode(sensorW, INPUT);// Switch signalpin to input  
   pinMode(triggerpin, OUTPUT); // set this pin to output
  //give the sensors time to boot up
   delay(250);
   // send RX pin high to signal to chain to ping
   digitalWrite(triggerpin, HIGH);
   delayMicroseconds(20);
   digitalWrite(triggerpin, LOW);
   pinMode(triggerpin, INPUT); // electrically disconnects the pin
   delay(50); 

  

}

void loop()
{
   


    pinMode (sensorN, INPUT);
    distanceN = pulseIn(sensorN, HIGH);
    myDelay (100);
    pinMode (sensorS, INPUT);
    distanceS = pulseIn(sensorS, HIGH);
    myDelay (100);
    pinMode (sensorE, INPUT);
    distanceE = pulseIn(sensorE, HIGH);
    myDelay (100);
    pinMode (sensorW, INPUT);
    distanceW = pulseIn(sensorW, HIGH);
    myDelay (100);
   

}



void myDelay(unsigned long duration)
{
  unsigned long start= millis();
  while (millis() - start <= duration)
  {
    servoDriver(25);
  }
}

//time the servos, check the limits
void servoTimer(long interval)
{
  if (millis() - previousMillis > interval)
  {
    servolimitmin[0]=uSmin+(distanceN/rangelimitN);    //north south axis one
    servolimitmax[0]=uSmax-(distanceS/rangelimitS);    //north south axis one
    servolimitmin[1]=uSmin+(distanceN/rangelimitN);  //three
    servolimitmax[1]=uSmax-(distanceS/rangelimitS);  //three
    servolimitmin[2]=uSmin+(distanceE/rangelimitE);    //east west axis one
    servolimitmax[2]=uSmax-(distanceW/rangelimitW);    //east west axis one
    servolimitmin[3]=uSmin+(distanceE/rangelimitE);    //three
    servolimitmax[3]=uSmax-(distanceW/rangelimitW);    //three
 //cut out this section of code to fit in the character limit for the forum, continues in this fashion all the way to the 35th servo pretty much  
 //   servoDriver(25);
  }  
}

//move the servos  
void servoDriver(long interval)
{
  if (millis() - previousMillis > interval)
  {
  for (int i=0;i<howmanyservos;i++)                                                              // loop to update servo positions  
  { 
    servoposition[i]+=servooperation[i];                                                         // calculate new servo position 
    if (servoposition[i]>servolimitmax[i] || servoposition[i]<servolimitmin[i]) servodirection[i]=-servodirection[i];  // reverse direction when limits are met.
    servooperation[i] = servodirection[i]*((servolimitmax[i]-servolimitmin[i])/200);
    servo[i].writeMicroseconds(servoposition[i]);      // update servo to new position
    servoTimer(25);  
  }

  }
}

here is some footage of the tentacles in action

cheers y'all,
nga mihi
S.F H.P

could it be that u r running out of SRAM?
i mean: 35 Servo structures might b quite big... and u need some stack space 2...
can u run avr-nm on ur .elf file (on linux it is somewhere in /tmp/build...)?

ah, that sounds like a good lead.
I can imagine this might be the case.
I'm away from the work for a while but i will try transferring some of the memory to the flash memory and find a way to check the SRAM usage when i get it back. (don't understand the "avr-nm" unfortunately, but will investigate further)
thanks
S>F

avr-nm gives u addresses like this:

00810000 T __eeprom_end
00800060 T _edata
00800060 T _end

:slight_smile:

i just had a look at this:

and they say it has 8KiB SRAM...
and the Servo class has just 3 bytes...

what if u use just 1 sensor?
maybe more observations help to find the root of the bug...