Newbie programming blink count leds

Hi all.
Newbie to this fancy programming stuff you got going here. Have spent days looking on the forums for help with this.
I have been on this problem for 5 day and serious thinking on why I pick this as a new hobby.
Anyhooooo, can anyone help me help me with my simple traffic lights? My program has bits and pieces of other examples.
I want "greenw" to flash 10 times then go "redw" using "IF".
Any suggestions?

int red = 10;
int yellow = 9;
int green = 8;
int redw = 11;
int greenw = 12;
int button = 13; // switch is on pin 12
int DELAY_15DB = 15;
int DELAY_25BK = 350;
int DELAY_HALFSEC = 500;
int DELAY_1SEC = 1000;
int DELAY_2SEC = 2000;
int DELAY_3SEC = 3000;
int DELAY_4SEC = 4000;
int DELAY_5SEC = 5000;
int DELAY_8SEC = 8000;
int DELAY_15SEC = 15000;
int maxnum = 5; // Blink the LED
int count = 0; // Our blink counter

void setup() {
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(redw, OUTPUT);
pinMode(greenw, OUTPUT);
pinMode(button, INPUT);
digitalWrite(green, HIGH);
digitalWrite(redw, HIGH);
}

// ***** This bit for Pedestrian push button
//void loop() {
// if (digitalRead(button) == HIGH){
// delay(15); // software debounce
//if (digitalRead(button) == HIGH) {
// if the switch is HIGH, ie. pushed down - change the lights!
// changeLights();
//delay(DELAY_15SEC); // wait for 15 seconds
//}
//}
//}

//****This bit for non stop
void loop()
{
changeLights();
delay(DELAY_2SEC);
}

void changeLights()
{
//green off, yellow on for 3 seconds
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
delay(DELAY_3SEC);

//turn off yellow, then turn on red for 3 seconds
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
delay(DELAY_3SEC);

// turn on green walk, and turn off red walk for 8 seconds
digitalWrite(greenw, HIGH);
digitalWrite(redw, LOW);
delay(DELAY_3SEC);

// Blink green walk, and turn on red walk

{
if (count > 4)

digitalWrite(redw, HIGH);
delay(DELAY_HALFSEC);
digitalWrite(redw, LOW);
count=0;

}

{ if (count < maxnum)
digitalWrite(greenw, LOW); // set the LED on
delay(350); // wait for a second
digitalWrite(greenw, HIGH); // set the LED off
delay(350); // wait for a second
//Serial.print(count);
count++; // add one (1) to our count
}

//turn on green, then turn off red

digitalWrite(redw, HIGH);
digitalWrite(greenw, LOW);
delay(DELAY_3SEC);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);

}

You can’t use “if” to make something happen 10 times.
You can use “if” to make something happen when some condition is met.
That “something” can be to do something 10 times...
Or it can be any other bit of code your decide to write...
Up to you.

Doing something 10 times when a condition is met, and using delay (since that seems to be your programming paradigm here)....

if (some condition you want to be met)
{
 for (uint8_t i = 0; i < 10; i++) {
  //put the code you want to repeat here... E.g.
  //turn led on
  //delay
  //turn led off
  //delay
 }
}

Note that sooner or later with this project (perhaps already) I feel you are going to come unstuck with using delay. That will be when you realise you want to do more than one thing at once.

There’s a famous meme, from before memes were famous, “real programmers don’t eat quiche”. I feel it should probably be updated for Arduino, “real Arduino programmers don’t use delay”?

i think this is closer to what you're trying to do (tested). I separated the conventional sequence: green, amber (in nj), red from the crosswalk sequence, allowing it to be optionally invoked when a button is pressed.

the problem is changeLights() is blocking and would be better off as state sequence so allowing a button press to be detected at anytime during the sequence

#if 0
int red = 10;
int yellow = 9;
int green = 8;
int redw = 11;
int greenw = 12;
int button = 13; // switch is on pin 12

#else
# define ON  LOW
# define OFF HIGH

# define LedRed1    10
# define LedAmber1  11
# define LedGreen1  12

# define LedRedW    0
# define LedGreenW  13       // fake

byte ledPins [] = {
    LedRed1, LedAmber1, LedGreen1,
    LedRedW, LedGreenW
};
# define N_PINS sizeof(ledPins)

int red     = LedRed1;
int yellow  = LedAmber1;
int green   = LedGreen1;
int redw    = LedRedW;
int greenw  = LedGreenW;

byte butPin = A1;
byte butLst;
#endif

