4WD arduino romeo programming (how to do several action in the same time)

DSC08765.JPG (1390343Bytes)

Good morning!

i have make a robot with :

-platform arduino

-ROMEO 328mo

-three sensor infrared

-apc 220 wireless

-1 battery nimh 12V for motors

-1 battery nimh 9V for ROMEO

i made a sketch for controled the robot with my keyboard and processing "it is good!!

i made a sketch for have a autonomous robot "it is good!!

but when i put the sketch "controled" with the "autonomous" in the same sketch there is problem!

when i pressed 'j' on my keyboard for the autonmous sketch it not work, i must hold the key 'j' pressed for start working ok

and for mode default "controled" when i pushed on a key for controled my robot it not work immediately "not in real time"

i don't understand if you have a idea i will very happy, sorry for my english i'm french.

i shawn you my sketch:

     int inputPinG = 8;
     int inputPinC = 9;
     int inputPinD = 10;
    
     int valG = 0;   
     int valC = 0;
     int valD = 0;
         
     int E1 = 5; //M1 vitesse
     int E2 = 6; //M2 vitesse
     int M1 = 4; //M1 Direction
     int M2 = 7; //M1 Direction
    
     void setup(void)
     {
     int i;
     for(i=4;i<=7;i++)
     pinMode(i, OUTPUT);
    
     pinMode(inputPinG, INPUT);  // declare Infrared sensor as input
     pinMode(inputPinC, INPUT);  // declare Infrared sensor as input
     pinMode(inputPinD, INPUT);  // declare Infrared sensor as input
    
     Serial.begin(19200); //Set Baud Rate
     }
    
   void loop()
    {
   valD = digitalRead(8);
   valC = digitalRead(9);
   valG = digitalRead(10);
  
     char choose = Serial.read();
  
     switch(choose){
     case 'j':
     automatique();//mode autonomous!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     break;
                  
    default:
    manuel();//mode manual or controled!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   }
   }
            
void automatique(void) //autonomous!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   {  
  if((valD==LOW&&!valG==LOW&&!valC==LOW)||(valD==LOW&&!valG==LOW&&valC==LOW))
  turn_L (100,100);

  else if ((valC==LOW&&!valG==LOW&&!valD==LOW)||(valC==LOW&&valG==LOW&&valD==LOW)||(!valC==LOW&&valG==LOW&&valD==LOW))
  back_R (100,60);
 
  else if ((valG==LOW&&!valC==LOW&&!valD==LOW)||(valC==LOW&&valG==LOW&&!valD==LOW))
  turn_R (100,100);

  else 
  advance (80,80);
     }

 void manuel(void) //Controled!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     {
     char val = Serial.read();
     switch (val) 
     {
     case 'z'://advance
     advance (50,50);
     break;
     case 'w'://back off
     back_off (50,50);
     break;
     case 'q'://virage left
     advance (70,50);
     break;
     case 's'://virage right
     advance (50,70);
     break;
     case 'a'://turn left
     turn_L (50,50);
     break;
     case 'x'://turn right
     turn_R (50,50);
     break;
     case 'c':// stop
     advance (0,0);
     break;
     }
     }
    
   void stop(void) //Stop
     {
     digitalWrite(E1,LOW);
     digitalWrite(E2,LOW);
     }
     void advance(char a,char B) //advance
     {
     analogWrite (E1,a);
     digitalWrite(M1,LOW);
     analogWrite (E2, B) ;
     digitalWrite(M2,LOW);
     }
     void back_R (char a,char B)//back off right
     {
     analogWrite (E1,a);
     digitalWrite(M1,HIGH);
     analogWrite (E2, B) ;
     digitalWrite(M2,HIGH);
     }
     void back_off (char a,char B) //back off
     {
     analogWrite (E1,a);
     digitalWrite(M1,HIGH);
     analogWrite (E2, B) ;
     digitalWrite(M2,HIGH);
     }
     void turn_L (char a,char B) //turn left
     {
     analogWrite (E1,a);
     digitalWrite(M1,HIGH);
     analogWrite (E2, B) ;
     digitalWrite(M2,LOW);
     }
     void turn_R (char a,char B) //turn right
     {
     analogWrite (E1,a);
     digitalWrite(M1,LOW);
     analogWrite (E2, B) ;
     digitalWrite(M2,HIGH);
     }

