combining programs?

thanks for the help!

I got it up and working but found it to be a little too fast, I need it to go about half the speed that it was, it took me a few hours but finally got it figured it out. :slight_smile: in the end I ended up doing the same thing that you would to an LED during PWM.

The problem I have now is that it runs the code in the order it was written, meaning it only pans after the motor commands and only does this once, then starts the loop all over again?

If someone can point me in the right direction it would be appreciated!

#include <Servo.h>
Servo servo1;



// Solarbotic L298 Motor Controller pins
int enablePin = 2;
int LeftMotorForward = 3;
int LeftMotorBackward = 5;
int RightMotorForward = 6;
int RightMotorBackward = 9;
int servoVal;  //variable to make servo pan slowly (not sure if it needs to be here?)





void setup()
{
  
  Serial.begin(9600);
  pinMode(enablePin, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorForward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  digitalWrite(enablePin, HIGH);
  servo1.attach(10);
  
  
}

void loop()
{
  // motor controller commands
   
   Serial.print("forward");
    forward();
    delay(3000);
  
  
   Serial.print("pause");
    digitalWrite(enablePin, LOW);
    delay(2000);
  
  
   Serial.print("backwards");
    backwards();
    delay(3000);
    
   
   Serial.print("pause");
    digitalWrite(enablePin, LOW);
    delay(2000);
  
 

  
    // servo pan commands
  
  Serial.println("looking left");

   for(int x=40; x<=140; x++){   // pans 1 degree at a time till it reaches it's max limit
    int servoVal = x;           // the degrees can be changed to your liking!
    servo1.write(x);             
    delay(10);
   }
   
   
  Serial.println("looking right!");

   for(int x=140; x>=40; x--){   // pans from one limit to the other
    int servoVal = x;           // this creates a varible
    servo1.write(x);           // writes the value of the varible
    delay(10);
   }
   }



  
  // motor controller funtions
  
  void forward() // might need to swap forward/reverse depending on your bot
  {
  digitalWrite(LeftMotorForward, LOW);
  analogWrite(LeftMotorBackward, 200);
  digitalWrite(RightMotorForward, LOW);
  analogWrite(RightMotorBackward, 200);
  digitalWrite(enablePin,HIGH);
  }
  
  
  void backwards() // might need to swap forward/reverse dending on your bot
  {
  analogWrite(LeftMotorForward, 200);
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  analogWrite(RightMotorForward, 200);
  digitalWrite(enablePin,HIGH);
  }