Go Down

Topic: arduino crushes after receiving multiple inputs for servos (Read 374 times) previous topic - next topic

james_angelakis

hello i am new to arduino and i want to move two servos. The problem is that if i send multiple inputs(for example if i send aaaarrrrrrrllllllddddddd ) from the keybord arduino crashes and the servos doesnt move. When i send one input at a time it works perfect.I have used delay() and serialflush() but they didnt seem to do the trick .The main goal is to receive multiple  inputs and the servos react properly without crushing.Is this even possible?? .If you guys can help me i will be more than grateful. Here is my code

Code: [Select]
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2; // twelve servo objects can be created on most boards

#define SERVO_PIN 9
#define SERVO_PIN_2 11

int pos = 0;    // variable to store the servo position
int pos2 = 0;
void setup()
{
  Serial.begin(9600);
  myservo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(SERVO_PIN_2);
 
  myservo.write(90);
  myservo2.write(90);
}

void loop() {
 
  myservo.attach(SERVO_PIN);
 
  if (Serial.available()) {
     char c = Serial.read();
     if (c=='d') {
       pos = pos +10;
       myservo.write(pos);
       
     
       Serial.flush();
     
     }
     myservo.attach(SERVO_PIN);
     
     if (c=='a') {
       pos = pos -10;
       myservo.write(pos);
       
       
       Serial.flush();
     
     } 
 
   myservo2.attach(SERVO_PIN_2);
       if (c=='l'){
        pos2 = pos2 +20;
       myservo2.write(pos2);
       

       Serial.flush();}

  myservo2.attach(SERVO_PIN_2);
     if (c=='r'){
     pos2 = pos2 -20;
     myservo2.write(pos2);
   
     Serial.flush();

       
     }     
      }
        }       

wildbill

No need to attach the servos more than once - just do it in setup.
Forget serial.flush - it's doing you no good.

With the example string of commands you show, you're initially asking the first servo to move to position -40 which will not go down well. Try initializing pos & pos2 to 90.   

duino_nano

Hi,

Some servos draw a fair amount of current, 200mA or more. If you have more than one servo running at the same time, it is possible that the Arduino voltage drop too much (depending of your supply) and it will crash and reset.

I know that by experience!!!

Measure the +5V of the Arduino while moving 2 servos; you will see.

james_angelakis

thanx guys for the replies. Do you have any idea if it is better to put a while instead of an if? or if it is better to go for case/break on the commands? or even to put the virables in an array? it is critical for my project to have multiple inputs(i measure brainwaves to move the servos). I have no idea if it is even possible to accept only one of multiples inputs(example if i send via serial aaaaaaaaa the arduino would read only the first and drop the others somehow). If you have any idea you will save my day really. Again thnx a lot for your time and for you replies guys.

MarkT

Hi,

Some servos draw a fair amount of current, 200mA or more. If you have more than one servo running at the same time, it is possible that the Arduino voltage drop too much (depending of your supply) and it will crash and reset.

I know that by experience!!!

Measure the +5V of the Arduino while moving 2 servos; you will see.
Budget more like 1A per servo for small servos, more for high torque or large servos.  The supply should
not be shared with the Arduino 5V, servos and motors should be separately powered so the Arduino 5V
rail is clean from spikes and drop-outs.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

james_angelakis

hi again guys i think i found a solution but am not sure yet

here is the code
Code: [Select]

//
// Servo motor thesis
//
// Made by James Angelakis
// License: CC-BY-SA 3.0


#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2; // twelve servo objects can be created on most boards

#define SERVO_PIN 9
#define SERVO_PIN_2 11

int pos = 90;    // variable to store the servo position
int pos2 = 90;
void setup()
{
  Serial.begin(9600);
  myservo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(SERVO_PIN_2); // attaches the servo on pin 11 to the servo object
 
  myservo.write(90);
  myservo2.write(90);
}

void loop() {
 
  delay(500);
  if (Serial.available()) {
    char c = Serial.read();
    switch (c) {
      case 'd':
       pos = pos +25;
       myservo.write(pos);
       Serial.end();
       
       Serial.begin(9600);
         break;
         
      case 'a':
       pos = pos -25;
       myservo.write(pos);
      Serial.end();
     
      Serial.begin(9600);
       break;
     
      case 'r':
       pos2 = pos2 +25;
       myservo2.write(pos2);
       Serial.end();
       
       Serial.begin(9600);
         break;
     
       case 'l':
       pos2 = pos2 -25;
       myservo2.write(pos2);
       
       Serial.end();
       
       Serial.begin(9600);
       break;
     
     default:
      myservo.write(90);
      myservo2.write(90);
     
      break;
    } }    }

Go Up