My arduino freezing.

Hi,

I’m trying to moving servo motor with arduino UNO.

3~4times my servo worked, but it stopped suddenly.
I think my circuit are correct.

I suspect it’a code problem.
here is my code.

#include <VarSpeedServo.h>

// hold servo angle
int servoAngle[] = {90, 90, 90, 90};

// designate servo pin
int servo[] = {10, 11, 12, 13};

// Input pin for pushing button
int firstLeft = 2;
int firstRight =3;
int secondLeft = 4;
int secondRight = 5;
int thirdLeft = 6;
int thirdRight = 7;
int forthLeft = 8;
int forthRight = 9;

int currentPosition = 0;

int activePin = 0;

void setup(){
  Serial.begin(115200);
  int countServo = sizeof servoAngle / sizeof servoAngle[0];
  servoSetup(countServo);
  pinMode(firstLeft, INPUT_PULLUP);
  pinMode(firstRight, INPUT_PULLUP);
  pinMode(secondLeft, INPUT_PULLUP);
  pinMode(secondRight, INPUT_PULLUP);
  pinMode(thirdLeft, INPUT_PULLUP);
  pinMode(thirdRight, INPUT_PULLUP);
  pinMode(forthLeft, INPUT_PULLUP);
  pinMode(forthRight, INPUT_PULLUP);
}

void  servoSetup(int countServo){
  int i = 1;
  for(i=countServo -1; i==0; i--){
    VarSpeedServo temp_servo;
    temp_servo.attach(servo[i]);
    temp_servo.write(servoAngle[i], 30, true);
  }
}

int pinState(int pin){
  int state = digitalRead(pin);
  return state;
}

int getInputNumber(){
  int i = 1;
  for (i=firstLeft; i<forthRight+1; i++){
    if(pinState(i) == 0){
      return i;
    }
  }
  if(i = 1){
    return 0;
  }
  free(i);
}

int getServoList(int activePin){  
  if(activePin == firstLeft || activePin == firstRight){
    return 0;
  }
  if(activePin == secondLeft || activePin == secondRight){
    return 1;
  }
  if(activePin == thirdLeft || activePin == thirdRight){
    return 2;
  }
  if(activePin == forthLeft || activePin == forthRight){
    return 3;
  }
}

int getServoNumber(int activePin){
  int servoNumber = getServoList(activePin);
  return servo[servoNumber];
}

int getServoAngle(int activePin){
  int servoNumber = getServoList(activePin);
  return servoAngle[servoNumber];
}  

void servoPositioning(){
  VarSpeedServo tempServo;
  tempServo.attach(servo[0]);
  tempServo.write(servoAngle[0], 30, true);
}

int getDirection(int activePin){
  if(activePin % 2 == 0){
    return 0;
  }
  if(activePin % 2 != 0){
    return 1;    
  }
}

void actuateServo(int direction, int servoNumber){
  VarSpeedServo tempServo;
  tempServo.attach(servoNumber);
  if(180 > currentPosition > 0){
    if(direction == 0){
      currentPosition = currentPosition - 10;
      Serial.println("TURN LEFT");
    }
    else{
      currentPosition = currentPosition + 10;
      Serial.println("TURN RIGHT");
    }  
    tempServo.write(currentPosition, 30, true);
  }
  tempServo.detach();
}


void testServo(){
  VarSpeedServo tempServo;
  tempServo.attach(servo[0]);
  tempServo.write(30);
}

void memorizeServo(int currentPosition, int activePin){
  int servoList = getServoList(activePin);
  servoAngle[servoList] = currentPosition;
}


void loop()
{
  // check inputed pin
  activePin = getInputNumber();
  delay(5000);
  Serial.println("NO SIGNAL");
  if(activePin != 0){
    Serial.println("INPUT PIN IS:");
    Serial.println(activePin);

    int direction = getDirection(activePin);
    Serial.println("DIRECTION IS:");
    Serial.println(direction);

    int servoNumber = getServoNumber(activePin);
    Serial.println("SERVO NUMBER IS:");
    Serial.println(servoNumber);

    currentPosition = getServoAngle(activePin);
    Serial.println("CURRENT POSITION IS:");
    Serial.println(currentPosition);

    actuateServo(direction, servoNumber);

    memorizeServo(currentPosition, activePin);

    activePin = 0;
    free(currentPosition);
  }
}
  int i = 1;
  for(i=countServo -1; i==0;

If you don't want the loop to ever run, just comment it out.

if(i = 1){

oops

 free(i);

:o

if(180 > currentPosition > 0){More oopsieness.

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How are you providing power for the servo? What model servo, please?

Tom.. Australia

Hi, AWOL.

Thanks for reply! Actually, this is my first C code, and I'm sorry,I didn't check C manual enough time.

Corrected my code.

  1. int i = 1; for(i=countServo -1; i==0;

=> for(int i=countServo -1; i > 0; i--){

  1. if(i = 1){

=>if(i == 1){

  1. free(i);

=> just remove

  1. if(180 > currentPosition > 0){

=> if(180 > currentPosition && currentPosition > 0){

Thanks in advance!

Hi, Tom,

Thanks for reply!

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks for the information, I should check it.

This is my circuit :o https://drive.google.com/file/d/1YNIsicmNSzLMaFl577-PzekcEvKpCb00/view?usp=sharing

Diode is IS4 http://akizukidenshi.com/download/1S3.pdf Register is 10kΩ

I'm using four AA battery for providing power to servo motor.

My servo is Tower Pro MG996R http://www.electronicoscaldas.com/datasheet/MG996R_Tower-Pro.pdf

Mizuki, Japan

Hi.
OPs circuit,
circuit.png
You need to connect the gnd of the servo supply directly to the UNO gnd, you should remove the resistor.
There is also no need for the diode.
You press button circuit is shorting the 5V UNO supply if you follow your diagram.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Trace your project wiring out and draw your circuit from that.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Also you code does not need line numbers.

Any images or code files need to be attached to you post not posted off forum, some forum users use a different platform to PC/Laptop and this makes it difficult for them to view your images.

Thanks… Tom… :slight_smile: