Go Down

Topic: robot that should turn left on its own and keep itself going strait (Read 1 time) previous topic - next topic

PaulS

Perhaps if you used random indenting, and random amounts of white space, you could mask where the curly braces don't match better.

There is a reason that code is indented, and that people make the effort to match indent levels for { and } (and put them on lines by themselves).

There's even a tool in the IDE to format the code for you.

lloyddean

PaulS is much like sandpaper.  He starts off in the 40-60 grit range and, if you can stand the "rub", eventually works his way to applying a 80-120 grit.

In other words - he's a but rough at first but if you're patient things'll eventually smooth out and you learn things along the way.

drab

lol ok ill just do that then. i rewrote my code tryng to use functions and subroutines like a good feller. and like everyone has been saying to do in the post i have read. it compiles fine but wont start up my motordriver everything else works great te ping ans sweep do exactley what  want them to do. but no motors :( pls help i am currently trying again and race for class is next week :( i know motordriver works i can get it to work on its own and with the sweep and ping and respond to the ping but would like to have the subroutines and the servo dont like not hving the delays between the pings

Code: [Select]
#include <Servo.h>
Servo myservo;  // create servo object to control a servo  
const int ping = 8;  //ping sensor pin number
int pos = 90;    // variable to set the initial position of the servo
int reset = 2;// motordriver reset pin number

void setup()
{
Serial.begin(9600); //sets serial communication bps
pinMode (reset, OUTPUT);
digitalWrite(reset,LOW);
delay(50);
digitalWrite(reset,HIGH);
delay(50);
myservo.attach(10);  // attaches the servo on pin 10 to the servo object
myservo.write(90); //center servo
delay(400); //delay 1000ms  
}


void loop()
{

long duration, inches, inches2, inches3; // establish variables for duration of ping, inches = 90; inches2 = 20; inches3 = 140

myservo.write(90); //move servo to pos 0
delay(5);
pinMode(ping, OUTPUT);
digitalWrite(ping, LOW);
delayMicroseconds(2);
digitalWrite(ping, HIGH);
delayMicroseconds(5);
digitalWrite(ping, LOW);

pinMode(ping, INPUT);
duration = pulseIn(ping, HIGH);

inches = microsecondsToInches(duration);
delay (400);

myservo.write(20); //move servo to pos 20
delay(5);

pinMode(ping, OUTPUT);
digitalWrite(ping, LOW);
delayMicroseconds(2);
digitalWrite(ping, HIGH);
delayMicroseconds(5);
digitalWrite(ping, LOW);

pinMode(ping, INPUT);
duration = pulseIn(ping, HIGH);

inches2 = microsecondsToInches(duration);
delay (400);

myservo.write(90); //move servo to pos 0
delay(5);

pinMode(ping, OUTPUT);
digitalWrite(ping, LOW);
delayMicroseconds(2);
digitalWrite(ping, HIGH);
delayMicroseconds(5);
digitalWrite(ping, LOW);
pinMode(ping, INPUT);
duration = pulseIn(ping, HIGH);
inches = microsecondsToInches(duration);
delay (400);

 myservo.write(140); //move servo to pos 0
          delay(5);
pinMode(ping, OUTPUT);
digitalWrite(ping, LOW);
delayMicroseconds(2);
digitalWrite(ping, HIGH);
delayMicroseconds(5);
digitalWrite(ping, LOW);
pinMode(ping, INPUT);
duration = pulseIn(ping, HIGH);
inches3 = microsecondsToInches(duration);
delay (400);

myservo.write(90); //move servo to pos 0
delay(5);

pinMode(ping, OUTPUT);
digitalWrite(ping, LOW);
delayMicroseconds(2);
digitalWrite(ping, HIGH);
delayMicroseconds(5);
digitalWrite(ping, LOW);
pinMode(ping, INPUT);
duration = pulseIn(ping, HIGH);
inches = microsecondsToInches(duration);
delay(100);

if(inches > 50 )
forward();

if (inches2 < 12)
rightWall(100);


if (inches3 < 12)
leftWall(100);

if (inches < 50 && inches3 > 52)
leftTurn(2000);

if (inches < 50 && inches2 > 52)
rightTurn(2000);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;  
}
void wall() {
 long duration, inches, inches2, inches3; // establish variables for duration of ping, inches = 90; inches2 = 20; inches3 = 140
{

}
}

void forward(){
//left motor
unsigned char buff1[6];
buff1[0]=0x80; //start byte
buff1[1]=0x00; //device type
buff1[2]=0x00; //motor number and directiobyte; motor one =00,01
buff1[3]=0x7F; //motor speed 0 to 128 in hex (ex 100 is 64 hex)
for (int i=0; i<4; i++) {Serial.print(buff1[i],BYTE);}

//right motor
unsigned char buff2[6];
buff2[0]=0x80;
buff2[1]=0x00;
buff2[2]=0x02; //motor and direction byte; motor two=02.03
buff2[3]=0x7E;//7E is 126 in hex; 7D is 125 in hex set to smaller value to compensate for uneven wheel speed???
for(int i=0; i<4; i++)  {Serial.print(buff2[i],BYTE);}
}
void rightWall(int duration)
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80;
buff3[1]=0x00;
buff3[2]=0x00;
buff3[3]=0x40;
for(int i=0; i<4; i++){Serial.print(buff3[i], BYTE);}

//right motor
unsigned char buff2[6];
buff2[0]=0x80;
buff2[1]=0x00;
buff2[2]=0x02; //motor and direction byte; motor two=02.03
buff2[3]=0x7D;
for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
}

void leftWall(int duration)
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80;
buff3[1]=0x00;
buff3[2]=0x00;
buff3[3]=0x7F;
for(int i=0; i<4; i++) { Serial.print(buff3[i], BYTE);}

//right motor
unsigned char buff2[6];
buff2[0]=0x80;
buff2[1]=0x00;
buff2[2]=0x02; //motor and direction byte; motor two=02.03
buff2[3]=0x7D;
for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
}
void reverse(int duration)
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80;
buff3[1]=0x00;
buff3[2]=0x01;
buff3[3]=0x45;
for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}

