Program to control robot

What do you think about this code? How should I organize functions in the main-loop??
Thanks

WallArduino.ino (3.58 KB)

What do you think about this code?

I'm working on a phone - I can't see any code.

Post it. Use code tags.

Ok Sorry!

#include <Servo.h>

Servo ServoTestaVerticale;
Servo ServoTestaOrizzontale;
Servo BraccioDestro;
Servo BraccioSinistro;
Servo RuotaDestra;
Servo RuotaSinistra;
int Sensore_Testa = A2;
int Sensore_Corpo = A3;
const int DistanzaLimite = 15;

void Avanti();
void Indietro();
void Destra();
void Sinistra();
void Fermati();
int average_value (int average_count, int sensore);

void ArmsDown();
void ArmsUp();

void TurnAround();
void Scout();

//servomotoreorizzontale
void LookLeft();
void LookRight();
void LookCenter();

//servomotoreverticale
void LookUP();
void LookDown();
void LookMiddle();


void FollowTheFinger();
void Dance();
void WallE_Stop();


void setup() {
  Serial.begin(9600);
  ServoTestaVerticale.attach(3);
  ServoTestaOrizzontale.attach(5);
  BraccioDestro.attach(6);
  BraccioSinistro.attach(9);
  RuotaDestra.attach(10);
  RuotaSinistra.attach(11); 
}

void loop() {
  
}


int average_value(int average_count, int sensore) {
  int sum = 0;
  for (int i=0; i<average_count; i++) {
    int sensor_value = analogRead(sensore);  //read the sensor value
    int distance_cm = pow(3027.4/sensor_value, 1.2134); //convert readings to distance(cm)
    sum = sum + distance_cm;
  }
  return(sum/average_count);  
}


void ArmsUp(){
  BraccioSinistro.write(180);
}


void LookLeft() {
  ServoTestaOrizzontale.write(180); 
}

void LookRight() {
  ServoTestaOrizzontale.write(0);
}

void LookCenter() {
  ServoTestaOrizzontale.write(90);
}

/*
void ArmsDown() {
  BraccioDestro.write();
  BraccioSinistro.write();
}

void ArmsUp() {
  BraccioDestro.write();
  BraccioSinistro.write();
}
*/

void ArmsCenter() {
  BraccioDestro.write(90);
  BraccioSinistro.write(90);
}

/*
void WallE_Stop() {
  BraccioDestro.write(90);
  BraccioSinistro.write(90);
  ServoTestaVerticale.write(90);
  ServoTestaOrizzontale.write(90);
}
*/
  
void Avanti() {
  int i, j;
  for ( i=1500, j=1500; i>1000 && j<2000; i-=100, j+=100 ) {
     RuotaDestra.writeMicroseconds( i );
     RuotaSinistra.writeMicroseconds( j );
     delay(1000);
  }
}

void Indietro() {
  int i, j;
  for ( i=1500, j=1500; i<2000 && j>1000; i+=100, j-=100 ) {
     RuotaDestra.writeMicroseconds(i);
     RuotaSinistra.writeMicroseconds(j);
     delay(1000);
  }
}

void Destra(){
  int i;
  //Modificare valore 500
  for ( i=1500; i>950; i-=100 ) {
    RuotaSinistra.writeMicroseconds(i);
    delay(10000);
  }   
}
void Sinistra(){
  int i;
  //Modificare valore 2500
  for ( i=1500; i<2050; i+=100) {
    RuotaDestra.writeMicroseconds(i);
    delay(1000);
  }
}

void TurnAround() {
  int i;
  //Verificare valore 2500
   for ( i=1500; i<2500; i+=100) {
    RuotaDestra.writeMicroseconds(i);
    delay(1000);
  }
}

void Scout(){
  int DistanzaTesta, DistanzaCorpo;  
  DistanzaTesta = average_value(100, Sensore_Testa);
  DistanzaCorpo = average_value(100, Sensore_Corpo);
  if (DistanzaTesta < DistanzaLimite && DistanzaCorpo < DistanzaLimite) {
     Avanti();
  }
  else {
    Sinistra();
    DistanzaTesta = average_value(100, Sensore_Testa);
    DistanzaCorpo = average_value(100, Sensore_Corpo);
    if (DistanzaTesta < DistanzaLimite && DistanzaCorpo < DistanzaLimite) {
      Avanti();
    }
    else {
      TurnAround();
      DistanzaTesta = average_value(100, Sensore_Testa);
      DistanzaCorpo = average_value(100, Sensore_Corpo);
      if (DistanzaTesta < DistanzaLimite && DistanzaCorpo < DistanzaLimite) {
        Avanti();
      }
      else {
        Sinistra();
        Indietro();
      }
    }   
  }
  delay(10000);
}


void LookUp() {
  ServoTestaVerticale.write(180);
}

void LookDown() {
  ServoTestaVerticale.write(0);
}

void LookMiddle() {
  ServoTestaVerticale.write(90);
}

I'd get rid of the calls to delay()

Ok, other suggestions?
I want to control robot through an Android App, so how I should organize the code in the loop and in the app?

Well, before you start putting code into loop(), you really need to
a) decide what you want the code to do
b) get rid of the calls to delay()

void loop() {
  
}

Why did you write all that other code, if you have no idea what to do with it? Do you even know if any of those functions does anything useful/correctly?

How did you decide that those functions were necessary?

I got rid of the calls to delay :slight_smile:
So I want to create an Android App in which there is a menu with all the functions that the robot can do, and when I click on the function, the robot starts doing that procedure. That's why I did not include anything in the Loop.

Michele1997:
I got rid of the calls to delay :slight_smile:
So I want to create an Android App in which there is a menu with all the functions that the robot can do, and when I click on the function, the robot starts doing that procedure. That's why I did not include anything in the Loop.

So why aren't there any Android communication procedures there?

Have I missed something?

Because I want to create the app, but I have no idea how to interface it with Arduino. Can you link me some guide or give me some suggestions? Thank you

Because I want to create the app, but I have no idea how to interface it with Arduino.

I don't understand this - if you don't know how to interface Android to Arduino, how come you've got so much Arduino code?

Can you link me some guide or give me some suggestions?

I suggest that it is YOU that wants to make your Android talk to your Arduino, so YOU should be the one using google. Perhaps something in the search field like, oh, I don't know, maybe Android + Arduino.

If you're using Bluetooth to communicate, do some reseach on Bluetooth comms in Arduino and Android.

I wrote the code to verify if the Robot did correctly what I wanted. Was I wrong?
Thanks to all for suggestions

Michele1997:
I wrote the code to verify if the Robot did correctly what I wanted. Was I wrong?
Thanks to all for suggestions

Only if it didn't do what you wanted, I guess. This is test code, you mean? Well, if it works, then no worries. You can continue on with the control interface.

Since you did all that in about an hour, the rest of the job should be a piece of cake.

Ok Thanks.
Now I have to create the Android App. I am studying Java but still don't write code for app mobile. Do you think that I should use App Inventor? Or Can I try to write the Java App?

Michele1997:
Ok Thanks.
Now I have to create the Android App. I am studying Java but still don't write code for app mobile. Do you think that I should use App Inventor? Or Can I try to write the Java App?

It's really an Android/Java question. It can be better answered on an Android or Java forum.