a other example :

if i want to controlled my robot and turn on or off a led ,when i pushed on a key for controled my robot it work but not immediately "not in real time" and it's very difficult to drive the robot

void loop()
  {
  while (Serial.available()){
  char choose = Serial.read();

if(choose=='k')
{
digitalWrite(ledPin, HIGH); //
}
if(choose=='m')
{
digitalWrite(ledPin, LOW); //
}
}
manuel();//mode controled!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

}

not say me that the romeo arduino can't run many actions in the same time else i cry!!!!

thank you!!!!

How is the keyboard

How is the keyboard connected to the Arduino? Perhaps the delay is on that side of things.

thank you djhesit8 for your

thank you djhesit8 for your answer now i 'm crying!!!

it’s very shame when you see all the input and output on the romeo and you can’t do two actions in the same time.

telefox the keyboard is connected to the arduino with a apc 220 module wireless.maybe there is a problem with the delay but i don’t know how to solve it.

information.png

Member since: February 2010

information.png

User Number: 11782

script_edit.png

Blogs: 0

flag-bookmarks-on.png

djhesit8’s Collection

email_go.png

Contact
Collect djhesit8 as Friend 

information.png

Location: Oradea, Bihor, Romania

thank you djhesit8 for your

thank you djhesit8 for your answer now i 'm crying!!!

it’s very shame when you see all the input and output on the romeo and you can’t do two actions in the same time.

telefox the keyboard is connected to the arduino with a apc 220 module wireless.maybe there is a problem with the delay but i don’t know how to solve it.

19,200 baud is pretty slow

19,200 baud is pretty slow for a BT wireless connection, give 115,200 a try.
I also notice you’re reading the serial value twice (in ‘loop()’ and ‘manuel(void)’), why don’t you just pass the first read value (‘choose’) onto the manuel() function and save some processing time?
Thirdly, your program is running the manuel() function every time loop() is executed, which doesn’t accomplish anything if no key has been pressed on the keyboard. If you wrap your case statements inside of the following:

if(choose!=-1) {
//case statement stuff goes here
}

then you won’t execute any of the case stuff if the serial read doesn’t return a usable value.

In general, devices with 1 CPU core can only perform 1 task at a given time. To get around this without using multiple processors we use faster core speeds, run multiple tasks by doing a little bit at a time while switching between the active tasks, and try to trim the code down as much as possible. In your case, you aren’t actually trying to do 2 things at once, you only want manual or autonomous control, right?

thank you telefox for your

thank you telefox for your help!

i try at 115200 BAUD but nothing and the software of the apc220 wireless can’t let me choice 115200, the max is 19200.

 

if i make one serial read in the sketch like that:

void loop()
  {
   valD = digitalRead(8);
   valC = digitalRead(9);
   valG = digitalRead(10);

while (Serial.available()) {
  char val = Serial.read();
  switch (val) 
  {
  case ‘j’:// autonomous!!!
  automatique();
  break; 
  case ‘z’://avancer
  advance (100,100); //réglage vitesse
  break;
  case ‘w’://reculer
  back_off (100,100);
  break;
  case ‘q’://virage gauche
  advance (150,50); //réglage vitesse
  break;
  case ‘s’://virage droite
  advance (50,150); //réglage vitesse
  break;
  case ‘a’://tourner sur place à gauche
  turn_L (100,100);
  break;
  case ‘x’://tourner sur place à droite
  turn_R (100,100);
  break;
  case ‘c’:// stop
  stop();
  break;
  }
  }
}

i have the same problem i must to hold the key ‘j’ pressed all time for run the mode autonomous!

tomorrow when my battery will charged, i will try your sketch:

if(choose!=-1) {
   manuel();}
    }

thank you!!!

You say having to hold the j

You say having to hold the j key for autonomous mode is a problem, but how do you want the program to work instead?

With processing i would like

With processing i would like press ‘j’ once to run autonomous  permanently and press ‘c’ to stop it.

 

i can control on off autonomous with a digtal button, why it’s not possible with the keyboard, i don’t understand!

