Pages: [1] 2   Go Down
Author Topic: help. Arduino is stuck  (Read 1274 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
« Last Edit: February 26, 2013, 06:54:26 am by kevrixmamaw » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25768
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
that's the code.
Why doesn't it have [code] [/code] tags around it?

Quote
7 servos
I count 6.
« Last Edit: February 26, 2013, 06:50:06 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Servos do not have to be attached to PWM pins.

Code:
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...?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25768
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4061
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does it work if you unplug all but one of the servos ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sorry, I couldn't resist.

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

Code:
#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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4061
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
byte i;
  for (i=0; i<6; i++) {
How quaint.   smiley-lol
Logged

Per Arduino ad Astra

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
byte i;
  for (i=0; i<6; i++) {
How quaint.   smiley-lol
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)...
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1] 2   Go Up
Jump to: