Ramping w/ servos using the Uno R3

Hi,

I have two servos controlling a little bot. I am using this to learn how to use the Arduino. I also did many general tutorials to get familiar. Anyways, I want my bot to move forward, turn one way, turn the other way, and reverse back all with ramping into and out of each maneuver. It almost all works well. It goes forward, turns, turns, and comes back and all the ramping except at the very end it just keeps reversing. I feel like I should see what's wrong here but I just don't. Maybe I've been looking at it too long, but I had this issue yesterday, thought maybe I just needed a break and picked it up again today. No luck. I will post the whole sketch because it's not very big. The problem is at the end of the last maneuver, full speed backwards w/ ramping.

// ForwardLeftRightBackwardRamping// Move forward, left, right, then backward for testing and tuning w/ ramping.


#include <Servo.h>                           // Include servo library


Servo servoLeft;                             // Declare left and right servos
Servo servoRight;
 
void setup()                                 // Built-in initialization block
{ 
  tone(4, 3000, 1000);                       // Play tone for 1 second
  delay(1000);                               // Delay to finish tone


  servoLeft.attach(13);                      // Attach left signal to P13 
  servoRight.attach(12);                     // Attach right signal to P12


  // Full speed forward
  for(int speed = 0; speed <= 100; speed += 2) 
  {
    servoLeft.writeMicroseconds(1500+speed);   
    servoRight.writeMicroseconds(1500-speed);  
    delay(20);                                 
  }
   
  delay(1500);                                 


  for(int speed = 100; speed >= 0; speed -= 2) 
  {
    servoLeft.writeMicroseconds(1500+speed);   
    servoRight.writeMicroseconds(1500-speed);  
    delay(20);                                 
  }                            


  // Turn left in place
    for(int speed = 0; speed <= 100; speed += 2) 
  {
    servoLeft.writeMicroseconds(1500-speed);   
    servoRight.writeMicroseconds(1500-speed);  
    delay(20);                                 
  }


  for(int speed = 100; speed >= 0; speed -= 2)
  {
    servoLeft.writeMicroseconds(1500-speed);   
    servoRight.writeMicroseconds(1500-speed);  
    delay(20);                                 
  }                           


  // Turn right in place
  for(int speed = 0; speed <= 100; speed += 2) 
  {
    servoLeft.writeMicroseconds(1500+speed);   
    servoRight.writeMicroseconds(1500+speed);  
    delay(20);                                 
  }


  for(int speed = 100; speed >= 0; speed -= 2)
  {
    servoLeft.writeMicroseconds(1500+speed);   
    servoRight.writeMicroseconds(1500+speed);  
    delay(20);                                 
  }                             


  // Full speed backward
    for(int speed = 0; speed <= 100; speed -= 2) 
  {
    servoLeft.writeMicroseconds(1500+speed);   
    servoRight.writeMicroseconds(1500-speed);  
    delay(20);                                 
  }
   
  delay(1500);                                 


  for(int speed = -100; speed >= 0; speed += 2) 
  {
    servoLeft.writeMicroseconds(1500+speed);   
    servoRight.writeMicroseconds(1500-speed); 
    delay(20);                                 
  }                             
  
  
  
  servoLeft.detach();                       
  servoRight.detach(); 
}  


void loop()                                  
{                                            
}

Any help will be much appreciated. I did look through the forum for answers, but didn't see any. Thanx again.

Syn7

  for(int speed = -100; speed >= 0; speed += 2) 
  {
    servoLeft.writeMicroseconds(1500+speed);   
    servoRight.writeMicroseconds(1500-speed); 
    delay(20);                                 
  }

Starting with speed at -100, while speed is greater than or equal 0, do some stuff, then increment speed by 2. How many times do you think the "do some stuff" part is going to happen? Yep, that's right. None.

    for(int speed = 0; speed <= 100; speed -= 2) 
  {
    servoLeft.writeMicroseconds(1500+speed);   
    servoRight.writeMicroseconds(1500-speed);  
    delay(20);                                 
  }

