Go Down

Topic: help. Arduino is stuck (Read 1 time) previous topic - next topic

kevrixmamaw

Feb 26, 2013, 12:06 pm Last Edit: Feb 26, 2013, 12:54 pm by kevrixmamaw Reason: 1
as I turn on the arduino and the 7 servos using PWM controlled by the potentiometers the servo freezes.
the 7 servo is powered by an external power supply

Code: [Select]

#include <Servo.h>   
Servo myservo;   // create servo object to control a servo
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;




int potpin = 0;  // analog pin used to connect the potentiometer
int potpin1 = 1;
int potpin2 = 2;
int potpin3 = 3;
int potpin4 = 4;
int potpin5 = 5;
int val;    // variable to read the value from the analog pin
void setup()
{
  myservo.attach(3);  // attaches the servo digital input
   myservo1.attach(5);
    myservo2.attach(6);
     myservo3.attach(9);
      myservo4.attach(10);
       myservo5.attach(11);
       
}
void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);   
  myservo.write(val);                   
  delay(15);                           // waits for the servo to get there
    val = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo1.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
    val = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 179, 0);     // scale it to use it with the servo (value between 0 and 180)
  myservo2.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
    val = analogRead(potpin3);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 179, 0);     // scale it to use it with the servo (value between 0 and 180)
  myservo3.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
    val = analogRead(potpin4);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 179, 0);     // scale it to use it with the servo (value between 0 and 180)
  myservo4.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
    val = analogRead(potpin5);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo5.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
 
}

that's the code.

AWOL

#1
Feb 26, 2013, 12:48 pm Last Edit: Feb 26, 2013, 12:50 pm by AWOL Reason: 1
Quote
that's the code.

Why doesn't it have [font=Verdana][[/font]code] [font=Verdana][[/font]/code] tags around it?

Quote
7 servos

I count 6.
"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.

kevrixmamaw

sorry about that, newbie here. 2 servos are attached to pin 5. is that ok?

PaulS

Servos do not have to be attached to PWM pins.

Code: [Select]
Servo myservo;   // create servo object to control a servo
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

Is that how you count? Nothing, one, two, three...?

AWOL

You know you don't have to use PWM pins to drives servos using the Servo library?
Two servos on one pin should be fine.
Your code is about 30 lines too long.
Check your power wiring - make sure you've got a common ground and that the wires are beefy enough to carry the current.
"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.

UKHeliBob

Does it work if you unplug all but one of the servos ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

majenko

I'm sorry, I couldn't resist.

This is how you should (IMHO) be writing your program:

Code: [Select]

#include <Servo.h>   

// This is what we want in our servo settings array
typedef struct {
  byte servoPin;
  byte analogPin;
  byte angle;
} servoconfig;

// Array to store settings for servos
servoconfig servodata[6] = {
  {3, 0, 0},
  {5, 1, 0},
  {6, 2, 0},
  {9, 3, 0},
  {10, 4, 0},
  {11, 5, 0}
};

Servo servos[6];  // Array to store actual servos

// Get new ADC values and map them to angles.  Store them in the
// settings array
void getAngles()
{
  byte i;
  int dummyValue;
  for (i=0; i<6; i++) {
    dummyValue = analogRead(servodata[i].analogPin); // Dummy read and short delay to clear S&H cap and ensure good readings
    delay(10);
    servodata[i].angle = map(analogRead(servodata[i].analogPin), 0, 1023, 0, 179);
  }
}

// Update the servos with the angles in the config array
void updateServos()
{
  byte i;
  for (i=0; i<6; i++) {
    servos[i].write(servodata[i].angle);
  }
}

// Set everything up.
void setup()
{
  byte i;
  for (i=0; i<6; i++) {
    servos[i].attach(servodata[i].servoPin);
    servos[i].write(0);
  }
}

// Do the work
void loop()
{
  getAngles();
  updateServos();
}


Much cleaner and easier to understand, yes?

I have loaded it onto one of my UNOs and tested that it gives pulses out on the right pins when pots change.  I don't have any servos to test it with though.

PaulS

Code: [Select]
servoconfig servodata[6] = {
Going to be hard to fit all 7 servos in that array, no?

billroy

Sometimes these problems are fun to work in Bitlash.  If you'll spot me moving the servos to D2..D7 so I can use the serial port it's a two-liner something like this:

Code: [Select]

// Six pots on A0..A5 control six servos on D2..D7
// (with Bitlash servo example sketch on Arduino)

function startup { run setservo; }
function setservo { servo(i+2, ar(i)*180/1024); if (++i>5) i=0; }


-br
http://bitlash.net

UKHeliBob

Quote
Much cleaner and easier to understand, yes?

Cleaner, yes, but easier to understand, I am not so sure.
That is going to depend on what you know and understand already.
You know and appreciate structs, arrays and functions but I am willing to bet that the OP doesn't.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Groove

Code: [Select]
byte i;
  for (i=0; i<6; i++) {
How quaint.   XD
Per Arduino ad Astra

kevrixmamaw

I think my external power supply is not working. is it ok to put the battery direct to the servos? I'm using 4 AA's =6v

majenko


Code: [Select]
servoconfig servodata[6] = {
Going to be hard to fit all 7 servos in that array, no?

Not when, as the OP states, two servos share one pin.

majenko


Quote
Much cleaner and easier to understand, yes?

Cleaner, yes, but easier to understand, I am not so sure.
That is going to depend on what you know and understand already.
You know and appreciate structs, arrays and functions but I am willing to bet that the OP doesn't.

Then the OP can use it to learn about structs, arrays and functions.

majenko


Code: [Select]
byte i;
  for (i=0; i<6; i++) {
How quaint.   XD

What's quaint about that?

You'd rather for (byte i=0; ... ?  Some versions of C (or the IDE's default settings) don't allow that kind of construct, so rather than try and remember which do and which don't, I just don't use it.  Or is it the "i" that you find quaint?  After decades of programming I have become used to "i" being "iterator" and I use it for pretty much all loops.  Yes, it started out with systems that only allow single character variables (BASIC) back in the dark ages (when computers came in black cases)...

Go Up