Go Down

Topic: reading numbers and chars from MIT Inventor App (Read 263 times) previous topic - next topic

alecks_teo

Hello everyone! I am sorry if this is not where i must write but I am new here and I am lost. I really need some help please.
I have a project for school to finish this week and i hope somebody could help me. I build a 4dof robot arm (which has 4 servos) mount on a moving platform which has 4 DC motors.

And I have to control the 4 DC motors and the other 4 servo motors with an app made with MIT App Inventor.
And my problem is that i receive numeric data from sliders and texts/ a string of characters from the buttons which controls the my moving platform.

I made two programs, one controlling just the servos and another one controlling just the platform.
And now i don't know how I can read all this data and not to mix it up.
I could leave the two programmes here if that helps.

I still can't find a better topic. I have a robot so i think this is the best topic where I should write.
I try to put the 2 programmes in just one but it still doesn't work as it should.

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

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

int bluetoothTx = 10;
int bluetoothRx =11;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

String readString;


//Motoare partea stanga

int const enA=0;
int const enB=1;
int const inMotorStangaFataA=6;
int const inMotorStangaFataB=7;
int const inMotorStangaSpateA=4;
int const inMotorStangaSpateB=5;

//Motoare partea dreapta

int const enC=2;
int const enD=3;
int const inMotorDreaptaFataA=8;
int const inMotorDreaptaFataB=9;
int const inMotorDreaptaSpateA=12;
int const inMotorDreaptaSpateB=13;

void setup() {
  Serial.begin(9600);
  bluetooth.begin(9600);
   
  myservo1.attach(19);
  myservo2.attach(18);
  myservo3.attach(17);
  myservo4.attach(16);

  Serial.setTimeout(50);

  myservo1.write(0);
  myservo2.write(160);
  myservo3.write(160);
  myservo4.write(90);

 
  pinMode(enA, HIGH);
  pinMode(enB, HIGH);
  pinMode(enC, HIGH);
  pinMode(enD, HIGH);
 
  pinMode(inMotorStangaFataA, OUTPUT);
  pinMode(inMotorStangaFataB, OUTPUT);
  pinMode(inMotorStangaSpateA, OUTPUT);
  pinMode(inMotorStangaSpateB, OUTPUT);
  pinMode(inMotorDreaptaFataA, OUTPUT);
  pinMode(inMotorDreaptaFataB, OUTPUT);
  pinMode(inMotorDreaptaSpateA, OUTPUT);
  pinMode(inMotorDreaptaSpateB, OUTPUT);
}

