Small programming problem. Might be me.

Hello! I'm not the greatest at scripting but I can do some. The problem with my code, is that I can only run both RGB led's or the 4 scrolling led's. What I'm trying to do is to have the 4 scrolling led's going all the time and the RGB led's going on there own program. I shortend my code alot so it will fit the 9500 charaters rule. If you could please help me with this quick fix, thanks. If not, that's ok to.

/*-----------------------------------------
  The main purpose of this vode is to
flash 2 RGB led's and 4 scrolling led's
at the same time. The problem is, I can
only have one of them on at a time. 
Eaither the 4 scrolling led's, are the
RGB led's.

Hope you like my code cause It took a 
little time to create!  I mean it works
just fine accept for the 4 led's.
      -Soapy29
------------------------------------------*/
int potPin= 5;
int potVal= 0;
int delay1= 50;
int delay2= 60;

void setup() {
      //----------------------------------------
      //  Sets up the leds to work.
      //----------------------------------------
      pinMode(12, OUTPUT);
      pinMode(11, OUTPUT);
      pinMode(10, OUTPUT);
      pinMode(9, OUTPUT);
      pinMode(8, OUTPUT);
      pinMode(7, OUTPUT);
      pinMode(6, OUTPUT);
      pinMode(5, OUTPUT);
      digitalWrite(12, HIGH);
      digitalWrite(11, HIGH);
      digitalWrite(10, HIGH);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(7, LOW);
      digitalWrite(6, LOW);
      digitalWrite(5, LOW);
}

void loop() {
        //------------------------------
        //  This part scrolls 4 led's.
        //------------------------------
        //while (true) {
        // scroll();
        //}
      //----------------------------
      //  Main commands.
      //----------------------------
      delay1= analogRead(potPin);
      rR();
      rR();
      rR();
      rL();
      rL();
      rL();
      bR();
      bR();
      bR();
      bL();
      bL();
      bL();
        rR();
      rR();
      rR();
      rL();
      rL();
      rL();
      bR();
      bR();
      bR();
      bL();
      bL();
      bL();
      blink1();
      blink2();
        blink3();
        blink4();
        blink1();
        blink2();
        blink3();
        blink4();
        blink1();
        blink2();
        blink3();
        blink4();
}

void rR() {
      //-------------------------------------
      //  Blinks the right led red.
      //-------------------------------------
      delay1= analogRead(potPin);
      digitalWrite(12, LOW);
      delay(delay1);
      digitalWrite(12, HIGH);
      delay(delay1);
}

void rL() {
      //-----------------------------------
      //  Blinks the left led red.
      //-----------------------------------
      delay1= analogRead(potPin);
      digitalWrite(11, LOW);
      delay(delay1);
      digitalWrite(11, HIGH);
      delay(delay1);
}

void bR() {
      //--------------------------------------
      //  Blinks the right led blue.
      //--------------------------------------
      delay1= analogRead(potPin);
      digitalWrite(10, LOW);
      delay(delay1);
      digitalWrite(10, HIGH);
      delay(delay1);
}

void bL() {
      //------------------------------------
      //  Blinks the left led blue.
      //------------------------------------
      delay1= analogRead(potPin);
      digitalWrite(9, LOW);
      delay(delay1);
      digitalWrite(9, HIGH);
      delay(delay1);
}
//------------------------------------------------
//  Blinks the led's different colors.
//------------------------------------------------
void blink1() {
        //rR
      delay1= analogRead(potPin);
      digitalWrite(12, LOW);
      delay(delay1);
      digitalWrite(12, HIGH);
}
void blink2() {
        //rL
        delay1= analogRead(potPin);
      digitalWrite(11, LOW);
      delay(delay1);
      digitalWrite(11, HIGH);
}
void blink3() {
        //bR
        delay1= analogRead(potPin);
      digitalWrite(10, LOW);
      delay(delay1);
      digitalWrite(10, HIGH);
}
void blink4() {
        //bL
        delay1= analogRead(potPin);
      digitalWrite(9, LOW);
      delay(delay1);
        digitalWrite(9, HIGH);
}

void scroll() {
  digitalWrite(8, LOW);
  digitalWrite(7, HIGH);
  delay(delay2);
  digitalWrite(7, LOW);
  digitalWrite(6, HIGH);
  delay(delay2);
  digitalWrite(6, LOW);
  digitalWrite(5, HIGH);
  delay(delay2);
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  delay(delay2);
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  delay(delay2);
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  delay(delay2);
}

The key to getting your Sketch to work is "blink without delay". I suggest spending a bit of time experimenting with the example...

DO NOT BE FOOLED: A CURRENT LIMITING RESISTOR MUST BE INCLUDED WITH THE LED.

I shortend my code alot so it will fit the 9500 charaters rule.

Still got a long ways to go. Look at the setup function. You are setting the pinMode and setting the state for each pin from 5 to 12, using 16 lines.

for(byte b=5; b<=12; b++)
{
   pinMode(b, OUTPUT);
   digitalWrite(b, HIGH);
}

Same effect in 5 lines.

