Need some quick help with flashing leds

With this code only led 1 and 2 blink. Not sure why 3 and 4 wont blink.

const int ledPin1 = 13;// the number of the LED pin const int ledPin2 = 12; const int ledPin3 = 11; const int ledPin4 = 10; const long baseinterval = 1000;

unsigned long ledPintimer1 = 0; unsigned long ledPintimer2 = 0; unsigned long ledPintimer3 = 0; unsigned long ledPintimer4 = 0;

void setup() { // set the digital pin as output or input: pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); pinMode(ledPin4, OUTPUT); }

void flashLED() {

if (digitalRead (ledPin1) == LOW) digitalWrite (ledPin1, HIGH); else digitalWrite (ledPin1, LOW);

// remember when we toggled it ledPintimer1 = millis (); }

void flashLED2 () { if (digitalRead (ledPin2) == LOW) digitalWrite (ledPin2, HIGH); else digitalWrite (ledPin2, LOW);

// remember when we toggled it ledPintimer2 = millis (); }

void flashLED3 () { if (digitalRead (ledPin3) == LOW) digitalWrite (ledPin3, HIGH); else digitalWrite (ledPin3, LOW);

// remember when we toggled it ledPintimer3 = millis (); }

void flashLED4 () { if (digitalRead (ledPin4) == LOW) digitalWrite (ledPin4, HIGH); else digitalWrite (ledPin4, LOW);

// remember when we toggled it ledPintimer4 = millis (); }

void loop () {

// Handling the blink of LED'S.

if (millis () - ledPintimer1 > baseinterval) flashLED ();

if (millis () - ledPintimer2 > baseinterval) flashLED2 ();

if(millis () - ledPintimer3 > baseinterval) flashLED3;

if(millis () - ledPintimer4 > baseinterval) flashLED4; }

if (millis () - ledPintimer1 > baseinterval)
     flashLED ();
  
     if (millis () - ledPintimer2 > baseinterval)
    flashLED2 ();
    
    if(millis () - ledPintimer3 > baseinterval)
    flashLED3;
    
    if(millis () - ledPintimer4 > baseinterval)
    flashLED4;

Nothing, 2, 3, 4. Not a reasonable thing to do.

Have you verified that the LED is wired correctly? A simple sketch to simply turn pins 10 through 13 on is all that is needed to verify that the hardware is correct.

This is only part of a bigger program. I need multiple conditions to access the flashing routine. I would post the larger program but it is a mess and not complete.

Of course it is wired correctly

... and just what is the purpose of digitalRead from a pin that's state is an output? Won't it just toggle?

cjdelphi: ... and just what is the purpose of digitalRead from a pin that's state is an output? Won't it just toggle?

I have been blinking leds that way for a while and have never ran into any problems..... well that is until now. The leds flash according to the delay, not just toggle

I would post the larger program but it is a mess

I never understand this. A sketch should be developed by adding functions that have been developed, tested, and validated in a small sketch. When flashLEDOnPin7() works by itself in one sketch, it should work in a larger sketch. Adding tested functions this way does NOT result in a “larger program that is a a mess”.

Oh, and, it is possible that the “larger program but it is a mess” is simply running out of memory, causing all kinds of strange behavior.

Mess or not, you may need to post it.

if (time1 = HIGH)

You are assigning a value, here, not comparing the values.

PaulS: if (time1 = HIGH)

You are assigning a value, here, not comparing the values.

I am aware of this. Along with the many many other errors. Hence why I did not want to post it.

I am aware of this. Along with the many many other errors. Hence why I did not want to post it.

When do you plan to take the time to develop the sketch correctly? Fixing those errors is trivial.

I'm not going to participate in this thread any more. I can't see that you are interested in fixing your code.

PaulS:

I am aware of this. Along with the many many other errors. Hence why I did not want to post it.

When do you plan to take the time to develop the sketch correctly? Fixing those errors is trivial.

I'm not going to participate in this thread any more. I can't see that you are interested in fixing your code.

I'm interested in fixing the flashing at this point. Not ready to move on to other parts of the program yet. If I can't get the basic flashing to work I can't move on.

To keep the thread on track I am going to delete the larger and very badly written program.

Try adding () to the function calls ...

   if(millis () - ledPintimer3 > baseinterval)
    flashLED3();
    
    if(millis () - ledPintimer4 > baseinterval)
    flashLED4();

If you were to modify your code like this (I have just shown code for one LED) all the decision making would be contained in the function.

And if you used arrays to store the values of pins and timings you could flash all the leds with one piece of code so they either all worked or none worked which would mean you only have one piece of code to fix.

void loop() {
     curMillis = millis();  // use the same time for everything
     flashLed4();
  }  

 void flashLED4 ()
  {
   if(curMillis - ledPintimer4 < baseinterval) {    // if it is not the right time, do nothing
       return;
   } 
  
   digitalWrite(ledPin4, ! digitalRead(ledPin4);   // toggle the value

  // remember when we toggled it
  ledPintimer4 += baseinterval;    // this approach avoids timing drift
  }

…R

Hackscribble: Try adding () to the function calls ...

   if(millis () - ledPintimer3 > baseinterval)
    flashLED3();
    
    if(millis () - ledPintimer4 > baseinterval)
    flashLED4();

Oh duh! Overlooked it. Thanks