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();
}
}
}
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
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.
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
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
In two's complement, there is no representation of -0.
Hence the
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.