Pages: [1]   Go Down
Author Topic: Help if/else  (Read 378 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont understand why this is not working!

it says else withou previous if

Code:
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);
 
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26469
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (digitalRead(switchPin) == HIGH);
Lose the semicolon.
Then lose the goto.
« Last Edit: January 05, 2013, 09:52:24 am by AWOL » Logged

"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.

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void loop()
{
  if (digitalRead(switchPin) == HIGH)
   {
       knightRider();
   }
  else
  {
    //do something else
  }
}

void knightRaider()
{
    //Stuff here
}
Logged

Pages: [1]   Go Up
Jump to: