Author Topic: Programming Help!  (Read 7301 times)

0 Members and 1 Guest are viewing this topic.

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Programming Help!
« on: February 21, 2009, 08:04:03 PM »
I have this program. I want both servos to move at the same time. Also I don't want them to go forward and back. Like a servo would go, than go back, than the next one would.

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

Servo right;
Servo left;

int pos = 0;

void setup()
{
  right.attach(9);
  left.attach(10);
}

void loop()
{
  for(pos = 0; pos < 360; pos += 5)
  {
    right.write(pos);
    delay(15);
  }
    for(pos = 0; pos < 360; pos -= 5)
  {
    right.write(pos);
    delay(15);
  }
}

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Programming Help!
« Reply #1 on: February 21, 2009, 09:08:46 PM »
What does this mean?
Code: [Select]
pos += 5

You probably mean
Code: [Select]
pos++
and for pos=- , you probably mean pos--;
« Last Edit: February 21, 2009, 09:10:15 PM by airman00 »
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

paulstreats

  • Guest
Re: Programming Help!
« Reply #2 on: February 21, 2009, 09:14:11 PM »
its just another way of incremeting the pos variable by 5. so it could be pos+5 or pos=pos+5 etc.. its just written the wrong way for the context its in.

Hes actually going the complete wrong way about moving a servo. you dont need to increment each step individually, you just tell it the definite position to move to and it goes there. If they need moving incrementally then just create a global change which will move servo1 1 increment then servo2 1 increment continuously rather than incrementing each servo individually

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Programming Help!
« Reply #3 on: February 21, 2009, 09:16:49 PM »
If I change the Red and Black wires around, will my servo move the other way?

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Programming Help!
« Reply #4 on: February 21, 2009, 09:19:20 PM »
If I change the Red and Black wires around, will my servo move the other way?
no

but you can open up the servo and reverse the wires to the motor
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Programming Help!
« Reply #5 on: February 21, 2009, 09:24:22 PM »
Is there a way I can program a servo to go forward, and the other to go back, constantly, because how I have my servos they are different ways.

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: Programming Help!
« Reply #6 on: February 21, 2009, 09:49:15 PM »
for(pos = 0; pos < 360; pos += 5)
  {
    right.write(pos);
    left.write(360 - pos);
    delay(15);
  }

This way when right is one way, left will be the other, etc.

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Programming Help!
« Reply #7 on: February 21, 2009, 10:37:00 PM »
I tried it but didn't work. I think I will just change the motor wires so I can program it much easier.

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Programming Help!
« Reply #8 on: February 22, 2009, 12:48:06 AM »
Don't change the wires to the motors, your problem is with the software and that would only mask it. What will you do when you actually want your robot go backwards? You'll absolutely need to do that with software, can't run around your robot with a soldering iron switching wires to the motors :)

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Programming Help!
« Reply #9 on: February 22, 2009, 07:30:54 AM »
backwords would be easy. I just switch the + to a - basicly. But how my set up is I don't know how. Because I can only get 1 wheel to spin at a time. And it will go the 360 degrees and than back. Than the next wheel will do it.

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Programming Help!
« Reply #10 on: February 22, 2009, 08:11:09 AM »
This is precisely why I say you've got a SOFTWARE problem, not a HARDWARE problem. You can't escape not fixing the software problem because next you'll try to do something really innovative like implementing an photovore algorithm or an object avoidance algorithm and that would require your wheels to spin under software control! At times you might want one wheel to stay put while the other rotates forward, at other times you might want both wheels to rotate in the same direction or you might want your wheels to rotate in opposite directions!

Changing the two wires on the motor doesn't fix your software, it just makes it insignificantly easier to write your software. If the help you get from changing the two wires on the motors would be significant you'd see people doing it all the time - but you don't see that!

Unfortunately I can't help you with your software problem because I don't know what you're using. It appears to be Roboduino code but I'm not sure - and I don't "speak" roboduino :) - but I strongly suggest fixing the software, not the hardware.

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Programming Help!
« Reply #11 on: February 22, 2009, 10:45:23 AM »
Will do. I will get to work on the code soon. I will not go switching around the wires.

paulstreats

  • Guest
Re: Programming Help!
« Reply #12 on: February 22, 2009, 11:21:26 AM »
this type of thing should move both servo's independantly and at the same time.

whats happening?

first of all we created a loop to loop for 1000 times.
then we increment or decrement the servos depending on its direction flag. if the servo position reaches 360 the flag changes and so it knows next time to decrement.

then we tell the servo's to move 1 stage each.

it goes back to the beginning of the loop, sets the new position then moves the servo's to the next stage then back to the top of the loop etc...

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

Servo right;
Servo left;

int pos = 0;

