Maxsonar/Maxbotix Array

Hi everyone,

I need to create an array for my maxbotix sonar sensor. I'm using 6 maxsonar - AnalogPins 0 to 5. Each sonar controls a servo. Ex. Servo0 attach to digital pin 2 goes with analogpin 0 maxsonar.
Servo opens when analog 0 -if (analogRead(0)< 110){
else servo is close and always sends pin val- to Max Jitter.

I'm also using Max Jitter to get the analog pin data to control video and audio.

Here is the code so far- Awol help with the servo array-

If you have any suggestions please do reply. THANKS

#include <Servo.h>

#define N_SERVOS 6

Servo myServo [N_SERVOS];
const byte servoPin [N_SERVOS] = {2, 3, 4, 5, 6, 7};




int pos = 0;
int pos2 = 0;
 
 
void setup() { 
  
  Serial.begin(9600); 
  for (int i = 0; i < N_SERVOS; ++i) {
  myServo [i].attach (servoPin [i]);
  
  }
}

 
void loop()  { 
  
 [glow] if (analogRead(0)< 110){
    Serial.print (analogRead(0));
    Serial.println();
    moveTo(170);          
  }
  
  else{
    Serial.print (analogRead(0));[/glow]
    Serial.println();
    moveTo(10);  
  }
}

void moveTo(int newPosition){
  int degreesPerStep =  1;  // decrease this to slow movement
 
  int currentPos = myServo.read();
  int movement = newPosition - currentPos; // the  number of degrees to move
  if(movement < 0){
    while(currentPos > newPosition){
      currentPos = currentPos - degreesPerStep;  
      myServo.write( currentPos);
      delay(20);
    }
  }
  else{ // movement is >0 )
    while(currentPos < newPosition){
      currentPos = currentPos + degreesPerStep;  
      myServo.write( currentPos);
      delay(20);
    }
  }
}

Thanks,
Larissa

Have a look at the Arduino IDE examples

File->Examples->Control->ForLoopIteration
File->Examples->Control->Arrays

and I think you'll see examples about how to do very similar things to many pins.

Have an experiment with those, especially the Arrays one, and see how you get on. Please ask if you need more help.

HTH
GB

HI gbulmer,

Thanks for the reply but it seems that I need an exact command
to name the sonars other than int before void setup like the:

#define N_SERVOS 6

Servo myServo [N_SERVOS];
const byte servoPin [N_SERVOS] = {2, 3, 4, 5, 6, 7};

I'm not sure of what to do> I have tried everything and nothing works- I really need help :frowning: I have open studios this week...

I need an exact command to name the sonars other than int

What do you mean by "name"? - there's no mention of sonars in your original post.

I need to create an array for my maxbotix sonar sensor.

I just need to create an array for the 6 maxbotix sonars that I'm using.
I already have the servos array and I tried to use the examples (array/loop) in arduino and it did not work.

it did not work.

In what way did it not work?
Did it not compile, or did it not behave as you expected when you ran it?
Can you post a link to the sensor?

Hi Awol,

It did not compile. http://www.maxbotix.com/

Sparkfun Ultrasonic Range Finder - LV-MaxSonar-EZ2 - SEN-08503 - SparkFun Electronics

thanks, larissa

I don't see your code.

I dont have the code in this computer- I'm in school. I can post it later.
I tried something similar to the arrays example in arduino-

int [glow]___?[/glow][] = {0, 1, 2, 3, 4, 5 }; // an array of pin numbers 

int pinCount = 6;

I just need that each (6) sonar to control each (6) servo (sonar 1 = servo 1) and send the analog readings to max jitter (which is working).
When the sonar goes to 110 or less the servo motor opens 170 degrees and stays open until the sonar sends more than 110-the servo goes to 10 degrees and stays close.
The first code work perfectly with one servo and one sonar. I only need to figure out how to write the other 5 servos and sonar's.
I already did the servo array that you kindly suggested, but I need to do the same with the sonar- and see if it works.
Thanks, larissa

const byte mySonar [] = {0, 1, 2, 3, 4, 5 }; // an array of pin numbers

Hey Awol-Thanks again.

Ok, now I go this but when I verify it is saying-

In function 'void loop()':
error: too few arguments to function 'void moveTo(int, int)

Here is my new code:

#include <Servo.h>

#define N_SERVOS 6

Servo myServo [N_SERVOS];
const byte servoPin [N_SERVOS] = {2, 3, 4, 5, 6, 7};

const byte sensorPin [N_SERVOS] = {0, 1, 2, 3, 4, 5};


int pos = 0;
int pos2 = 0;


void setup() {

      Serial.begin(9600);
      for (int i = 0; i < N_SERVOS; ++i) {
            myServo [i].attach (servoPin [i]);

      }
}


void loop()  {

      for (int i = 0; i < N_SERVOS; ++i) {

            if (analogRead(sensorPin[i])< 110){
                  Serial.print (analogRead(sensorPin[i]));
                  Serial.println();
                  moveTo(170);          
            }

            else{
                  Serial.print (analogRead(sensorPin[i]));
                  Serial.println();
                  moveTo(10);  
            }

      }

}

void moveTo(int whichServo, int newPosition){
      
      int degreesPerStep =  1;  // decrease this to slow movement

      int currentPos = myServo[whichServo].read();
      int movement = newPosition - currentPos; // the  number of degrees to move
      if(movement < 0){
            while(currentPos > newPosition){
                  currentPos = currentPos - degreesPerStep;  
                  myServo[whichServo].write( currentPos);
                  delay(20);
            }
      }
      else{ // movement is >0 )
            while(currentPos < newPosition){
                  currentPos = currentPos + degreesPerStep;  
                  myServo[whichServo].write( currentPos);
                  delay(20);
            }
      }
}

The error report is correct

void moveTo(int whichServo, int newPosition){

is declared as needing two parameters, two values, whichServo, and newposition.

but in the code in loop(), it says:

void loop()  {

      for (int i = 0; i < N_SERVOS; ++i) {

            if (analogRead(sensorPin[i])< 110){
                  Serial.print (analogRead(sensorPin[i]));
                  Serial.println();
                  [glow]moveTo(170);[/glow]        
            }

            else{
                  Serial.print (analogRead(sensorPin[i]));
                  Serial.println();
                  [glow]moveTo(10);[/glow]  
            }

      }

}

The code is only handing over one value. I think you need to fix this.