if and for statements

Gdday all, I’m new to all this so please bear with me on my learning curve.
I’m using an if statement followed by a for statement after and the code won’t run. Am I going about this the wrong way?

I’ve remmed out the rest of the code and this seems to be the problem

if ((Lefthand == HIGH) && (brake == LOW) && (Haz == LOW)) // taking off with brakes off, lights still flashing
// timing out
for(int i = 0; i < 5;i++) { // timer at 10 seconds
Serial.println (i);

digitalWrite (Lefthandout,HIGH); // left flasher lights on
delay (froot); // delay on time
digitalWrite (Lefthandout,LOW); // left flasher lights off
delay (froot); // delay off time

}

Cheers Steve

Oh, if there’s any forum issues with etiquette etc. please let me know - I’m a 52 y.o. newbie :slight_smile:

Please:

  1. what do you want the program do?
  2. What the program does?
  3. put your complete code
  4. use tag codes

Other than poor style, there’s nothing fundamentally wrong with the snippet you posted. Beyond that can’t be speculated. The code runs (making the obvious changes to stand alone). the for statement should be enclosed in braces and terminated with a semi-colon. In a larger framework, this will cause problems and be be the source of your’s. Without seeing your code, that’s all you get.

the for statement should be enclosed in braces and terminated with a semi-colon.

Did you mean what you wrote ? If so can you please explain what you meant ?

UKHeliBob: Did you mean what you wrote ? If so can you please explain what you meant ?

The if statement has no { and there is no } after the for {} statement.

I’d also have have written it like this, as has been suggested. You’d have to do this anyway if you add further statements after the for statement which are conditioned by the if statement.

	if ((Lefthand == HIGH) && (brake == LOW) && (Haz == LOW)) {  // taking off with brakes off, lights still flashing
		// timing out
		for(int i = 0; i < 5;i++) {            // timer at 10 seconds
			Serial.println (i);

			digitalWrite (Lefthandout,HIGH);          // left flasher lights on
			delay (froot);                      // delay on time
			digitalWrite (Lefthandout,LOW);           // left flasher lights off
			delay (froot);                     // delay off time

		} ;

	}

The delay() statements in the loop may give problems if you want to do anything else in the loop().

If it doesn’t run as you expect, explicitly set the variables Lefthand, brake and Haz to HIGH, LOW and LOW respectively before the if statement to check your logic.

The if statement has no {

Whilst it would be better for the code block controlled by the if to be enclosed in { }, and I would do it as a matter of course, it is not actually needed.

For example :

int a = 1;

void setup()
{
  Serial.begin(115200);
  if (a == 1)
    for (int x = 0; x < 8; x++)
    {
      Serial.println(x);
    }
}

void loop()
{
}

As to the final } at the end of the if clause in the original code, then as there is no opening { then the closing one is not needed

I am still bemused by the comment about the semicolon.

the for statement should be enclosed in braces and terminated with a semi-colon.

On a more general note, I am horrified by the state of much code posted here, often unformatted and with multiple blank lines. I simply cannot understand how people code like that. Ctrl + T serves me well. I use it often when entering programs and the formatting can be changed to suit your own style such as moving { and } onto their own lines and deleting blank lines within functions, both of which I have set as defaults

I use it often when entering programs and the formatting can be changed to suit your own style such as moving { and } onto their own lines and deleting blank lines within functions, both of which I have set as defaults

Formatting is another issue. Beauty (of formatting) is in the eye of the beholder. I agree mine wasn't exemplary. I copied it out of Eclipse where the tabs are treated differently giving a spaced out look in the post.

UKHeliBob: On a more general note, I am horrified by the state of much code posted here, often unformatted and with multiple blank lines.

I suspect that some of the extra blanks come from some bit of code treating the "/r/n" of a Windows 'end of line' as two '/n' Unix 'newline' characters. The extra linefeeds seem to have started in relatively recent Arduino versions (roughly 1.8.4?). In one case it looked like someone has moved the code between Arduino and a Windows editor several times and each time the linefeeds got doubled! In that case the code was almost unreadable.

6v6gt: Formatting is another issue. Beauty (of formatting) is in the eye of the beholder. I agree mine wasn't exemplary. I copied it out of Eclipse where the tabs are treated differently giving a spaced out look in the post.

Don't take it personally. It was not your code that triggered my outburst of formatting OCD, it really was a general comment.

In practice it makes very little difference to me how code is formatted here because if it is more than a couple of lines I copy it into the IDE and Auto Format it as a matter of course.

UKHeliBob: Did you mean what you wrote ? If so can you please explain what you meant ?

My comment was merely pursuant to that of style.

DKWatson: My comment was merely pursuant to that of style.

Does that include the mention of the apparent need to terminate a for loop with a semicolon ?

The semicolon was meant to terminate to if statement.

DKWatson: The semicolon was meant to terminate to if statement.

Semicolon on the end of an if ?

Can you please provide an example of how it would be used ?

Point taken.

Gdday all, Supplying some more info…

I’m using windows 7 and uno R3

This program is for a '48 chev indicator circuit that has no self cancelling on the stalk switch.
Iv’e included hazard light function which works…
The idea is if your stopped at traffic lights foot on brake & then inch forward foot off brake that indicator keeps flashing and resetting a counter when foot comes on brake again.
Once you leave the intersection with brake off, the counter will count down 5 cycles and turn indicators off - not to come on until the stalk switch has been returned to off.

I’ve been toying with break,return & continue statements to no avail.
I’m thinking that I haven’t got my curly braces in the correct spots or semi colons etc.
The counter doesn’t get triggered at all

I’ve had my head in “getting started with Arduino” By Massimo Banzi but I seem to be missing keypoints with this whole programming structure…

Any tips Greatly appreciated as I’ve been going around in circles for weeks!

Sketch attached Cheers Steve

chev_hazind_3.ino (3.66 KB)

You thought about putting a reed switch/magnet on the steering column and counting wheel turns?

Or...

8 reed switches into a 165 and determine steering wheel position. You could maybe extend it to self steering... maybe not. Position would be good though, 1 of 8 values, -90 t0 90 degrees? Or every 45?

Yep reed switches or maybe a proximity sensor on the tailshaft to hold output high until I reach maybe 5 - 8mph...

I think I was going wrong with digitalread statements inside my if statement... thinking I have to assign them a name in the setup section... Hoping this works!

Cheers Steve

Keep me posted.