Need Help w/ End of My Code

Thread Starter

Alln3w2m3

Joined Jun 20, 2023
57
I fixed the braces, but now I have an error stating I have an "else" without a previous "if" function. It's referring to line 27 of the code below. How would I write in an "if" without wrecking the current function?

Could it be a "mirrored" version of line 16 where I copy that but replace LOW with HIGH?
Code:
int switchState = 0;
const unsigned long event_1 = 3000;
const unsigned long event_2 = 7000;
unsigned long previousTime = 0;
unsigned long currentTime = 0;
void setup(){
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
 
}

void loop(){
  currentTime = millis();
  switchState = digitalRead(2);
  if (switchState == LOW) {
    digitalWrite(3, HIGH); // green led
    digitalWrite(4, LOW); // red led
    previousTime = millis();
  }
  else {
    if (currentTime - previousTime >= event_1){
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
    }
  }
  else {
    if (currentTime - previousTime >= event_2){
      digitalWrite(3, HIGH);
      digitalWrite(4, LOW);
    }
  }
}
 

Thread Starter

Alln3w2m3

Joined Jun 20, 2023
57
This is why @MrChips advice about formatting is so important. If you rigorously indent for each open brace you will much more easily find the problem when they aren’t matched. It’s also easier to read. As an aside I make my tabs 2 spaces because more can quickly cause the code to overrun the right margin and make it much harder to read.
.
I will definitely take that advice. I think I'm going to copy this code line and then redo it with the extra spacing that you both have suggested. The first time I started trying to correct it within the original, I just made it worse.
 

MrChips

Joined Oct 2, 2009
34,866
C:
int switchState = 0;
const unsigned long event_1 = 3000;
const unsigned long event_2 = 7000;
unsigned long previousTime = 0;
unsigned long currentTime = 0;
void setup()
{
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop()
{ // start of loop
  currentTime = millis();
  switchState = digitalRead(2);
  if (switchState == LOW)
  { // switch low
    digitalWrite(3, HIGH); // green led
    digitalWrite(4, LOW); // red led
    previousTime = millis();
  } // switch low
  else

// not switch low - missing brace

  if (currentTime - previousTime >= event_1)
    { // event 1
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
    } // event 1
  else
   { // not event 1
    if (currentTime - previousTime >= event_2)
      { // event 2
        digitalWrite(3, HIGH);
        digitalWrite(4, LOW);
      } // event 2
    } // not event 1

// not switch low - missing brace

} // end of loop
 

Ya’akov

Joined Jan 27, 2019
10,258
Disregard my last, I believe I fixed the bracing. Sheesh, that was a subtle issue. I do have a new error that I will post in a separate reply.
I should say, I use a slightly different convention than @MrChips which is more comfortable for me but I don’t think has inherent superiority, like so:

Formatting, Ya’akov Style:
void function() {
    int someVariable = 0;
    while (someVariable < 10) {
        someVariable = read(something);
    };
    return(someVariable);
}
The theory is that the closing brace will always align with the line containing the opening brace, so long as you always indent when using a new open. Many editors will draw nice colored lines connected them, too.
 

MrChips

Joined Oct 2, 2009
34,866
There are two other constructs to be aware of.

1) You can have multiple AND conditions on the same IF statement.

if ( (condition1) && (condition2) )

2) There is such a construct as ELSE IF ( ).
 

Thread Starter

Alln3w2m3

Joined Jun 20, 2023
57
There are two other constructs to be aware of.

1) You can have multiple AND conditions on the same IF statement.

if ( (condition1) && (condition2) )

2) There is such as construct as ELSE IF ( ).
I think I need to use one of those, but I'll explain on another reply to your earlier comment.
 

Thread Starter

Alln3w2m3