Look at rR and rL. Exactly the same code, except for the hardcoded pin number. Make the pin number an input to the function, and use one function. bR and bL do the same thing, too.

The blink1, blink2, blink3, and blink4 functions are identical, too, except for pin number.

Can someone please help me fix my code? Help me find the right loop to do both things? (Without shortening my code.)

Without shortening my code.

Not me, sorry. Modifying a dozen functions to properly implement blinking without using delay is far more effort than modifying only two functions.

...I'll try my best to do what I can, but this is really bugging me that no one will help me. What more do I have to offer to help you?

"Helping" and "doing it for you" are very different things.

In order to achieve the result you want (running two different patterns at the same time), you would need to re-engineer your code. You would need to add a state machine, change how your patterns are stored, and implement using the millis() function.

That's a huge chunk of work and not something some one can just "help" you write.

What's the mills function and don't refer me to the blink without delay cause that's to hard. Most of this stuff is brand new to me and I've never done anything like this in my life. Sorry for being new and dumb.

There is a difference between ignorance and stupidity. Ignorance is a lack of information or knowledge. Stupidity (being dumb) is lack of desire to gain that knowledge.

The BlinkWithoutDelay is example of a building block. You need to experiment with the millis() function (which is shown in the BlinkWithoutDelay example.) It will teach you how to make the Arduino to appear to be doing two things at once.

Start with the BlinkWithoutDelay and understand how it works. Until you do, you won't be able to achieve the effect you desire.

The blink without delay example boils down to a function call (millis), a simple arithmetic operation and a comparison.
Point me to the hard bit.

What is hard is trying to get you to look at your code, take advice and simplify it.

Your current sketch is over 180 lines long, and making changes to it probably looks quite daunting.

Without trying very hard, I reduced it to fewer than 50 lines for exactly the same functionality.
Now modifications don't looks so tedious or difficult.

Ok. Well I am new and I am learning quick, but I'm still not the greatest. I'll leave my code the way it is and try to learn and understand the mills function. Yes I have seen my code and it is pretty long, but it didn't even fit into this textbox before.

When you are ignoring the advice of at least three people over two threads to SIMPLIFY YOUR CODE, then 'learning quick' doesn't really seem true, does it?

Ok well I'm starting to try to learn the Millis function, but I also have a few questions? Why am I getting "lvalue required as left operand of assignment" for an outout of this code:

/*-----------------------------------------
  The main purpose of this vode is to
flash 2 RGB led's and 4 scrolling led's
at the same time. The problem is, I can
only have one of them on at a time. 
Eaither the 4 scrolling led's, are the
RGB led's.

Hope you like my code cause It took a 
little time to create!  I mean it works
just fine accept for the 4 led's.
      -Soapy29
------------------------------------------*/
int potPin= 5;
int potVal= 0;
int delay1= 50;
int delay2= 60;
int ledState= LOW;
long previousMillis= 0;
long interval= 0;

void setup() {
      //----------------------------------------
      //  Sets up the leds to work.
      //----------------------------------------
      pinMode(12, OUTPUT);
      pinMode(11, OUTPUT);
      pinMode(10, OUTPUT);
      pinMode(9, OUTPUT);
      pinMode(8, OUTPUT);
      pinMode(7, OUTPUT);
      pinMode(6, OUTPUT);
      pinMode(5, OUTPUT);
      digitalWrite(12, HIGH);
      digitalWrite(11, HIGH);
      digitalWrite(10, HIGH);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(7, LOW);
      digitalWrite(6, LOW);
      digitalWrite(5, LOW);
       // long interval= 0;
}

void loop() {
        unsigned long currentMillis= millis();
        
        if(currentMillis - previousMillis = interval) {
          previousMillis= currentMillis;
          if (1 == 1){
            scroll();
          }
         if(currentMillis - previousMillis = interval) {
            previousMillis= currentMillis;
            if (1 == 1) {
              delay1= analogRead(potPin);
      rR();
      rR();
      rR();
      rL();
      rL();
      rL();
      bR();
      bR();
      bR();
      bL();
      bL();
      bL();
        rR();
      rR();
      rR();
      rL();
      rL();
      rL();
      bR();
      bR();
      bR();
      bL();
      bL();
      bL();
      blink1();
      blink2();
        blink3();
        blink4();
        blink1();
        blink2();
        blink3();
        blink4();
        blink1();
        blink2();
        blink3();
        blink4();
            }
          }
           
          
        }
        //------------------------------
        //  This part scrolls 4 led's.
        //------------------------------
        //while (true) {
        // scroll();
        //}
      //----------------------------
      //  Main commands.
      //----------------------------
      /*delay1= analogRead(potPin);
      rR();
      rR();
      rR();
      rL();
      rL();
      rL();
      bR();
      bR();
      bR();
      bL();
      bL();
      bL();
        rR();
      rR();
      rR();
      rL();
      rL();
      rL();
      bR();
      bR();
      bR();
      bL();
      bL();
      bL();
      blink1();
      blink2();
        blink3();
        blink4();
        blink1();
        blink2();
        blink3();
        blink4();
        blink1();
        blink2();
        blink3();
        blink4();*/
}