void loop() {
   
 while (bluetooth.available()){  //Check if there is an available byte to read
  delay(10); //Delay added to make thing stable
  char c = bluetooth.read(); //Conduct a serial read
  readString = c; //build the string- "forward", "reverse", "left" and "right"
  }
  if (readString.length() > 0) {
    Serial.println(readString);

   unsigned int servopos = bluetooth.read();
    unsigned int servopos1 = bluetooth.read();
    unsigned int realservo = (servopos1 *256) + servopos;
    Serial.println(realservo);
     
  if(readString == "forward")
  {
   
    digitalWrite (inMotorStangaFataA, HIGH);
    digitalWrite (inMotorStangaFataB, LOW);
    digitalWrite (inMotorStangaSpateA, HIGH);
    digitalWrite (inMotorStangaSpateB, LOW);
    digitalWrite (inMotorDreaptaFataA, HIGH);
    digitalWrite (inMotorDreaptaFataB, LOW);
    digitalWrite (inMotorDreaptaSpateA, HIGH);
    digitalWrite (inMotorDreaptaSpateB, LOW);

   delay(100);
  }

  else if(readString == "reverse")
  {
    digitalWrite (inMotorStangaFataA, LOW);
    digitalWrite (inMotorStangaFataB, HIGH);
    digitalWrite (inMotorStangaSpateA, LOW);
    digitalWrite (inMotorStangaSpateB, HIGH);
    digitalWrite (inMotorDreaptaFataA, LOW);
    digitalWrite (inMotorDreaptaFataB, HIGH);
    digitalWrite (inMotorDreaptaSpateA, LOW);
    digitalWrite (inMotorDreaptaSpateB, HIGH);

    delay(100);
  }

  else if (readString == "right")
  {
    digitalWrite (inMotorStangaFataA, HIGH);
    digitalWrite (inMotorStangaFataB, LOW);
    digitalWrite (inMotorStangaSpateA, LOW);
    digitalWrite (inMotorStangaSpateB, HIGH);
    digitalWrite (inMotorDreaptaFataA, LOW);
    digitalWrite (inMotorDreaptaFataB, HIGH);
    digitalWrite (inMotorDreaptaSpateA, HIGH);
    digitalWrite (inMotorDreaptaSpateB, LOW);

    delay (100);
   
  }

 else if ( readString == "left")
 {
    digitalWrite (inMotorStangaFataA, LOW);
    digitalWrite (inMotorStangaFataB, HIGH);
    digitalWrite (inMotorStangaSpateA, HIGH);
    digitalWrite (inMotorStangaSpateB, LOW);
    digitalWrite (inMotorDreaptaFataA, HIGH);
    digitalWrite (inMotorDreaptaFataB, LOW);
    digitalWrite (inMotorDreaptaSpateA, LOW);
    digitalWrite (inMotorDreaptaSpateB, HIGH);

   delay (100);
 }

 else if ( readString == "diag-fata-stanga")
 {
    digitalWrite (inMotorStangaFataA, LOW);
    digitalWrite (inMotorStangaFataB, LOW);
    digitalWrite (inMotorStangaSpateA, HIGH);
    digitalWrite (inMotorStangaSpateB, LOW);
    digitalWrite (inMotorDreaptaFataA, HIGH);
    digitalWrite (inMotorDreaptaFataB, LOW);
    digitalWrite (inMotorDreaptaSpateA, LOW);
    digitalWrite (inMotorDreaptaSpateB, LOW);


   delay (100);
 }

else if ( readString == "diag-fata-dreapta")
 {
    digitalWrite (inMotorStangaFataA, HIGH);
    digitalWrite (inMotorStangaFataB, LOW);
    digitalWrite (inMotorStangaSpateA, LOW);
    digitalWrite (inMotorStangaSpateB, LOW);
    digitalWrite (inMotorDreaptaFataA, LOW);
    digitalWrite (inMotorDreaptaFataB, LOW);
    digitalWrite (inMotorDreaptaSpateA, HIGH);
    digitalWrite (inMotorDreaptaSpateB, LOW);


   delay (100);
 }

 else if ( readString == "diag-spate-dreapta")
 {
    digitalWrite (inMotorStangaFataA, LOW);
    digitalWrite (inMotorStangaFataB, LOW);
    digitalWrite (inMotorStangaSpateA, LOW);
    digitalWrite (inMotorStangaSpateB, HIGH);
    digitalWrite (inMotorDreaptaFataA, LOW);
    digitalWrite (inMotorDreaptaFataB, HIGH);
    digitalWrite (inMotorDreaptaSpateA, LOW);
    digitalWrite (inMotorDreaptaSpateB, LOW);


   delay (100);
 }

 else if ( readString == "diag-spate-stanga")
 {
    digitalWrite (inMotorStangaFataA, LOW);
    digitalWrite (inMotorStangaFataB, HIGH);
    digitalWrite (inMotorStangaSpateA, LOW);
    digitalWrite (inMotorStangaSpateB, LOW);
    digitalWrite (inMotorDreaptaFataA, LOW);
    digitalWrite (inMotorDreaptaFataB, LOW);
    digitalWrite (inMotorDreaptaSpateA, LOW);
    digitalWrite (inMotorDreaptaSpateB, HIGH);


   delay (100);
 }

 else if ( readString == "rotate-right")
 {
    digitalWrite (inMotorStangaFataA, HIGH);
    digitalWrite (inMotorStangaFataB, LOW);
    digitalWrite (inMotorStangaSpateA, HIGH);
    digitalWrite (inMotorStangaSpateB, LOW);
    digitalWrite (inMotorDreaptaFataA, LOW);
    digitalWrite (inMotorDreaptaFataB, HIGH);
    digitalWrite (inMotorDreaptaSpateA, LOW);
    digitalWrite (inMotorDreaptaSpateB, HIGH);

   delay (100);
 }

 else if ( readString == "rotate-left")
 {
    digitalWrite (inMotorStangaFataA, LOW);
    digitalWrite (inMotorStangaFataB, HIGH);
    digitalWrite (inMotorStangaSpateA, LOW);
    digitalWrite (inMotorStangaSpateB, HIGH);
    digitalWrite (inMotorDreaptaFataA, HIGH);
    digitalWrite (inMotorDreaptaFataB, LOW);
    digitalWrite (inMotorDreaptaSpateA, HIGH);
    digitalWrite (inMotorDreaptaSpateB, LOW);


   delay (100);
 }
 
 
 else if (readString == "stop")
 {
    digitalWrite (inMotorStangaFataA, LOW);
    digitalWrite (inMotorStangaFataB, LOW);
    digitalWrite (inMotorStangaSpateA, LOW);
    digitalWrite (inMotorStangaSpateB, LOW);
    digitalWrite (inMotorDreaptaFataA, LOW);
    digitalWrite (inMotorDreaptaFataB, LOW);
    digitalWrite (inMotorDreaptaSpateA, LOW);
    digitalWrite (inMotorDreaptaSpateB, LOW);
   
   delay (100);
 }

if (realservo >= 1000 && realservo <1180) {
      int servo1 = realservo;
      servo1 = map(servo1, 1000, 1180, 0, 180);
   
      myservo1.write(servo1);
      Serial.println("Servo 1 ON");

      delay(100);
    }
    if (realservo >= 2000 && realservo <2180) {
      int servo2 = realservo;
      servo2 = map(servo2, 2000, 2180, 0, 180);
      myservo2.write(servo2);
      Serial.println("Servo 2 ON");
      delay(100);
    }
    if (realservo >= 3000 && realservo <3180) {
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180, 0, 180);
      myservo3.write(servo3);
      Serial.println("Servo 3 ON");
      delay(100);
    }
    if (realservo >= 4000 && realservo <4180) {
      int servo4 = realservo;
      servo4 = map(servo4, 4000, 4180, 0, 180);
      myservo4.write(servo4);
      Serial.println("Servo 4 ON");
      delay(100);
    }
 
readString="";}}


And i will also add 2 screenshots with a short code example from Mit App Inventor .


I hope that somebody will want to help me at this project.
Thank you!

alecks_teo

Again, I hope that someone will have enough patience and will understand that i don't know which title or topic is better for my problem, cause i don't know exactly how to explain better what happens.
 I can explain only with my words, i can explain what i see that happen or not. 
  Like i said, I am so noob right now at coding but o have to finish this homework.

PaulS

Code: [Select]
  delay(10); //Delay added to make thing stable
What do you think that this useless delay() is making more stable?

Code: [Select]
  char c = bluetooth.read(); //Conduct a serial read
  readString = c; //build the string- "forward", "reverse", "left" and "right"

You are NOT building up a string. You are OVERWRITING the previous string with ONE character.

You REALLY need to make your sender send some kind of end-of-message marker, and read and accumulate data until that end of message marker arrives. You are currently hoping that an entire message arrives all at once, which is not going to happen.

You need to spend some time with this thread:
http://forum.arduino.cc/index.php?topic=396450.0
The art of getting good answers lies in asking good questions.

Go Up