Go Down

Topic: combining programs? (Read 642 times) previous topic - next topic

whiskey

May 11, 2010, 09:26 am Last Edit: May 11, 2010, 09:27 am by whiskey Reason: 1
hi,  

I'm building a robot and already I'm stuck in a rut   :-?

I've managed to get some basic movements with the arduino and a Solarbotics L298 motor driver so the next step was to add a servo that will pan from  left to right. I thought all I would have to do is copy n paste the servo code but I guess that's not the case.

Any help and simple explanation on how/why this is not working would be much appreciated!!

Code: [Select]
#include <ServoTimer1.h>

ServoTimer1 servo1;



// Solarbotic L298 Motor Controller pins
int enablePin = 2;
int LeftMotorForward = 3;
int LeftMotorBackward = 5;
int RightMotorForward = 6;
int RightMotorBackward = 9;





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

void loop()
{
 Serial.print("forward");
 forward();
 delay(3000);
 
 
 Serial.print("pause");
 digitalWrite(enablePin, LOW);
 delay(2000);
 
 
 Serial.print("backwards");
 backwards();
 delay(3000);
 }
 {
  servo1.write(180);
  delay(1000);
  servo1.write(0);
  delay(1000);
 }
 
 
 
 
 
 void forward()
 {
 digitalWrite(LeftMotorForward, LOW);
 analogWrite(LeftMotorBackward, 200);
 digitalWrite(RightMotorForward, LOW);
 analogWrite(RightMotorBackward, 200);
 digitalWrite(enablePin,HIGH);
 }
 
 
 void backwards()
 {
 analogWrite(LeftMotorForward, 200);
 digitalWrite(LeftMotorBackward, LOW);
 digitalWrite(RightMotorBackward, LOW);
 analogWrite(RightMotorForward, 200);
 digitalWrite(enablePin,HIGH);
 }
 
 


Just to be clear, the motor driver is working and the servo does work when it's on it's own.


The error I get when compiling is:

In static member funtion 'static void ServoTimer: :seizeTimer()':

and...

error: 'TIMSK' was not declared in this scope


thanks for taking a look!  8-)










mem

#1
May 11, 2010, 09:56 am Last Edit: May 11, 2010, 10:01 am by mem Reason: 1
replace :
#include <ServoTimer1.h>
ServoTimer1 servo1;

with :
#include <Servo.h>
Servo servo1;

change :
 servo.attach(10);
to :
 servo1.attach(10);

and it looks like you may have an unmatched pair of braces in the loop code



fdufnews

You should suppress the two braces in the middle of your loop()
Code: [Select]
void loop()
{
 Serial.print("forward");
 forward();
 delay(3000);
 
 
 Serial.print("pause");
 digitalWrite(enablePin, LOW);
 delay(2000);
 
 
 Serial.print("backwards");
 backwards();
 delay(3000);
 }                              |<------------|
 {                              |<------------|
  servo1.write(180) ;   |                     |
  delay(1000);          | This part of code   |
  servo1.write(0);      | is never executed   |
  delay(1000);          | because of that -----
 }
 

mem

#3
May 11, 2010, 01:22 pm Last Edit: May 11, 2010, 01:24 pm by mem Reason: 1
the problem with those braces is not that code is never executed, they prevent the code from compiling because the lines after the closing brace are not in a function

whiskey

#4
May 12, 2010, 05:17 am Last Edit: May 12, 2010, 05:20 am by whiskey Reason: 1
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.  :)  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!

Code: [Select]
#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);
 }
 
 





Go Up