void rR() {
      //-------------------------------------
      //  Blinks the right led red.
      //-------------------------------------
      delay1= analogRead(potPin);
      digitalWrite(12, LOW);
      delay(delay1);
      digitalWrite(12, HIGH);
      delay(delay1);
}

void rL() {
      //-----------------------------------
      //  Blinks the left led red.
      //-----------------------------------
      delay1= analogRead(potPin);
      digitalWrite(11, LOW);
      delay(delay1);
      digitalWrite(11, HIGH);
      delay(delay1);
}

void bR() {
      //--------------------------------------
      //  Blinks the right led blue.
      //--------------------------------------
      delay1= analogRead(potPin);
      digitalWrite(10, LOW);
      delay(delay1);
      digitalWrite(10, HIGH);
      delay(delay1);
}

void bL() {
      //------------------------------------
      //  Blinks the left led blue.
      //------------------------------------
      delay1= analogRead(potPin);
      digitalWrite(9, LOW);
      delay(delay1);
      digitalWrite(9, HIGH);
      delay(delay1);
}
//------------------------------------------------
//  Blinks the led's different colors.
//------------------------------------------------
void blink1() {
        //rR
      delay1= analogRead(potPin);
      digitalWrite(12, LOW);
      delay(delay1);
      digitalWrite(12, HIGH);
}
void blink2() {
        //rL
        delay1= analogRead(potPin);
      digitalWrite(11, LOW);
      delay(delay1);
      digitalWrite(11, HIGH);
}
void blink3() {
        //bR
        delay1= analogRead(potPin);
      digitalWrite(10, LOW);
      delay(delay1);
      digitalWrite(10, HIGH);
}
void blink4() {
        //bL
        delay1= analogRead(potPin);
      digitalWrite(9, LOW);
      delay(delay1);
        digitalWrite(9, HIGH);
}

void scroll() {
  digitalWrite(8, LOW);
  digitalWrite(7, HIGH);
  delay(delay2);
  digitalWrite(7, LOW);
  digitalWrite(6, HIGH);
  delay(delay2);
  digitalWrite(6, LOW);
  digitalWrite(5, HIGH);
  delay(delay2);
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  delay(delay2);
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  delay(delay2);
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  delay(delay2);
}
        if(currentMillis - previousMillis = interval) {

= is the assignment operator. == is the equality operator.

You really want >= here, by the way. The difference between the two values my not be exactly equal to interval, and you need to wait 7+ weeks for another chance.

      pinMode(12, OUTPUT);
      pinMode(11, OUTPUT);
      pinMode(10, OUTPUT);
      pinMode(9, OUTPUT);
      pinMode(8, OUTPUT);
      pinMode(7, OUTPUT);
      pinMode(6, OUTPUT);
      pinMode(5, OUTPUT);
      digitalWrite(12, HIGH);
      digitalWrite(11, HIGH);
      digitalWrite(10, HIGH);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(7, LOW);
      digitalWrite(6, LOW);
      digitalWrite(5, LOW);

Loops, man, loops. Quite writing the same code...

Oh, thank you. And I need to wait 7+ weeks for what?

Ok, I changed it and it sorta works. When I say sorta, I mean, one thing only works when the potentiometer is at the fullest, and it will do the other thing anyother place, but just not at the same place.

Loops, man, loops. Quite writing the same code...

What do you mean by that?

And I need to wait 7+ weeks for what?

Suppose interval had a value of 1000, and previousMillis had a value of 0.

Suppose that millis() returned 13, 17, 20, 25, .... 995, 998, 1001, 1004...

998 + 0 is less than interval, so wait some more. 1001 - 0 is not equal to interval, either, so wait some more. How long? Well, you need to wait until the value returned by millis() will no longer fit in an unsigned long, so it rolls over, and returns small numbers again. The time that takes is greater than 49 days, or 7+ weeks.

Even then, you aren't guaranteed an exact match. So, you should never use == in a time test. You want to quit waiting (or doing) when the time waiting (or doing) equals or exceeds the time to wait (or do) whatever.

As I pointed out earlier, you can use a for loop to call the pinMode function and to call the digitalWrite function, and have many fewer lines of code.

I still don't get this millis function. It just doesn't make sence that I need to use millis to create a loop to do 2 things... Isn't there an easier way to do it?

There is a much easier way - write your sketch in occam, a language specifically designed to express concurrency.
Or maybe look at one of the timed action libraries.
But please - use loops and simplify your code.
You think it is long now, but add a couple of LEDs, and the way you're going, it'll end up huge.

@Soapy
I think you have to spend time reading the examples in the tutorial section to get understanding of how the Arduino works, and how things can be done.

Furthermore there is a section about arrays in the reference section very worth reading - http://www.arduino.cc/en/Reference/Array -

As you are new and eager to learn, please spend some time in the tutorial section as there is so much information. Takes maybe 2 weeks but the comments made here will make so much more sense to you. I know as I have been a completely newbie once myself - and still am in lot of areas :slight_smile:

It's good to know that someone knows what I'm going through because I'm new to this platform and typing code. I will look at the site as soon as posible. I have been reading some of the examples, but they dont make the greatest sence.