int DELAY_15DB = 15;
int DELAY_25BK = 350;
int DELAY_HALFSEC = 500;

int ONE_SEC   = 1000;
int TWO_SEC   = 2000;
int THREE_SEC = 3000;
int FOUR_SEC  = 4000;
int FIVE_SEC  = 5000;
int EIGHT_SEC = 8000;
int DELAY_15SEC = 15000;
int maxnum = 5;     // Blink the LED
int count = 0;       // Our blink counter

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin (115200);

    for (unsigned n = 0; n < N_PINS; n++)  {
        digitalWrite (ledPins [n], OFF);
        pinMode      (ledPins [n], OUTPUT);
    }

    delay (2000);

    pinMode (butPin, INPUT_PULLUP);
    butLst = digitalRead (butPin);
}

// -----------------------------------------------------------------------------
// ***** This bit for Pedestrian push button
//void loop() {
    //    if (digitalRead(button) == HIGH){
        //        delay(15); // software debounce
        //if (digitalRead(button) == HIGH) {
            // if the switch is HIGH, ie. pushed down - change the lights!
            //    changeLights();
            //delay(DELAY_15SEC); // wait for 15 seconds
        //}

    //}

//}

// -----------------------------------------------------------------------------
//****This bit for non stop
void loop()
{
    changeLights();
    crossWalk ();
}

// -----------------------------------------------------------------------------
void crossWalk()
{
    Serial.println ("cross walk");

    digitalWrite(redw, OFF);
    for (count = 5; count > 0; count--)  {
        digitalWrite(greenw, ON);
        delay(DELAY_HALFSEC);
        digitalWrite(greenw, OFF);
        delay(DELAY_HALFSEC);
    }

    digitalWrite(redw, ON);
}

// -----------------------------------------------------------------------------
void changeLights()
{
    Serial.println ("green");
    digitalWrite(red,  OFF);
    digitalWrite(green, ON);

    delay(FIVE_SEC);

    Serial.println ("yellow");
    digitalWrite(green, OFF);
    digitalWrite(yellow, ON);

    delay(ONE_SEC);

    Serial.println ("red");
    digitalWrite(yellow, OFF);
    digitalWrite(red, ON);

    delay(FIVE_SEC);
}

I very much appreciate your help and effort guys. However, you have lost me big time.

I was hoping for an answer that I could understand but to no avail.

I am such a newbie (5 days) I have no idea on the code you have posted and what it does.

I'll keep investigating and experimenting. Thanks again.

Not a problem.

oldsid:
I was hoping for an answer that I could understand but to no avail.

No offence intended, but the answers you received will no doubt have been targetted at the level of understanding we assumed you had from the code you already posted.

I know you said you pieced it together from elsewhere, but we do kind of assume posters have some kind of basic understanding of the code they create and post.

If not, then your question should probably be "how does this aspect of my code work?", not "how do I add more complexity to something I don't yet understand?".

If you are having trouble there are lots of step by step tutorials that will build up to the sketch shown above. No point jumping in the deep end. For example you might consider starting at the Tutorials sticky. So please don't be put off, the key to learning any new skill is to start at the correct level.

oldsid:
can anyone help me help me with my simple traffic lights? My program has bits and pieces of other examples.
I want "greenw" to flash 10 times then go "redw" using "IF".
Any suggestions?

sorry if the answer wasn't what you expected.

besides 1) trying to figure out what is wrong with the programs posted on these forums, is trying to figure out 2) what the poster intended to do with the code, as well as 3) what they are trying to ask.

in your case, why do you think the answer involves an "IF"? (i thought i addressed that future possibility)

one of the best ways to learn to write code is to understand other well written code. Have you tried to understand the modifications i made to your code?

Hi gcjr.

I've had a crack at your code and will have to do some adjustments to get it to do what I want.
There is a lot of '#" in there that I haven't seen before.

pcbbc.
Thanks for the help with the link.

oldsid:
There is a lot of '#" in there that I haven't seen before

#if/#else/#endif are a more convenient way of disabling or replacing code that simply commenting it out.

i try to only post code I've tested on a Multifunction Board using the buttons and LEDs on the pins it has. The #if/#else is an easy way for you to revert back to your pin definitions. change if from "#if 0" to "#if 1"

i think you only want to call crossWalk() in loop() "if" a button is pressed.

Unfortunately because the changeLights() code runs for a period of time preventing other code from running, the button must be pressed when changeLights() completes.

a better and more convention approach is to keep track of the light sequence state and use millis() to track time to determine when to change state and the lights. It would allow constantly monitoring for a button press while the light sequence is proceeding