Starting with speed at 0, while speed is less than or equal 100, do some stuff, then decrement speed by 2. How many times do you think the "do some stuff" part s going to happen? 0, -2, -4, -6, -10, ..., -32768, 2, 4,... That loop will take quite a while to complete.

Syn7:

    for(int speed = 0; speed <= 100; speed -= 2) 

for(int speed = -100; speed >= 0; speed += 2)

It looks as if these have been copied-and-pasted from the code to move forwards, but the loop constraints aren't right. If you want the first loop to go from 0 to -100, the condition needs to be speed >= -100. If you want the second loop to go from -100 to 0, the constraint needs to be speed <= 0.

Thanx.

This worked.

// ForwardLeftRightBackwardRamping// Move forward, left, right, then backward for testing and tuning w/ ramping.


#include <Servo.h>                           // Include servo library


Servo servoLeft;                             // Declare left and right servos
Servo servoRight;
 
void setup()                                 // Built-in initialization block
{ 
  tone(4, 3000, 1000);                       // Play tone for 1 second
  delay(1000);                               // Delay to finish tone


  servoLeft.attach(13);                      // Attach left signal to P13 
  servoRight.attach(12);                     // Attach right signal to P12


  // Full speed forward
  for(int speed = 0; speed <= 100; speed += 2) 
  {
    servoLeft.writeMicroseconds(1500+speed);   
    servoRight.writeMicroseconds(1500-speed);  
    delay(20);                                 
  }
   
  delay(1500);                                 


  for(int speed = 100; speed >= 0; speed -= 2) 
  {
    servoLeft.writeMicroseconds(1500+speed);   
    servoRight.writeMicroseconds(1500-speed);  
    delay(20);                                 
  }                            


  // Turn left in place
    for(int speed = 0; speed <= 100; speed += 2) 
  {
    servoLeft.writeMicroseconds(1500-speed);   
    servoRight.writeMicroseconds(1500-speed);  
    delay(20);                                 
  }


  for(int speed = 100; speed >= 0; speed -= 2)
  {
    servoLeft.writeMicroseconds(1500-speed);   
    servoRight.writeMicroseconds(1500-speed);  
    delay(20);                                 
  }                           


  // Turn right in place
  for(int speed = 0; speed <= 100; speed += 2) 
  {
    servoLeft.writeMicroseconds(1500+speed);   
    servoRight.writeMicroseconds(1500+speed);  
    delay(20);                                 
  }


  for(int speed = 100; speed >= 0; speed -= 2)
  {
    servoLeft.writeMicroseconds(1500+speed);   
    servoRight.writeMicroseconds(1500+speed);  
    delay(20);                                 
  }                             


  // Full speed backward
   for(int speed = 0; speed <= 100; speed += 2) 
  {
    servoLeft.writeMicroseconds(1500-speed);   
    servoRight.writeMicroseconds(1500+speed);  
    delay(20);                                 
  }
   
  delay(1500);                                 


  for(int speed = 100; speed >= 0; speed -= 2) 
  {
    servoLeft.writeMicroseconds(1500-speed);   
    servoRight.writeMicroseconds(1500+speed);  
    delay(20);                                 
  }                            

  
  servoLeft.detach();                       
  servoRight.detach(); 
}  


void loop()                                  
{                                            
}

Just switched the plus/minus when in reverse from the forward part. I had that idea in the very beginning, but I was not looking for a quick fix, rather trying to understand the for speed thing. Anyone know how I could have fixed it with the for int speed statements? Or did I do this time what I should have done from the get go when I first thought of it?

Thanx for your help.

Or did I do this time what I should have done from the get go when I first thought of it?

Yes.

Thanx

And thank you, Peter. While I didn't do it your way, I did mess with it and it was a great self lesson. Much appreciated. Eventually I stuck with just swapping the +/- cause it was just easier for me. But at least I actually understand what kept me confused before. It was one of those things where once I knew what was wrong I felt kind of dumb, but I had so much code on the brain after such long sessions, I doubt I would have found it on my own w/o taking a day off and coming back to it completely fresh.

Again, much appreciated.

I learned....!!!