Help needed with debugging (Beginner)

Hi, i'm a complet beginner and i got myself the adruino starter kit.

I have a question about the code. I get a error message with the if, else statement.

attached is my code:

int switchState = 0;
void setup() {
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(2, INPUT);
}

void loop() {
  switchState = digitalRead(2);
}

if (switchState == LOW) {

  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
}
else {
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);

  delay(250);

  digitalWrite(4, High);
  digitalWrite(5, LOW);
  deley(250);
}

Can someone tell me what's wrong with it?

Thans in advance!

Your if statement is not inside any function.

That closing bracket completes the function loop() so the code after that isn't associated with a function but it needs to be. Move your closing bracket to the end of the file

void loop() {
  switchState = digitalRead(2);


  if (switchState == LOW) {

    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
  }
  else {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);

    delay(250);

    digitalWrite(4, High);
    digitalWrite(5, LOW);
    deley(250);
  }
}

Worked! Thanks soo much for your fast respons.

Hi @hackbart77

welcome to the arduino-Forum.
Well done posting your code as a code-section in your very first posting.

So if your if - else is INSIDE the function loop() it will work.
You should use meaningful names instead of numbers.
You did not describe what your IO-pins 3,4,5 are
So I named them at least IO_PinA, IO_PinB, IO_PinC

int switchState = 0;

const byte IO_PinA = 3;
const byte IO_PinB = 4;
const byte IO_PinC = 5;

const byte mySwitchPin = 2;

void setup() {
  pinMode(IO_PinA, OUTPUT);
  pinMode(IO_PinB, OUTPUT);
  pinMode(IO_PinC, OUTPUT);
  pinMode(mySwitchPin, INPUT);
}

void loop() {
  switchState = digitalRead(mySwitchPin);

  if (switchState == LOW) {

    digitalWrite(IO_PinA, HIGH);
    digitalWrite(IO_PinB, LOW);
    digitalWrite(IO_PinC, LOW);
  }
  else {
    digitalWrite(IO_PinA, LOW);
    digitalWrite(IO_PinB, LOW);
    digitalWrite(IO_PinC, HIGH);

    delay(250); // freeze_microcontroller_completely_stop_all_codeexecution_until_freezingtime_is_over

    digitalWrite(IO_PinB, High);
    digitalWrite(IO_PinC, LOW);
    delay(250); // freeze_microcontroller_completely_stop_all_codeexecution_until_freezingtime_is_over
  }
} // END of function loop()

You are using function delay().
In two days function delay() will cause problems to proceed any further iif you want to do two things in parallel.

You should learn non-blocking timing right from the start.
Unofrtunately the standard example about non-blocking timing is a real bad one.

This standard example misses to explain the fundamental difference between delay() and non-blocking timing.

The function delay() should have the name
freeze_microcontroller_completely_stop_all_codeexecution_until_freezingtime_is_over()

non-blocking timing makes the code loop very fast all the time and execute code that shall be executed all the time UNconcitional

And the actions that shall be executed only after some time are executed CONDITIONAL with a timing-condition.

Your code does a sequence of steps

step 1 switch on IO-pin 5
step 2 wait for 250 milliseconds
step 3 switch on IO-pin 4
step 4 wait for 250 milliseconds

repeat
if button is pressed then switch on io-pin 3
wait 250 milliseconds

Here is a code that does a sequence to but in a non-blocking manner
step 0 switch all off
if switch is LOW
step 1 switch on IOpin 5
step 2 wait 250 milliseconds
step 3 switch on IO-pin 4
step 4 wait 250 milliseconds
step 5 switch on io-pin 3
step 6 wait 250 milliseconds

repeat

int switchState = 0;

const byte IO_PinA = 3;
const byte IO_PinB = 4;
const byte IO_PinC = 5;

const byte mySwitchPin = 2;

const byte sm_allOff       = 0;
const byte sm_switchOnA    = 1;
const byte sm_waitWithA_On = 2;
const byte sm_switchOnC    = 3;
const byte sm_waitWithC_On = 4;
const byte sm_switchOnB    = 5;
const byte sm_waitWithB_On = 6;

byte mysequenceStep = sm_allOff;

unsigned long myTimer;


void setup() {
  pinMode(IO_PinA, OUTPUT);
  pinMode(IO_PinB, OUTPUT);
  pinMode(IO_PinC, OUTPUT);
  pinMode(mySwitchPin, INPUT);
}


void loop() {
  switchState = digitalRead(mySwitchPin);
  myStepSequence();
} // END of function loop()



void myStepSequence() {
  switch (mysequenceStep) {

    case sm_allOff:
      if (switchState == LOW) {
        mysequenceStep = sm_switchOnA;
      }
      break; // IMMIDIATELY jump down to END-OF-SWITCH


    case sm_switchOnA:
      digitalWrite(IO_PinA, HIGH);
      digitalWrite(IO_PinB, LOW);
      digitalWrite(IO_PinC, LOW);
      myTimer = millis(); // store snapshot of time
      mysequenceStep = sm_waitWithA_On;
      break; // IMMIDIATELY jump down to END-OF-SWITCH


    case sm_waitWithA_On:
      // check if waiting-time has gone by
      if ( TimePeriodIsOver(myTimer, 250) ) {
        mysequenceStep = sm_switchOnC;
      }
      break; // IMMIDIATELY jump down to END-OF-SWITCH


    case sm_switchOnC:
      digitalWrite(IO_PinA, LOW);
      digitalWrite(IO_PinB, LOW);
      digitalWrite(IO_PinC, HIGH);
      myTimer = millis(); // store snapshot of time
      mysequenceStep = sm_waitWithC_On;
      break; // IMMIDIATELY jump down to END-OF-SWITCH


    case sm_waitWithC_On:
      if ( TimePeriodIsOver(myTimer, 250) ) {
        mysequenceStep = sm_switchOnB;
      }
      break; // IMMIDIATELY jump down to END-OF-SWITCH


    case sm_switchOnB:
      digitalWrite(IO_PinB, HIGH);
      digitalWrite(IO_PinC, LOW);
      myTimer = millis(); // store snapshot of time
      mysequenceStep = sm_waitWithB_On;
      break; // IMMIDIATELY jump down to END-OF-SWITCH


    case sm_waitWithB_On:
      if ( TimePeriodIsOver(myTimer, 250) ) {
        digitalWrite(IO_PinA, LOW);
        digitalWrite(IO_PinB, LOW);
        digitalWrite(IO_PinC, LOW);
        mysequenceStep = sm_allOff;
      }
      break; // IMMIDIATELY jump down to END-OF-SWITCH

  } // END-OF-SWITCH  
}

// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

best regards Stefan

Dear @StefanL38

Thanks for the super detailed solution and the input with the non-blocking timing.
I really appreciate your time to exlpain it since im planing the skill myself to do more with a Arduino then just operate some LED :stuck_out_tongue:

The code works, but i definetly look into the non-blocking timing more in detail.

Im looking very much forward to the future for more complex protects and all the good conversations her in the forum.

Happy Coding!

[ open bracket
] close bracket
{ open brace
} close brace
1 Like

deley(250); // typo

bracket2
bracket1

(this article is inaccurate... I read waka-waka-bang-splat in the '80s)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.