void setup()
{
  right.attach(9);
  left.attach(10);

unsigned int leftposition = 0;
unsigned int rightposition = 360;
unsigned byte leftflag = 0;
unsigned byte rightflag = 1;
}

void loop()
{
insigned int looper = 1000;
while(looper >= 0){

if(leftflag == 0){
leftposistion+=5;
if (leftposition >= 360){
leftflag = 1;
}

if(rightflag == 0){
rightposistion+=5;
if (rightposition >= 360){
rightflag = 1;
}

if(leftflag == 1){
leftposistion-=5;
if (leftposition <= 0){
leftflag = 0;
}

if(rightflag == 1){
rightposistion-=5;
if (rightposition <= 0){
rightflag = 0;
}

right.write(rightposition);
left.write(leftposition);
delay(50);

}
looper--;

}


This will probably achieve what your code was aiming at while controlling the servo's individually, but it is still the wrong way to use modified servo's (i presume they are modified since you are expecting them to turn 360 degrees)
« Last Edit: February 22, 2009, 11:26:21 AM by paulstreats »

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Programming Help!
« Reply #13 on: February 22, 2009, 01:19:12 PM »
 In function 'void setup()':
error: invalid combination of multiple type-specifiers In function 'void loop()':

paulstreats

  • Guest
Re: Programming Help!
« Reply #14 on: February 22, 2009, 01:48:34 PM »
just remove the "unsigned" part. If you get any other error then change the "byte" to "int" aswell. Also you probably want to move all 4 of these variables to the top of the code where you declared the "pos" variable

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Programming Help!
« Reply #15 on: February 22, 2009, 03:03:34 PM »
 In function 'void loop()':
error: 'leftflag' was not declared in this scope

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

Servo right;
Servo left;

int pos = 0;


void setup()
{
  right.attach(9);
  left.attach(10);
int leftposition = 0;
int rightposition = 360;
int leftflag = 0;
int rightflag = 1;
}

void loop()
{
int looper = 1000;
while(looper >= 0){

if(leftflag == 0){
leftposistion += 5;
if (leftposition >= 360){
leftflag = 1;
}

if(rightflag == 0){
rightposistion+=5;
if (rightposition >= 360){
rightflag = 1;
}

if(leftflag == 1){
leftposistion-=5;
if (leftposition <= 0){
leftflag = 0;
}

if(rightflag == 1){
rightposistion-=5;
if (rightposition <= 0){
rightflag = 0;
}

right.write(rightposition);
left.write(leftposition);
delay(50);

}
looper--;

}

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: Programming Help!
« Reply #16 on: February 22, 2009, 03:07:07 PM »
Move
Code: [Select]
int leftflag = 0;
int rightflag = 1;

from the setup() to the beginning of loop()

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Programming Help!
« Reply #17 on: February 22, 2009, 03:12:19 PM »
MOAR ERRORz

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

Servo right;
Servo left;

int pos = 0;

void setup()
{
  right.attach(9);
  left.attach(10);
}

void loop()
{
int leftposition = 0;
int rightposition = 360;
int leftflag = 0;
int rightflag = 1;
int looper = 1000;
while(looper >= 0){

if(leftflag == 0){
leftposition += 5;
if (leftposition >= 360){
leftflag = 1;
}

if(rightflag == 0){
rightposition+=5;
if (rightposition >= 360){
rightflag = 1;
}

if(leftflag == 1){
leftposition-=5;
if (leftposition <= 0){
leftflag = 0;
}

if(rightflag == 1){
rightposition-=5;
if (rightposition <= 0){
rightflag = 0;
}

right.write(rightposition);
left.write(leftposition);
delay(50);

}
looper--;

}

In function 'void loop()':
error: a function-definition is not allowed here before '{' token

it is at the bottom???
« Last Edit: February 22, 2009, 03:20:35 PM by offy »

paulstreats

  • Guest
Re: Programming Help!
« Reply #18 on: February 22, 2009, 06:22:40 PM »
Code: [Select]
#include <Servo.h>

Servo right;
Servo left;

int pos = 0;

void setup()
{
  right.attach(9);
  left.attach(10);
}

void loop()
{
int leftposition = 0;
int rightposition = 360;
int leftflag = 0;
int rightflag = 1;
int looper = 1000;
while(looper >= 0){

if(leftflag == 0){
leftposition += 5;
if (leftposition >= 360){
leftflag = 1;
}

if(rightflag == 0){
rightposition+=5;
if (rightposition >= 360){
rightflag = 1;
}

if(leftflag == 1){
leftposition-=5;
if (leftposition <= 0){
leftflag = 0;
}

if(rightflag == 1){
rightposition-=5;
if (rightposition <= 0){
rightflag = 0;
}

right.write(rightposition);
left.write(leftposition);
delay(50);

} //DELETE THIS BRACKET!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
looper--;

}

 


data_list