Go Down

Topic: Help if/else (Read 403 times) previous topic - next topic

afonsus

I dont understand why this is not working!

it says else withou previous if

Code: [Select]
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;
int led5 = 9;
int switchPin = 7;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(switchPin, INPUT);
}


void loop()
{
  if (digitalRead(switchPin) == HIGH);
   {
    goto knightRider;
   }
  else
  {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, HIGH);
    digitalWrite(led5, HIGH);
  }
}



knightRaider:
{
  digitalWrite(led1, HIGH);
  delay(100);
  digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH);
  delay(100);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  delay(100);
  digitalWrite(led3, LOW);
  digitalWrite(led4, HIGH);
  delay(100);
  digitalWrite(led4, LOW);
  digitalWrite(led5, HIGH);
  delay(100);
  digitalWrite(led5, LOW);
  digitalWrite(led4, HIGH);
  delay(100);
  digitalWrite(led4, LOW);
  digitalWrite(led3, HIGH);
  delay(100);
  digitalWrite(led3, LOW);
  digitalWrite(led2, HIGH);
  delay(100);
  digitalWrite(led2, LOW);
  digitalWrite(led1, HIGH);
  digitalWrite(led1, LOW);
 
}

AWOL

#1
Jan 05, 2013, 03:49 pm Last Edit: Jan 05, 2013, 03:52 pm by AWOL Reason: 1
Code: [Select]
if (digitalRead(switchPin) == HIGH);
Lose the semicolon.
Then lose the goto.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

tobyb121

the "goto" instruction is generally frowned upon in high level languages (anything above assembly code really) as it causes alot of problems, particularly for beginners, because of code readability, and sometimes causes unexpected errors. For this kind of code you should use a function call:
Code: [Select]

void loop()
{
  if (digitalRead(switchPin) == HIGH)
   {
       knightRider();
   }
  else
  {
    //do something else
  }
}

void knightRaider()
{
    //Stuff here
}

Go Up