What can improve at this code

This code is working but i want to improve it for this robot :

hi....i need some help from an expert in arduino program part.....in few days i will go to a contest and there are alot of good expert with good robots,what i want to ask you is :
This code is good for my robot?(my robo has 2 motors pololu,i know i should put 4 pololu but i didn't have 4 pololu so i just put only 2)
can you give me some advice about what i should improve at my code,or if you have already one to tell give mem.....
SENZOR_ALB_FATA means colour front senzor

#define SENZOR_ALB_FATA A5
int MOTOR1_PIN1 = 10;
int MOTOR1_PIN2 = 11;

int MOTOR2_PIN1 = 5;
int MOTOR2_PIN2 = 6;



void setup() {
    pinMode(49,INPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  Serial.begin(9600);
}


void go(int speedLeft, int speedRight) {
  if (speedLeft > 0) {
    analogWrite(MOTOR1_PIN1, speedLeft);
    analogWrite(MOTOR1_PIN2, 0);
  }
  else {
    analogWrite(MOTOR1_PIN1, 0);
    analogWrite(MOTOR1_PIN2, -speedLeft);
  }

  if (speedRight > 0) {
    analogWrite(MOTOR2_PIN1, speedRight);
    analogWrite(MOTOR2_PIN2, 0);
  }
  else {
    analogWrite(MOTOR2_PIN1, 0);
    analogWrite(MOTOR2_PIN2, -speedRight);
  }
}


int readDistance() {
  int sum = 0;
  for (int i=0; i<6;i++){
    float volts = analogRead(0)* ((float) 5 / 1024);
    float distance = 65*pow(volts, -1.10);
    sum = sum + distance;
    delay(5);
  }
  return (int)(sum / 6);
}

//void setup() {
  //Serial.begin(9600);
//}





void loop() {
     //go (-250, 250);
  int distance = readDistance();
  int white=analogRead(SENZOR_ALB_FATA);
 Serial.println(white);  
 // Serial.println(distance);
   if (white<1005){
         go(-250,-250);
           delay(1200);
}
  if (distance >= 100){
        while(distance>=100){
            white=analogRead(SENZOR_ALB_FATA);
             Serial.println(white);
                if (white<1005){
                   go(-250,-250);
                    delay(1000);
              }
            distance=readDistance();
            go(-250,250);
        }
 
       
    
        while(distance<100){
                 white=analogRead(SENZOR_ALB_FATA);
                  Serial.println(white);
                     if (white<1005){
                       Serial.println("going back");
                       go(-250,-250);
                       delay(1000);
              }
                //  Serial.println(white);
                  // delay(500);
                  go(250,250);
                 distance=readDistance();
                 
             }
   
           }
}

Your code is not rocket science...

I'm not sure you can use a negative value in analogwrite(), but if you say your code works... I believe you. :slight_smile:

The position controller, could be something a bit more advanced. But as far as the quality of code, goes I see no problem.

that code works....i tested it today....but i posted here because i want to know from an expert if i can make an optimization or something iproved at this code:)
or a strategy for my contest or if someone can help me with another code for a robot with an arduino platfor with 2 engines pololu

@bubulindo

I'm not sure you can use a negative value in analogwrite(), but if you say your code works... I believe you.

Point to the bit where it writes negative values, please.

@OP

#define SENZOR_ALB_FATA A5
int MOTOR1_PIN1 = 10;
int MOTOR1_PIN2 = 11;

int MOTOR2_PIN1 = 5;
int MOTOR2_PIN2 = 6;

I'd like to see these values expressed as "const int", but I'm probably just being picky.

I'd also like to see some comments about what you are trying to do with the code.

(The Ctrl-T function in the IDE will help sort out your indentation)

My intention is to make a good program for my robot

that picture bellow is my robot
i want to code for a sumo contest,that participate others with more good robots and upgrade with 4 engines (pololuls)
All i want to know is if my code bellow is good for such contest or if someone have a good code for sumocontest or just an ideea for such contest.

    pinMode(49,INPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);

You've given nice names topns 5, 6,10 and 11, why not use them here?

Not sure what pin 49 is. Maybe give a name to that too?

49 is for distance sensor

49 is for distance sensor

A digital distance sensor?

What does A5 do?

(sorry to ask all these questions, but maybe if there were more useful comments, I wouldn't have to)

49 is for distance(digital senzor)
A5 a senzor for recognize the colour(that senzor is under the robot)
SENZOR_ALB_FATA means colour front senzor and is that A5

49 is for distance(digital senzor)

Other than in "setup", I can't see any reference to pin 49.
Please help me.

int readDistance() {
int sum = 0;
for (int i=0; i<6;i++){
float volts = analogRead(0)* ((float) 5 / 1024);
float distance = 65*pow(volts, -1.10);
sum = sum + distance;
delay(5);
}
return (int)(sum / 6);
}
just for recognize it
than this is the code for distance:)

You know, like those 3D pictures, no matter how hard I stare at that code, I can't see a "49".
How do you do that?
Do you squint?
Cross your eyes?
What?

i think i shouldn't put that 49 there....i will make a test to see if i can delete that line.
but in rest the code is good?.....i mean if you have any sugestion about somthing pls tell me

?.....i mean if you have any sugestion about somthing pls tell me

Well, something here:

  if (distance >= 100){
        while(distance>=100){

looks a bit redundant to me.

Apart from the redundancy AWOL mentioned with this

while(distance>=100){
white=analogRead(SENZOR_ALB_FATA);
Serial.println(white);
if (white<1005){
go(-250,-250);
delay(1000);
}
distance=readDistance();
go(-250,250);
}
while(distance<100){
white=analogRead(SENZOR_ALB_FATA);
Serial.println(white);
if (white<1005){
Serial.println("going back");
go(-250,-250);
delay(1000);
}
// Serial.println(white);
// delay(500);
go(250,250);
distance=readDistance();

}

}

You have two while loops that basically do the same thing with the only real difference being one of the speed variables.

If you tell us what is supposed to happen someone may be able to provide more informed information.

For example, when the bot get within 100 of something it should reverse for 1 second etc etc.


Rob

AWOL:
@bubulindo

I'm not sure you can use a negative value in analogwrite(), but if you say your code works... I believe you.

Point to the bit where it writes negative values, please.

void go(int speedLeft, int speedRight) {
  if (speedLeft > 0) {
    analogWrite(MOTOR1_PIN1, speedLeft);
    analogWrite(MOTOR1_PIN2, 0);
  }
  else {
    analogWrite(MOTOR1_PIN1, 0);
    analogWrite(MOTOR1_PIN2, -speedLeft); //HERE???
  }

  if (speedRight > 0) {
    analogWrite(MOTOR2_PIN1, speedRight);
    analogWrite(MOTOR2_PIN2, 0);
  }
  else {
    analogWrite(MOTOR2_PIN1, 0);
    analogWrite(MOTOR2_PIN2, -speedRight); //HERE???
  }

Good enough for you?

bubulindo:
Good enough for you?

Not really - look what the if statements test for :wink:

wildbill:

bubulindo:
Good enough for you?

Not really - look what the if statements test for :wink:

He can still write -0 to it... :stuck_out_tongue:

He can still write -0 to it

In two's complement, there is no representation of -0.
Maybe you were thinking we used sign and magnitude?

AWOL:

He can still write -0 to it

In two's complement, there is no representation of -0.

Hence the :stuck_out_tongue:

AWOL:
Maybe you were thinking we used sign and magnitude?

The go function seemed too "hi tech" for the rest of the function that "calculated" the speeds. It's fairly normal to have that implementation when you have a more complex controller on the speeds of the wheels (normally tied to a position controller), but it is perfectly correct to use it like this, even if we only assign values for the outputs. My bad.