//right motor
unsigned char buff2[6];
buff2[0]=0x80;
buff2[1]=0x00;
buff2[2]=0x03; //motor and direction byte; motor two=02.03
buff2[3]=0x45;
for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
}

void leftTurn(int duration)
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80;
buff3[1]=0x00;
buff3[2]=0x00;
buff3[3]=0x45;
for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}

//right motor
unsigned char buff2[6];
buff2[0]=0x80;
buff2[1]=0x00;
buff2[2]=0x02; //motor and direction byte; motor two=02.03
buff2[3]=0x7D;
for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
}

void rightTurn(int duration)
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80;
buff3[1]=0x00;
buff3[2]=0x00;
buff3[3]=0x7F;
for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}

//right motor
unsigned char buff2[6];
buff2[0]=0x80;
buff2[1]=0x00;
buff2[2]=0x02; //motor and direction byte; motor two=02.03
buff2[3]=0x45;
for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
}




AWOL

Do yourself a favour - shorten your code with a single range function, instead of typing the same code over and over again:
Code: [Select]
long getRangeInInches (int ping)
{
pinMode(ping, OUTPUT);
digitalWrite(ping, LOW);
delayMicroseconds(2);
digitalWrite(ping, HIGH);
delayMicroseconds(5);
digitalWrite(ping, LOW);

pinMode(ping, INPUT);
long duration = pulseIn(ping, HIGH);

return microsecondsToInches(duration);
}


With shorter code, it may make it easier for you to see where you are overwriting results.

Do the rest of us a favour by typing ctrl-T in the IDE before posting your code.
"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.

AWOL

Humans are really bad at repetition; computers are really good at it.
Code: [Select]
void forward()
{
  moveMotor (0, 0x7F);
  moveMotor (2, 0x7E);
}

void rightWall(int duration)
{
  moveMotor (0, 0x40);
  moveMotor (2, 0x7D);
}

void leftWall(int duration)
{
  moveMotor (0, 0x7F);
  moveMotor (2, 0x7D);
}

void reverse(int duration)
{
  moveMotor (1, 0x45);
  moveMotor (3, 0x45);
}

void leftTurn(int duration)
{
  moveMotor (0, 0x45);
  moveMotor (2, 0x7D);
}

void rightTurn(int duration)
{
  moveMotor (0, 0x7F);
  moveMotor (2, 0x45);
}

void moveMotor (unsigned char motor, unsigned char speed)
{
  unsigned char buff[4];
  //left motor
  buff[0]=0x80;
  buff[1]=0x00;
  buff[2]=motor;
  buff[3]=speed;
  for(int i=0; i<4; i++) {
    Serial.print(buff[i], BYTE);
  }
}


Reducing source size gives bugs fewer dusty corners to hide in.
"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.

Go Up