-1

I have some problem with my fyp, which is auto serving of food using train.

I have 4 servo motors, first servo motor's function is to connect the track to table no.1, and second motor for table no.2 and third and fourth motor for table no.3.

All the motors are performing well at first time.. but when I reset and give command for second or third table, motors behavior changes and they start rotating in arbitrary directions which are not defined in code. but sometimes it works good and sometimes motors are not performing as expected.

I have check all the grounds and powers. they are all good.

Please help me, as I have my final submission in two days.

The code is given below.. Please check and tell me if I have a logical error in my code or any other issue regarding to hardware also tell me is their any cache in arduino, which I need to clean or something like that.

#include <Servo.h>

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

const int button1 = A1; 
const int button2 = A2;
const int button3 = A3; 

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;


void setup() {

  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);

  servo1.attach(9);
  servo2.attach(11);
  servo3.attach(6);
  servo4.attach(5);  

  Serial.begin(9600);
}


void loop() {   

  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
  buttonState3 = digitalRead(button3);

  if (buttonState1 == HIGH) {
    int pos1 = 180;
    int pos2 = 180;
    int pos3 = 90;
    int pos4 = 90;

    for (int pos2=90; pos2 <= 180; pos2 += 1) {
      servo2.write(pos2);
      delay(0);
    }
    for (int pos1=90; pos1 <= 180; pos1 += 1) {
      servo1.write(pos1);
      delay(50);
    }

    while (pos1 >= 0 && pos2 >= 0 && pos3 >= 0 && pos4 <= 180) {
//      servo1.writeMicroseconds (1000);

//      servo1.write(pos1);
//      servo2.write(pos2);
      servo3.write(pos3); 
      servo4.write(pos4);
      delay(200000);                // 500ms delay
    }

  }
  else if (buttonState2 == HIGH) {

    int pos5 = 90;
    int pos6 = 0;

    for (int pos5=90; pos5 >= 0; pos5 -= 1) {
      servo1.write(pos5);
      delay(0);
    }
    for (int pos6=90; pos6 >= 0; pos6 -= 1) {
      servo2.write(pos6);
      delay(50);
    }

    while ( pos6 <= 180 && pos5 <= 180) {

//      servo2.write(pos6);
//      servo1.write(pos5);
      delay(200);              
    } 
//      while ( pos5 <= 180) {
//        servo1.write(pos5);
//        delay(200);              
//      } 

  }
  else if (buttonState3 == HIGH) {
    int pos9 = 90;
    int pos10 = 180;
    int pos11 = 0;
    int pos12 = 180;

    for (int pos10=90; pos10 <= 180; pos10 += 1) {
      servo2.write(pos10);
      delay(0);
    }
    for (int pos9=90; pos9 >= 0; pos9 -= 1) {
      servo1.write(pos9);
      delay(0);
    }
    for (int pos11=90; pos11 >= 0; pos11 -= 1) {
      servo3.write(pos11);
      delay(30);
    }
    for (int pos12=90; pos12 <= 180; pos12 += 5) {
      servo4.write(pos12);
      delay(50);
    }

    while ( pos9 <= 180 && pos10 >= 0 && pos11 >= 0 && pos12 >= 0) {

//      servo1.write(pos9);
//      servo2.write(pos10);
//      servo3.write(pos11);
//      servo4.write(pos12);
//      delay(200);              
    } 
//      while ( pos9 <= 180) {
//        servo1.write(pos9);
//        delay(200);              
//      }
  }

}
  • Try to write a minimal example that works for only one of your "tracks" and then you may see where the problem lies. – MichaelT Apr 23 at 15:40
1

I think reason for which you are not getting the expected result is because of you while loops in you if condition.

As you loops doesn't have any termination condition. In case of first button pressed you will get what you want because you have only single not terminating while loop, while in you Case of button 2 and button 3 there are two while loop's and both are not terminating.

In that case if you logic goes in first while loop, then it never comes out of it and won't go for second loop.

I don't know what are you up to so can't suggest on logic changes, but add termination in while loop might get you expected results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.