Joined Jun 20, 2023
57
C:
int switchState = 0;
const unsigned long event_1 = 3000;
const unsigned long event_2 = 7000;
unsigned long previousTime = 0;
unsigned long currentTime = 0;
void setup()
{
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop()
{ // start of loop
  currentTime = millis();
  switchState = digitalRead(2);
  if (switchState == LOW)
  { // switch low
    digitalWrite(3, HIGH); // green led
    digitalWrite(4, LOW); // red led
    previousTime = millis();
  } // switch low
  else

// not switch low - missing brace

  if (currentTime - previousTime >= event_1)
    { // event 1
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
    } // event 1
  else
   { // not event 1
    if (currentTime - previousTime >= event_2)
      { // event 2
        digitalWrite(3, HIGH);
        digitalWrite(4, LOW);
      } // event 2
    } // not event 1

// not switch low - missing brace

} // end of loop
This worked perfectly at "resetting the switch" with each press (it will now begin the correct delay with each press without resetting the chip. However, I'm still trying to get it to switch off at a later time while retaining the initial delay. I assumed I was doing that and that your correction would, too. What did I miss?

I'm referring to the const in line 3 of the code below (which works perfectly as-is for the delay portion). I just need it to turn off now.
Code:
int switchState = 0;
const unsigned long event_1 = 3000;
const unsigned long event_2 = 7000;
unsigned long previousTime = 0;
unsigned long currentTime = 0;
void setup()
{
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop()
{
  currentTime = millis();
  switchState = digitalRead(2);
  if (switchState == LOW)
  {
    digitalWrite(3, HIGH); // green led
    digitalWrite(4, LOW); // red led
    previousTime = millis();
  }
  else
  {
  if (currentTime - previousTime >= event_1)
    {
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
    }
  else
   {
    if (currentTime - previousTime >= event_2)
      {
        digitalWrite(3, HIGH);
        digitalWrite(4, LOW);
      }
    }
  }
}
 

djsfantasi

Joined Apr 11, 2010
9,237
Your test for the second delay (line 32) is contained in the else block of the if statement for event 1 (line 25).

If the test for event 2 is true, won’t the test for event 1 also be true? Hence, the second test will never be executed.

I also suggest a flowchart or at least pseudo-code. This should help prevent grouping/ brave matching errors and clear up your logic.
 

Thread Starter

Alln3w2m3

Joined Jun 20, 2023
57
Before writing complex code, I would suggest that you draw a flowchart first.

https://forum.allaboutcircuits.com/ubs/structured-programming-part-1.1126/
I understand where you are coming from and I did read the link you attached. However, that is more theory and jargon that requires its own explanation, which creates a loop and never really answers my question. I admittedly don't know what I'm doing. I'm also not trying to make this a hobby. I've said this on another post but I'm just looking for a solution, not learn all of electrical engineering and construction. While those were lovely While & Do diagrams, I'm still nowhere.
 

Thread Starter

Alln3w2m3

Joined Jun 20, 2023
57
Your test for the second delay (line 32) is contained in the else block of the if statement for event 1 (line 25).

If the test for event 2 is true, won’t the test for event 1 also be true? Hence, the second test will never be executed.

I also suggest a flowchart or at least pseudo-code. This should help prevent grouping/ brave matching errors and clear up your logic.
I'm wondering if this is what Mr. Chips was referring to by suggesting the AND and ELSE IF functions? I'm not sure how to ungroup it.
 

djsfantasi

Joined Apr 11, 2010
9,237
I understand where you are coming from and I did read the link you attached. However, that is more theory and jargon that requires its own explanation, which creates a loop and never really answers my question. I admittedly don't know what I'm doing. I'm also not trying to make this a hobby. I've said this on another post but I'm just looking for a solution, not learn all of electrical engineering and construction. While those were lovely While & Do diagrams, I'm still nowhere.
I’d get rid of the last ELSE statement and check again that your braces match up. Start there and then debug the next issue. (I’m only guessing that there is another issue because I am a programmer)
 

djsfantasi

Joined Apr 11, 2010
9,237
ELSE IF would remove several sets of braces making the program easier to read. And use of AND would eliminate several nested IFs, agsin making it easier to read. For example. In the problem I have been discussing, I’d do something like:
If (event1 AND not event2) turn LED on
If (event2) turn LED off for a number of secs
Or something like that…
 

Thread Starter

Alln3w2m3

Joined Jun 20, 2023
57
ELSE IF would remove several sets of braces making the program easier to read. And use of AND would eliminate several nested IFs, agsin making it easier to read. For example. In the problem I have been discussing, I’d do something like:
If (event1 AND not event2) turn LED on
If (event2) turn LED off for a number of secs
Or something like that…
Ahhhh, that seems like it would work much better on several fronts. Thanks for the help.
 

Thread Starter

Alln3w2m3

Joined Jun 20, 2023
57
ELSE IF would remove several sets of braces making the program easier to read. And use of AND would eliminate several nested IFs, agsin making it easier to read. For example. In the problem I have been discussing, I’d do something like:
If (event1 AND not event2) turn LED on
If (event2) turn LED off for a number of secs
Or something like that…
Is what you are describing the same as in this link:
https://www.arduino.cc/reference/en/language/structure/control-structure/else/
 

MrChips

Joined Oct 2, 2009
34,866
I understand where you are coming from and I did read the link you attached. However, that is more theory and jargon that requires its own explanation, which creates a loop and never really answers my question. I admittedly don't know what I'm doing. I'm also not trying to make this a hobby. I've said this on another post but I'm just looking for a solution, not learn all of electrical engineering and construction. While those were lovely While & Do diagrams, I'm still nowhere.
Sorry to hear that.

You are attempting to write computer code with no programming skills. Learning to do so is no different than learning any new skill. It is like trying to be a weekend mechanic or a DIY sous chef. Sure, you can learn by trial and error and learning from your mistakes.

For any student in a computer programming course, it takes 2- 4 years of hard study and practice before one can become comfortable at writing proper code.

Sorry, I don't know of any short cuts.
 

strantor

Joined Oct 3, 2010
6,875
Would it be correct to say that the previousTime = millis(); causes the command to constantly refresh until the switch is closed and the timing portion begins to count?
Bob = currentTime
Steve = perviousTime
Bob and Steve are in a foot race, side by side, running exactly the same speed in lock step.
Bob never stops.
Steve stops instantly when you press the button and stays stopped as long as you hold it down.
If Bob gets 5000 steps ahead of Steve, the LEDs change state.
When you release the button, Steve teleports back to Bob's side.


Also, is it the unsigned long portion that facilitates the longer timing duration versus using "int" instead of "const"?
Yes, unsigned long and int are two different data types and unsigned long holds a lot more data than int. Your millis() timer can run for 50 days, counting every millisecond since power-on, using unsigned long. INT would only store about 66 seconds.

CONST means constant, something that doesn't change. As opposed to a variable, like you are using here.

The declaration is the creation of the variable or constant. We put it at the beginning and not in the loop because we do not need to generate a new version of Bob to replace the old Bob for every step of the foot race.
 

strantor

Joined Oct 3, 2010
6,875
Sorry to hear that.

You are attempting to write computer code with no programming skills. Learning to do so is no different than learning any new skill. It is like trying to be a weekend mechanic or a DIY sous chef. Sure, you can learn by trial and error and learning from your mistakes.

For any student in a computer programming course, it takes 2- 4 years of hard study and practice before one can become comfortable at writing proper code.

Sorry, I don't know of any short cuts.
We are the short cut and I'm fine with that.
 

djsfantasi

Joined Apr 11, 2010
9,237
Sorry to hear that.

You are attempting to write computer code with no programming skills. Learning to do so is no different than learning any new skill. It is like trying to be a weekend mechanic or a DIY sous chef. Sure, you can learn by trial and error and learning from your mistakes.

For any student in a computer programming course, it takes 2- 4 years of hard study and practice before one can become comfortable at writing proper code.

Sorry, I don't know of any short cuts.
Don’t be discouraging. One might take away that student in computer programming will take 2-4 years before they write a program.

What are they doing for 4 years? Why writing computer programs of course. But the first one will be very difficult and only gets easier.
 
Top