void loop()
  {
   valD = digitalRead(8);
   valC = digitalRead(9);
   valG = digitalRead(10);
 
   if(digitalRead(key_s6)==0)
   {
   while(!digitalRead(key_s6));
   val++;
   }
  if(val==1)
   {
   automatique();//autonomous!!!
   }

  else if(val==2)
   {
   val=0;
   stop();
   }

  manuel();//controled!!!
 }

The problem with the above

The problem with the above code is that whenever you press the button, you’re technically holding it active for a short length of time. If your loop() function executes more than once in the time the button is still being pressed, then ‘val’ will jump back and forth between 1 and 0 rapidly.
You need to implement some sort of delay or debouncing routine to make sure the mode is not changed too frequently, or can only be changed if you release the button and then press it again.

thank you everybody!!now i

thank you everybody!!

now i can on off a led, on off a camera and controled my robot in same time.

the problem like telefox said me is to not put two serial read

i must use only one serial read and now i’m happy!!!

and for the mode autonomous i hold the key ‘j’ pressed to run what a pitty!!

 

i would like to ask you a question but i don’t know if i must created a new post or not : i have a module wireless apc220 and i can controled my robot with it but impossible for me to download a sketch “pc to arduino” a lot of people have this problem but not solution!!! if you know how to do to transfered a sketch of pc to arduino it wil great!!!.

Thank you!!!

autonomous mode

Hi there,

Just checking on the status of this: "and for the mode autonomous i hold the key ‘j’ pressed to run what a pitty!!"

Is that still the case? If you’re still having problems, this can easily be fixed. The problem is that you’re reading in the state with every loop. E.g., everytime loop() runs, you’re doing:

while (Serial.available()) {
  char val = Serial.read();
  switch (val) 
  {
  case ‘j’:// autonomous!!!
  automatique();
  break; 

That means that for your robot to run the automatique() function you must be pushing the ‘j’ key. Each time you read from the Serial buffer you’re clearing the value. So if you push ‘j’ once, it goes into the buffer once, your loop reads it and the next time you loop it forgets that the ‘j’ was ever pressed. The solution is simply to add a variable to keep the state. E.g. a simple boolean would do:

(put this at the top of your code above the setup() function)

boolean automatique = false;

(use this in your loop() function)

void loop() {
  valD = digitalRead(8);
  valC = digitalRead(9);
  valG = digitalRead(10);

  while (Serial.available()) {
    char val = Serial.read();
    switch (val) {
      case ‘j’:// autonomous!!!
        automatique = true;
        break;
      case ‘z’://avancer
        automatique = false;
        advance (100,100); //réglage vitesse
        break;
      case ‘w’://reculer
        automatique = false;
        back_off (100,100);
        break;
      case ‘q’://virage gauche
        automatique = false;      
        advance (150,50); //réglage vitesse
        break;
      case ‘s’://virage droite
        automatique = false;      
        advance (50,150); //réglage vitesse
        break;
      case ‘a’://tourner sur place à gauche
        automatique = false;      
        turn_L (100,100);
        break;
      case ‘x’://tourner sur place à droite
        automatique = false;      
        turn_R (100,100);
        break;
      case ‘c’:// stop
        automatique = false;      
        stop();
        break;
    }
  }
  if (automatique) automatique();
}

That way, if you push ‘j’ the automatique boolean will be set to true. If you don’t push any other buttons, then it will stay true and your robot will keep operating in automatic mode, when you push any other key you’ll perform a manual override and switch automatic off.

I hope this helps, feel free to ask questions if I haven’t been clear enough. Basically this is just a matter of keeping state between loops.

Cheers,

Phil

PHIL YOU ARE THE

PHIL YOU ARE THE BESSSSSSSSSST!!!

your help is great, my sketch running and now i can controled, camera, autonomous etc… with my pc keyboard.

Very very good thank’s for you!

 

De rien :wink:

No worries, glad I could help

Hey again, I’ve been out of

Hey again, I’ve been out of the country for the past week and a half, great to hear your robot is working so much better now!

You should definitely start a new post regarding your issue with downloading a sketch via the APC220, so that others with the same problem can also go there for help.