-1
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

#define M11  2
#define M12  3
#define M13  4
#define M14  5

#define M21  6
#define M22  7
#define M23  8
#define M24  9




uint32_t commands[32];
int confirmationBuzzer = 17;
int i = 0;
boolean DirectionR = true;
boolean DirectionL = true;
int StepsR;
int StepsL;
int steps_left = 4095;
int caseLeft;

RF24 radio(10, 14); // CE, CSN
SoftwareSerial mySoftwareSerial(15, 16); // RX, TX

DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

const byte address[6] = "00001";

void stepperR(int xw) {
    for (int x = 0;x < xw;x++) {
        switch (StepsR) {
        case 0:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, HIGH);
            break;
        case 1:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, HIGH);
            digitalWrite(M14, HIGH);
            break;
        case 2:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, HIGH);
            digitalWrite(M14, LOW);
            break;
        case 3:
            digitalWrite(M11, LOW);
            digitalWrite(M12, HIGH);
            digitalWrite(M13, HIGH);
            digitalWrite(M14, LOW);
            break;
        case 4:
            digitalWrite(M11, LOW);
            digitalWrite(M12, HIGH);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        case 5:
            digitalWrite(M11, HIGH);
            digitalWrite(M12, HIGH);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        case 6:
            digitalWrite(M11, HIGH);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        case 7:
            digitalWrite(M11, HIGH);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, HIGH);
            break;
        default:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        }
        SetDirectionR();
    }
}

void SetDirectionR() {
    if (DirectionR == 1) { StepsR++; }
    if (DirectionR == 0) { StepsR--; }
    if (StepsR>7) { StepsR = 0; }
    if (StepsR<0) { StepsR = 7; }
}


void stepperL(int xw) {
    for (int x = 0;x<xw;x++) {
        switch (StepsL) {
        case 0:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, HIGH);
            break;
        case 1:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, HIGH);
            digitalWrite(M14, HIGH);
            break;
        case 2:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, HIGH);
            digitalWrite(M14, LOW);
            break;
        case 3:
            digitalWrite(M11, LOW);
            digitalWrite(M12, HIGH);
            digitalWrite(M13, HIGH);
            digitalWrite(M14, LOW);
            break;
        case 4:
            digitalWrite(M11, LOW);
            digitalWrite(M12, HIGH);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        case 5:
            digitalWrite(M11, HIGH);
            digitalWrite(M12, HIGH);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        case 6:
            digitalWrite(M11, HIGH);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        case 7:
            digitalWrite(M11, HIGH);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, HIGH);
            break;
        default:
            digitalWrite(M11, LOW);
            digitalWrite(M12, LOW);
            digitalWrite(M13, LOW);
            digitalWrite(M14, LOW);
            break;
        }
        SetDirectionL();
    }
}

void SetDirectionL() {
    if (DirectionL == 1) { StepsL++; }
    if (DirectionL == 0) { StepsL--; }
    if (StepsL>7) { StepsL = 0; }
    if (StepsL<0) { StepsL = 7; }
}

void setup() {

    myDFPlayer.volume(7);

    radio.begin();
    radio.openReadingPipe(0, address);
    radio.setPALevel(RF24_PA_MIN);
    radio.startListening();
}
void loop() {

    if (radio.available()) {
        radio.read(&commands, sizeof(commands));
    }
    for (i = 0; i<15; i++) {
        switch (i) {

        case(0):

            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }

            break;

        case(1): {
        case1:
            int caseLeft = 1;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }

            break;
        }
        case(2): {
        case2:
            int caseLeft = 2;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(3): {
        case3:
            int caseLeft = 3;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(4): {
        case4:
            int caseLeft = 4;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(5): {
        case5:
            int caseLeft = 5;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(6): {
        case6:
            int caseLeft = 6;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(7): {
        case7:
            int caseLeft = 7;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(8): {
        case8:
            int caseLeft = 8;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(9): {
        case9:
            int caseLeft = 9;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(10): {
        case10:
            int caseLeft = 10;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(11): {
        case11:
            int caseLeft = 11;
            switch (commands[i]) {
            case(0):
                goto outoftheloop;
                break;

            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(4):

                goto function1;

                break;
            }
            break;
        }
        case(12): {
        function1:
            switch (commands[i]) {
            case(0):
                switch (caseLeft) {

                case(0):
                    goto case1;
                    break;

                case(1):
                    goto case2;
                    break;

                case(2):
                    goto case3;
                    break;

                case(3):
                    goto case4;
                    break;
                case(4):
                    goto case5;
                    break;

                case(5):
                    goto case6;
                    break;

                case(6):
                    goto case1;
                    break;

                case(7):
                    goto case2;
                    break;

                case(8):
                    goto case1;
                    break;

                case(9):
                    goto case2;
                    break;
                case(10):
                    goto case1;
                    break;

                case(11):
                    goto case2;
                    break;
                }
                break;
            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                goto function2;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                goto function2;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                goto function2;
                break;


            case(4):
                goto function1;

                break;

            }
            break;
        }
        case(13): {
        function2:
            switch (commands[i]) {
            case(0):
                switch (caseLeft) {

                case(0):
                    goto case1;
                    break;

                case(1):
                    goto case2;
                    break;

                case(2):
                    goto case3;
                    break;

                case(3):
                    goto case4;
                    break;
                case(4):
                    goto case5;
                    break;

                case(5):
                    goto case6;
                    break;

                case(6):
                    goto case1;
                    break;

                case(7):
                    goto case2;
                    break;

                case(8):
                    goto case1;
                    break;

                case(9):
                    goto case2;
                    break;
                case(10):
                    goto case1;
                    break;

                case(11):
                    goto case2;
                    break;
                }
                break;
            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                goto function3;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                goto function3;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                goto function3;
                break;


            case(4):
                goto function1;

                break;

            }
            break;
        }
        case(14): {
        function3:
            switch (commands[i]) {
            case(0):
                switch (caseLeft) {

                case(0):
                    goto case1;
                    break;

                case(1):
                    goto case2;
                    break;

                case(2):
                    goto case3;
                    break;

                case(3):
                    goto case4;
                    break;
                case(4):
                    goto case5;
                    break;

                case(5):
                    goto case6;
                    break;

                case(6):
                    goto case1;
                    break;

                case(7):
                    goto case2;
                    break;

                case(8):
                    goto case1;
                    break;

                case(9):
                    goto case2;
                    break;
                case(10):
                    goto case1;
                    break;

                case(11):
                    goto case2;
                    break;
                }
                break;
            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                goto function4;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                goto function4;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                goto function4;
                break;


            case(4):
                goto function1;

                break;

            }
            break;
        }
        case(15): {
        function4:
            switch (commands[i]) {
            case(0):
                switch (caseLeft) {

                case(0):
                    goto case1;
                    break;

                case(1):
                    goto case2;
                    break;

                case(2):
                    goto case3;
                    break;

                case(3):
                    goto case4;
                    break;
                case(4):
                    goto case5;
                    break;

                case(5):
                    goto case6;
                    break;

                case(6):
                    goto case1;
                    break;

                case(7):
                    goto case2;
                    break;

                case(8):
                    goto case1;
                    break;

                case(9):
                    goto case2;
                    break;
                case(10):
                    goto case1;
                    break;

                case(11):
                    goto case2;
                    break;
                }
                break;
            case(1):

                DirectionR = true;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(2);
                    stepperL(2);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(2):
                DirectionR = true;
                DirectionL = false;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;

            case(3):
                DirectionR = false;
                DirectionL = true;

                while (steps_left>0) {

                    stepperR(1);
                    stepperL(1);
                    steps_left--;
                }
                steps_left = 4095;
                break;


            case(4):
                goto function1;

                break;

            }
            break;
        }
        }
    }
outoftheloop:
}

Sorry for the long code,I would send only a part of the code that shows error however I am not sure if the other parts are related to the code. I get this error.

Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\TEDNOKENT2\Desktop\sketch_dec22a\sketch_dec22a.ino: In function 'void setup()':

C:\Users\TEDNOKENT2\Desktop\sketch_dec22a\sketch_dec22a.ino:183:35: warning: invalid conversion from 'const byte* {aka const unsigned char*}' to 'uint64_t {aka long long unsigned int}' [-fpermissive]

radio.openReadingPipe(0, address);

In file included from >C:\Users\TEDNOKENT2\Desktop\sketch_dec22a\sketch_dec22a.ino:3:0:

C:\Users\TEDNOKENT2\Documents\Arduino\libraries\RF24-master/RF24.h:350:8: note: >initializing argument 2 of 'void RF24::openReadingPipe(uint8_t, uint64_t)'

void openReadingPipe(uint8_t number, uint64_t address);

C:\Users\TEDNOKENT2\Desktop\sketch_dec22a\sketch_dec22a.ino: In function 'void loop()':

sketch_dec22a:1288: error: expected primary-expression before '}' token

}

exit status 1 expected primary-expression before '}' token

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

Normally, it should have been fixed adter I deleted some extra brackets but it just stayed there. I have no idea why. I know I shouldn't use gotos that often, however, I am kind of a novice programmer and goto s make it a lot easier to code. I would appreaciate any suggestions on the error. If anthing else that is not related to the error but you think is not necessary, you can mention them too.

Thanks in advance

  • 2
    That is terrible code! There is no call ever to use goto. Full stop. And certainly never in the way you have used it. That code is completely impossible to follow the flow of. – Majenko Dec 22 '17 at 10:39
  • 1
    You should not only use goto's that often, you should not use them ever. – Michel Keijzers Dec 22 '17 at 10:39
  • goto s make it a lot easier to code - but not as easy to read, eh? Or debug? – Nick Gammon Dec 22 '17 at 21:09
1

Actually, although I'm not going to check for every bracket, here are some tips to prevent the problem (and if you follow them you will find your missing/extra bracket):

  • Align brackets, I know it saves one line if you put the { directly after statements, but much neater to put them separately on the next line.
  • Make short functions. If you get a compiler error in a function, it's much easier to find the location. It also helps clearity, maintainability, testability etc. To do this:
  • Do not use more than 2 or 3 compound statements (while, switch, if etc).
  • Put (almost) duplicate code in separate functions and call them (either without parameters or with parameters if needed).
  • Never use goto statements. You can always replace them by if, while, for or do statements.
0

you can escape the loop with return. no need for your goto outoftheloop.

and the goto label syntax is

goto label;
...
label: statement;

so you should write outoftheloop:;

0

My two cents here.

When you have lots of repetitive structures, like in function stepperR, is time to simplify, put some things as data and reduce the repetitiveness of your code.

Here is stepperR reimplemented

byte values[9] = {1, 3, 2, 6, 4, 12, 8, 9, 0};
void stepperR(int xw) {
    for (int x = 0;x < xw;x++) {
            digitalWrite(M11, values[StepsR] & 0x08);
            digitalWrite(M12, values[StepsR] & 0x04);
            digitalWrite(M13, values[StepsR] & 0x02);
            digitalWrite(M14, values[StepsR] & 0x01);      
            SetDirectionR();
    }    
}

I choose StepsR == 8 as the default value (all zeros).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.