Pages: [1]   Go Down
Author Topic: combining programs?  (Read 533 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 81
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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-)









« Last Edit: May 11, 2010, 02:27:57 am by whiskey » Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


« Last Edit: May 11, 2010, 03:01:46 am by mem » Logged

France
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3010
There is an Arduino for that
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should suppress the two braces in the middle of your loop()
Code:
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 -----
  }
  
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: May 11, 2010, 06:24:44 am by mem » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 81
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley  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:
#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);
  }
  
  



« Last Edit: May 11, 2010, 10:20:17 pm by whiskey » Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The problem I have now is that it runs the code in the order it was written
Usually there is a  problem if the code does not run in the order written  smiley-wink


Can you provide some more details about what should happen
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 505
Posts: 31332
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's all those delays. Look up the blink without delay example and apply it to your code.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

Only if you are trying it then put a resistor in the LED
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 81
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so I'm back and now with a little more knowledge under my belt, thanks for the link grumpy mike!

BUT... for some reason I still can't get the proper outcome, I know grumpy has said it's because of my delay's and it actually makes "sense" that they are not needed but without them the pan does a very sharp move then that's it.

here's my assumption on what's going on:

~ I'm creating my "current time"

~ I'm subtracting my current time from my "previous time" (which at the moment is zero)

~ if the statement is true it runs "pan servo"

this is the problem...

~ "pan servo" runs it's full cycle from the min-max-min degrees

~ loop starts over again running the motors then pan


... what I need it to do is move 1 degree "tag it's time", wait 10 millis, move to the next degree, etc... all while the other programs are running (in other words the panning need to be done in the background).

... it seems like I need to replace "delay" with somthing like "previous time" so it will tag it's time in 10 millisecond increments? If you have a look in the program you can see that I tried that but I get the error:
"Ivalue required as left operand of assignment"

I'm stumped once again, but in a god way! any help with this would be appreciated.

here's my code, I took out the motor commands to minimize the clutter.

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

long servoDelay = 10;  // intervals at which to pan servo (milliseconds)
unsigned long previousTime; //represents the timing of the "cycle"


void setup()
{

 unsigned long currentTime = millis();
  
 if(currentTime - previousTime > servoDelay){  // if the time exceeds 10 milliseconds run "panServo"
      panServo();
      previousTime = currentTime; // "resets" the loop time
    }
  }

// pan servo funtions
void panServo()
{

  for(int x=40; x<=140; x++){
    servo1.write(x);
    //delay (10);
   millis() = previousTime;
    }
  
  for(int x=140; x>=40; x--){
    servo1.write(x);
    //delay (10);
    millis() = previousTime;
  }
}

thanks for looking 8-)
« Last Edit: May 22, 2010, 02:08:46 pm by whiskey » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24286
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This:
Code:
 for(int x=40; x<=140; x++){
    servo1.write(x);
    //delay (10);
   millis() = previousTime;
    }
will simply do 100 "servo.write"s and 100 assignments to "millis()" (which is a function and therefore and illegal lvalue) without stopping.
It isn't surprising it sweeps from one end of the travel to the other.
You can't use a "for" loop for this sort of "blink-without-delay" application, you need to code the loop explicitly, kind-of turning it inside-out.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: