0

I have learned Arduino for a month, and I want to bulid a car with 2 modes, Remote and Auto. Auto mode needs to have while() loop, so it can receive data from sensors. When I choose Auto mode via bluetooth, it keeps looping and it won't stop when I choose the Remote mode. Here's the part code:

char mode;

void loop()
{
    while(serial1.available())
    {
        char control = serial.read();
        if(control == 'r') mode = 'r';     // r stand for Remote Mode
        if(control == 'a') mode = 'a';     // a stand for Auto Mode

        if(mode == 'r') remoteMode(control);

        while(mode = 'a')
        {
            int IR = analogRead(A10);
            float U1cmMsec,U2cmMsec;
            long U1microsec = u1.timing();
            long U2microsec = u2.timing();
            U1cmMsec = u1.convert(U1microsec, Ultrasonic::CM);
            U2cmMsec = u2.convert(U2microsec, Ultrasonic::CM);

            robot(U1cmMsec, U2cmMsec,IR);

            /*
            Here is the code to stop while() that I don't know how
            to program.
            I have tried:

            if(serial1.read() != 'a') break; 

            But it will stop while(), because it will waits for
            bluetooth send data to arduino board. 
            And if I take this line off, it will keeps while() and
            nothing can stop it.
            So, what am I supposed to do?
            */
        }
    }
}
1

Your problem is with this line:

while(mode = 'a') {                 

What you are saying there is not what you think you are saying. You are confusing = (assignment) with == (comparison).

You are basically saying "While I assign 'a' to the variable 'mode' and 'a' is not 0 ...", which of course will always be true.

Instead you want:

while (mode == 'a') {

or, "While the variable 'mode' contains the letter 'a' ..."

Now you just need to program in some way of changing the value of mode within your while loop.

You can read more on while loops here:

0

The while(serial1.available()) can be turned into an if: the loop function is already executed in a loop, as the name hints :-)

And unnest the inner while.

here it is at high level, which is probably easier to understand:

void loop() { static char mode; /*notice the "static"*/ if (character_available()) update_mode(); if (mode == 'r') do_one_iteration_in_remote_mode(); else if (mode == 'a') do_one_iteration_in_auto_mode(); }

Just make sure that you do not loop inside neither do_one_iteration_in_remote_mode() nor do_one_iteration_in_auto_mode().

0

I have replaced both 'mode' and 'control' (they were identical) with a single variable, btChar. At the end of setup(), wait for a transmission to be received before entering the main loop.

Once the main loop begins, a while loop executes until the serial receives a new transmission. Once it is received it is read, btChar is updated and the main loop starts again.

char btChar;

void setup()
{
/* AT THE END OF SETUP() */

// Wait in setup for first BT transmission

    while (!serial1.available())
    { /* do nothing */ }
    btChar = serial1.read();
}

void loop()
{
    while(!serial1.available())
    {
    // Program will stay in this 'while' loop until
    //    a new BT character is recv'd

        switch(btChar)
        {
            case('a'):
            {
            // do the 'auto' stuff
                break;
            }
            case('r'):
            {
            // do the 'remote' stuff
                break;
            }
            default: // this 'block' is not essential
            {
            // you end up here if neither 'a' nor 'r' was received
            }
        }
    }
    btChar = serial1.read();
}

You can adapt the setup while-loop further to make sure that the main loop doesn't begin until the a correct character is received - change the line to:

while ((!serial1.available())&&(btChar != 'a')&&(btChar != 'r'))

Using a switch-case statement within the main while-loop is not essential, and I included it out of interest more than necessity. You can use if statements instead, with one for 'a' and one for 'r'. However, in the event that you have a third or fourth (or more-th) mode then a switch-case statement, in my view, looks easier to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy