Hey I'm an arduino newb and I was wondering why the use of GOTO command is discouraged in C and similar languages. In my opinion it makes the code much more modular and easier to edit specific sections of code. Is there a better way or a reason why the use of goto is discouraged? if there is, is there a way I can enhance this code?
//---------------------------------------int PIN definitions------------------------------------
// THere are 7 LEDs and 3 pushbutton switches, the pushbuttons have harware debouncing circuits so LOW registers when the button is pressed
int SW1 = 11; // push-buttons
int SW2 = 12;
int SW3 = 10;
int timer = 75; // LED timer
int pins[] = { 2, 3, 4, 5, 6, 7, 8 }; // array of the LED pins
int num_pins = 7; // the number of LEDs
long randomNumber; // random number generator
//-----------------------------------------Setup------------------------------------------------
void setup()
{
int i;
pinMode (SW1 , INPUT); // I/O definitions
pinMode (SW2 , INPUT);
pinMode (SW3 , INPUT);
for (i = 0; i < num_pins; i++) // the array elements are numbered from 0 to num_pins - 1
pinMode(pins[i], OUTPUT); // set each pin as an output for LEDs
}
//-----------------------------------------Main Loop------------------------------------------------
void loop()
{
// reads 3 pushbuttons for cases then goes to subroutines
// HIGH = button pressed = 0 , LOW = button not pressed = X
Buttons:
{
if (digitalRead (SW1) == HIGH && digitalRead (SW2) == HIGH && digitalRead (SW3) == HIGH) //-----0----0----0------
goto LED_1;
if (digitalRead (SW1) == LOW && digitalRead (SW2) == HIGH && digitalRead (SW3) == HIGH) //-----X----0----0------
goto LED_2;
if (digitalRead (SW1) == HIGH && digitalRead (SW2) == LOW && digitalRead (SW3) == HIGH) //-----0----X----0------
goto LED_3;
if (digitalRead (SW1) == HIGH && digitalRead (SW2) == HIGH && digitalRead (SW3) == LOW) //-----0----0----X------
goto LED_4;
if (digitalRead (SW1) == LOW && digitalRead (SW2) == LOW && digitalRead (SW3) == HIGH) //-----X----X----0------
goto LED_5;
if (digitalRead (SW1) == LOW && digitalRead (SW2) == HIGH && digitalRead (SW3) == LOW) //-----X----0----X------
goto LED_6;
if (digitalRead (SW1) == HIGH && digitalRead (SW2) == LOW && digitalRead (SW3) == LOW) //-----0----X----X------
goto LED_7;
if (digitalRead (SW1) == LOW && digitalRead (SW2) == LOW && digitalRead (SW3) == LOW) //-----X----X----X------
goto LED_8;
}
//-----------------------------------------Subroutines-------------------------------------------------
// These are the LED "subroutines" using the goto statements to come and go to the main loop.
//-----------------------------------------
LED_1:
{ // Random Lighting
int i;
for (i = 0; i < num_pins; i++)
{ randomNumber = random(0, 2);
if (randomNumber == 0)
digitalWrite (pins[i], LOW);
delay(timer);
if (randomNumber == 1)
digitalWrite (pins[i], HIGH);
delay(timer);
}
for (i = num_pins - 1; i >= 0; i--)
{ randomNumber = random(0, 2);
if (randomNumber == 0)
digitalWrite (pins[i], LOW);
delay(timer);
if (randomNumber == 1)
digitalWrite (pins[i], HIGH);
delay(timer);
}
goto Buttons;
}
//-----------------------------------------
LED_2: // Move to the Left
{
int i;
for (i = 0; i < num_pins; i++)
{
digitalWrite(pins[i] && pins[i+1], LOW);
delay(timer);
digitalWrite(pins[i], HIGH);
}
for (i = num_pins - 1; i >= 0; i--)
{
digitalWrite(pins[i], LOW);
delay(timer);
digitalWrite(pins[i], HIGH);
}
goto Buttons;
}
//-----------------------------------------
LED_3: // Cylon lighting
{
int i;
for (i = 0; i < num_pins; i++)
{
digitalWrite(pins[i], LOW);
delay(timer);
digitalWrite(pins[i], HIGH);
}
for (i = num_pins - 1; i >= 0; i--)
{
digitalWrite(pins[i], LOW);
delay(timer);
digitalWrite(pins[i], HIGH);
}
goto Buttons;
}
//-----------------------------------------
LED_4: // Move to the Right
{
int i;
for (i = 0; i < num_pins; i++)
{
digitalWrite(pins[i] , LOW);
delay(timer);
digitalWrite(pins[i], HIGH);
}
for (i = num_pins - 1; i >= 0; i--)
{
digitalWrite(pins[i] && pins[i+1], LOW);
delay(timer);
digitalWrite(pins[i], HIGH);
}
goto Buttons;
}
//-----------------------------------------
LED_5: // Fill to the Left
{
int i;
for (i = 0; i < num_pins; i++)
{
digitalWrite(pins[i], LOW);
delay(timer);
}
for (i = num_pins - 1; i >= 0; i--)
{
digitalWrite(pins[i], HIGH);
delay(timer);
}
goto Buttons;
}
//-----------------------------------------
LED_6: // Pocketed Fill
{
int i;
for (i = 0; i < num_pins; i++)
{
digitalWrite(pins[i++] , LOW);
delay(timer);
delay(timer/2);
}
for (i = num_pins - 1; i >= 0; i--)
{
digitalWrite(pins[i--] , HIGH);
delay(timer);
delay(timer/2);
}
for (i = num_pins - 1; i >= 0; i--)
{
digitalWrite(pins[i--] , LOW);
delay(timer);
delay(timer/2);
}
for (i = 0; i < num_pins; i++)
{
digitalWrite(pins[i++] , HIGH);
delay(timer);
delay(timer/2);
}
goto Buttons;
}
//-----------------------------------------
LED_7:
{ // Fill to the Right
int i;
for (i = 0; i < num_pins; i++)
{
digitalWrite(pins[i], HIGH);
delay(timer);
}
for (i = num_pins - 1; i >= 0; i--)
{
digitalWrite(pins[i], LOW);
delay(timer);
}
goto Buttons;
}
//-----------------------------------------
LED_8: // Bouncing Bar
{
int i;
for (i = 0; i < num_pins; i++)
{
digitalWrite(pins[i], LOW);
delay(timer);
}
for (i = 0; i < num_pins; i++)
{
digitalWrite(pins[i], HIGH);
delay(timer);
}
for (i = num_pins - 1; i >= 0; i--)
{
digitalWrite(pins[i], LOW);
delay(timer);
}
for (i = num_pins - 1; i >= 0; i--)
{
digitalWrite(pins[i], HIGH);
delay(timer);
}
goto Buttons;
}
//-----------------------------------------
}