Servo Programming

Hello,
I am trying to make a robot that also has an ultrasonic sensor in front of it. I am using two servo motors. But the problem is one of them is continuous rotation servo and the other is 180 degrees. I plugged another power source for motors. They should stop when I press the button that I've put on the breadboard. I am using the code below but it doesn't work for me. What is wron with me? Please help!

#include <Servo.h>

// create servo objects
Servo leftMotor;
Servo rightMotor;

const int serialPeriod = 250;       // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 20;          // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay   = 0;

// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 8;
const int ultrasonic2EchoPin = 9;

int ultrasonic2Distance;
int ultrasonic2Duration;

// define the states
#define DRIVE_FORWARD     0
#define TURN_LEFT         1

int state = DRIVE_FORWARD; // 0 = drive forward (DEFAULT), 1 = turn left

void setup()
{
    Serial.begin(9600);
  
    // ultrasonic sensor pin configurations
    pinMode(ultrasonic2TrigPin, OUTPUT);
    pinMode(ultrasonic2EchoPin, INPUT);
    
    leftMotor.attach(13);
    rightMotor.attach(12);
}


void loop()
{
    if(digitalRead(2) == HIGH) // if the push button switch was pressed
    {
        while(1)
        {
            leftMotor.write(90);
            rightMotor.write(90);
        }
    }

    debugOutput(); // prints debugging messages to the serial console
    
    if(millis() - timeLoopDelay >= loopPeriod)
    {
        readUltrasonicSensors(); // read and store the measured distances
        
        stateMachine();
        
        timeLoopDelay = millis();
    }
}


void stateMachine()
{
    if(state == DRIVE_FORWARD) // no obstacles detected
    {
        if(ultrasonic2Distance > 6 || ultrasonic2Distance < 0) // if there's nothing in front of us (note: ultrasonicDistance will be negative for some ultrasonics if there's nothing in range)
        {
            // drive forward
            rightMotor.write(180);
            leftMotor.write(0);
        }
        else // there's an object in front of us
        {
            state = TURN_LEFT;
        }
    }
    else if(state == TURN_LEFT) // obstacle detected -- turn left
    {
        unsigned long timeToTurnLeft = 500; // it takes around .5 seconds to turn 90 degrees
        
        unsigned long turnStartTime = millis(); // save the time that we started turning

        while((millis()-turnStartTime) < timeToTurnLeft) // stay in this loop until timeToTurnLeft (.5 seconds) has elapsed
        {
            // turn left
            rightMotor.write(180);
            leftMotor.write(180);
        }
        
        state = DRIVE_FORWARD;
    }
}


void readUltrasonicSensors()
{
    // ultrasonic 2
    digitalWrite(ultrasonic2TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic2TrigPin, LOW);
    
    ultrasonic2Duration = pulseIn(ultrasonic2EchoPin, HIGH);
    ultrasonic2Distance = (ultrasonic2Duration/2)/29;
}
//

void debugOutput()
{
    if((millis() - timeSerialDelay) > serialPeriod)
    {
        Serial.print("ultrasonic2Distance: ");
        Serial.print(ultrasonic2Distance);
        Serial.print("cm");
        Serial.println();
        
        timeSerialDelay = millis();
    }
}

but it doesn't work for me.

Grrrr.

Cengizhan19:
But the problem is one of them is continuous rotation servo and the other is 180 degrees.

What happens when you solve that problem?
Seems like an easy matter to get the correct type of servo.

...R

        while(1)
        {
            leftMotor.write(90);
            rightMotor.write(90);
        }

This will cause the real servo to go to its 90 degree position and stay there but the other (non)servo may continue to rotate if 90 degrees is not exactly at the mid position. This is because the (non)servo has no way of knowing that it has reached the position that it was commanded to go to. In fact it is not a servo at all, it is a motor whose speed and direction is controlled by a PWM signal.

You may be able to stop the rotation of the (non)servo by experimenting with values around 90 in the write command to find a value at which it stops or for more accuracy use the writeMicroseconds() command.

If the servos are being used to drive a robot's wheels then it is not going to go very far using a real servo but you have not said what the servo and (non)servo are being used for exactly.

As Robin2 has hinted you seem to have a hardware problem not a programming problem.