Led swing doesn`t work when I put no delay code and my code together

Hello,
I have a problem with putting no delay led swing and my code (controlls servo, relay and leds via ir remote) together.
No delay 7 led swing code works fine on it`s own , but when I import it to my code it lights up first 3 leds and turns them off, then it fires up last 4 leds and turns them off and loops that.

I copied everything from led swing code to my code,
I made if statement for digitalWrite part of the led swing code:
if (volPlusSwing== 1)

Under case(0x10) I put volPlusSwing = 1;
-case(0x10) executes when I press Volume plus button on the remote.

I didn`t write the code to make volPlusSwing = 0 after case isnt 0x10 yet, I must make the swing work first.

Also I didn`t write swing in opposite direction for case(0x11) (vol-) yet.

I made if (volPlusSwing== 0) statements for other parts of the code that could interupt my no delay swing code.

I also tried to remote every other digitalWrite command for LEDs (to make sure nothing is interupting it), but still no luck. After that I removed if(volPlusSwing== 1) statement to start with sweep as soon as arduino turns on, but still the same problem.

Any suggestions?
Marry christmas

together.txt (9.93 KB)

led swing.txt (6.08 KB)

My code.txt (6.51 KB)

Sorry, but almost everything is wrong in your post.

What is a LED swing?
Please trim, format and post your code in tags.
What is your code meant to do if it’s working as intended ?

lastchancename:
Sorry, but almost everything is wrong in your post.

What is a LED swing?
Please trim, format and post your code in tags.
What is your code meant to do if it’s working as intended ?

Hy,
led swing - leds1 to 7 turning on / off from 1 to 7.
There is no point of trimming the code. All parts of code work on its own. It doesnt work when I put it together.
My code does following:
led4 is high by default.
When i press power button on remote it turns on a relay, led 4 goes off.
Now it is in “ON” mode:
Moves servo (that will be connected to audio potentiometer) accoring to volume up or volume down buttons on remote.
When volume up is pressed it moves servo and it make led swing pattern from left to right. (must still write right to left for volume down).
When nothing is pressed it lights up leds according to servo angle (higher the angle, more leds are lit) -volume level.
Now my code did swing leds first when I had code made with delays.
Now I want the same thing without delays.

Ok, your code is VERY longhand (linear) Read up on arrays, and you can shrink it by more than 50%. Then the transition code can be made a lot smaller. You can/could use millis() to step through your swing states. (You probably don’t need a ‘state-machine’, because the objective is quite simple.

Start with creating arrays of your pins and LEDs, then you can easily iterate through their states.

Then, I’d use the buttons to choose the operating mode, while the LED sequencing just keeps running in the ‘background’ under millis.

lastchancename: Ok, your code is VERY longhand (linear) Read up on arrays, and you can shrink it by more than 50%. Then the transition code can be made a lot smaller. You can/could use millis() to step through your swing states. (You probably don’t need a ‘state-machine’, because the objective is quite simple.

Start with creating arrays of your pins and LEDs, then you can easily iterate through their states.

Then, I’d use the buttons to choose the operating mode, while the LED sequencing just keeps running in the ‘background’ under millis.

I know the code is long and complicated because of my limited knowledge, thank you for bearing up with me. I basicly started with servo ir code and I added thing by thing. I will learn about arrays and millis(0) I will try to optimize it.

I started optimizing my code with arrays, but for some reason it doesn`t work bellow “case(0x11):” part of the code and to the end.
It works from start of the code to the “case(0x11):”

#include <IRremote.h>
#include <Servo.h>



Servo myservo;
int ledPins[] = { 2, 3, 4, 5, 6, 7, 8}; 
int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
int ledPin6 = 7;
int ledPin7 = 8;
int relay = 12;
int receiver = 11;
IRrecv irrecv(receiver);
decode_results results;
int code=0;
int x;
int cont=0;
int volume=0;
int y=0;

void setup()
{
  pinMode(12, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  Serial.begin(9600); 
  irrecv.enableIRIn();
  myservo.attach(9);
  myservo.write(0);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, LOW);      
    digitalWrite(ledPin6, LOW);
    digitalWrite(ledPin7, LOW);


}
void loop()
{
    if (relay== LOW) {
digitalWrite(ledPins[3], HIGH);

     }
  if (relay== HIGH) {
 digitalWrite(ledPins[3], LOW);



  if (myservo.read() >= 2)   
  { 
    // turn on the LED
    digitalWrite(ledPins[1, 2, 3, 4, 5, 6], LOW);
    digitalWrite(ledPins[0], HIGH);
    

  }
  if (myservo.read() >= 25)   
  { 
    // turn on the LED
    digitalWrite(ledPins[2, 3, 4, 5, 6]  , LOW);
    digitalWrite(ledPins[0, 1], HIGH);



  }

  if (myservo.read() >= 51)   
  { 
    // turn on the LED
    digitalWrite(ledPins[3, 4, 5, 6], LOW);
    digitalWrite(ledPins[0, 1, 2], HIGH);



  }

  if (myservo.read() >= 77)   
  { 
    // turn on the LED
    digitalWrite(ledPins[0, 1, 2, 3]  , HIGH);
    digitalWrite(ledPins[4, 5, 6], LOW);

  }

  if (myservo.read() >= 103)   
  { 
    // turn on the LED
    digitalWrite(ledPins[0, 1, 2, 3, 4], HIGH);
    digitalWrite(ledPins[5, 6], LOW);


  }

  if (myservo.read() >= 128)   
  { 
    // turn on the LED
    digitalWrite(ledPins[0, 1, 2, 3, 4, 5], HIGH);
    digitalWrite(ledPins[6], LOW);


  }

  if (myservo.read() >= 153)   
  { 
    // turn on the LED
    digitalWrite(ledPins[0, 1, 2, 3, 4, 5, 6], HIGH);


  }
    else {



    }
}



  relay=digitalRead(12);

  if (irrecv.decode(&results))
  {
    code=results.value;
    Serial.println(code, HEX);
    irrecv.resume();
    switch(code)
    { 
      case(0x11):
   relay=digitalRead(12);
   myservo.read();
   if (relay== HIGH && ((myservo.read() > 3))) {
      Serial.println("dela-");
      cont=1;
      x=x-9;
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);      
    digitalWrite(ledPin6, LOW);
    digitalWrite(ledPin7, LOW);
    digitalWrite(ledPin7, HIGH);
    delay(35);
    digitalWrite(ledPin6, HIGH);
    delay(35);
    digitalWrite(ledPin7, LOW);
    delay(35);
    digitalWrite(ledPin5, HIGH);
    delay(35);
    digitalWrite(ledPin6, LOW);
    delay(35);
    digitalWrite(ledPin4, HIGH);
    delay(35);
    digitalWrite(ledPin5, LOW);
    delay(35);
    digitalWrite(ledPin3, HIGH);
    delay(35);
    digitalWrite(ledPin4, LOW);
    delay(35);
    digitalWrite(ledPin2, HIGH);
    delay(35);
    digitalWrite(ledPin3, LOW);
    delay(35);
    digitalWrite(ledPin2, HIGH);
    delay(35);
    digitalWrite(ledPin1, HIGH);
    delay(35);
    digitalWrite(ledPin2, LOW);
    delay(35);
    digitalWrite(ledPin1, LOW);
    delay(70);


      break;
}else{

    
  
      break;
}
      case(0x10):
         relay=digitalRead(12);
   if (relay== HIGH && ((myservo.read() < 178))) {
      Serial.println("dela+");
      cont=0;
      x=x+9;

    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);      
    digitalWrite(ledPin6, LOW);
    digitalWrite(ledPin7, LOW);
    digitalWrite(ledPin1, HIGH);
    delay(35);
    digitalWrite(ledPin2, HIGH);
    delay(35);
    digitalWrite(ledPin1, LOW);
    delay(35);
    digitalWrite(ledPin3, HIGH);
    delay(35);
    digitalWrite(ledPin2, LOW);
    delay(35);
    digitalWrite(ledPin4, HIGH);
    delay(35);
    digitalWrite(ledPin3, LOW);
    delay(35);
    digitalWrite(ledPin5, HIGH);
    delay(35);
    digitalWrite(ledPin4, LOW);
    delay(35);
    digitalWrite(ledPin6, HIGH);
    delay(35);
    digitalWrite(ledPin5, LOW);
    delay(35);
    digitalWrite(ledPin6, HIGH);
    delay(35);
    digitalWrite(ledPin7, HIGH);
    delay(35);
    digitalWrite(ledPin6, LOW);
    delay(35);
    digitalWrite(ledPin7, LOW);
    delay(35);


      break;
      }else{
      break;
}
      case(0x31):
      Serial.println("pwron");
relay=digitalRead(12);
if (relay== HIGH) {
    digitalWrite(12, LOW);
     irrecv.disableIRIn();
     delay (1000);
     irrecv.enableIRIn();

    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, HIGH);
    digitalWrite(ledPin6, HIGH);
    digitalWrite(ledPin7, HIGH);
    delay(400);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin7, LOW);
    delay(400);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin6, LOW);
    delay(400);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin5, LOW);
    delay(50);

    } else {
     digitalWrite(12, HIGH);

     irrecv.disableIRIn();
     delay (1000);
     irrecv.enableIRIn();
    }
      break;
      cont=2;
      break;

    }

    x=constrain(x,3,180);
    Serial.println(x);
    myservo.write(x);
    volume=map(x,3,180,0,100);
  }
 
if (relay== LOW) {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, LOW);      
    digitalWrite(ledPin6, LOW);
    digitalWrite(ledPin7, LOW);
}  
delay(50);

}

I can`t figure out what is wrong. If for example I repleace:

if (relay== LOW) {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, LOW);      
    digitalWrite(ledPin6, LOW);
    digitalWrite(ledPin7, LOW);
}  
delay(50);

with:

if (relay== LOW) {
    digitalWrite(ledPins[0, 1, 2, 4, 5, 6], LOW);
    digitalWrite(ledPins[3], HIGH);

}  
delay(50);

It doesnt do its thing…

relay has the value 12.
It will never equal LOW.

Or HIGH

digitalWrite(ledPins[0, 1, 2, 4, 5, 6], LOW);

digitalWrite(ledPins[3], HIGH);

The second line is the correct syntax, but the first does nothing... You need to ‘iterate’ (step) through each of the array members... probably with a for() loop. You’re heading in the right direction! Good on you for trying.

Some of these things you do repeatedly can be our into functions, but that will appear to you later.

TheMemberFormerlyKnownAsAWOL: relay has the value 12. It will never equal LOW.

Or HIGH

relay doesnt have value 12, its pin 12. Relay works fine.

popcorn time !

lastchancename: ``` digitalWrite(ledPins[0, 1, 2, 4, 5, 6], LOW);

digitalWrite(ledPins[3], HIGH);



The second line is the correct syntax, but the first does nothing...
You need to ‘iterate’ (step) through each of the array members... probably with a for() loop.
You’re heading in the right direction!
Good on you for trying.

Some of these things you do repeatedly can be our into functions, but that will appear to you later.

I will make a research on step iteration and see what I come up with and go from there. Thank you

0212995500229:
relay doesnt have value 12, its pin 12.
Relay works fine.

int relay = 12;

...

if (relay== LOW)

Like I said, relay has the value 12.

It will never equal LOW.
Or HIGH.

Did someone mention popcorn?

I did find this code:

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);

  }

But I don`t understand for to modify it to turn all pins in array HIGH or LOW at the same time, instead of one after another.
I could just write delay(0); and it should work or is there more neat way to do it?

Just lose the delay.

for (auto & pin : ledPins) {
    digitalWrite(pin, HIGH);
}

TheMemberFormerlyKnownAsAWOL: ``` int relay = 12;

...

if (relay== LOW)



Like I said, relay has the value 12.

It will never equal LOW.
Or HIGH.

Did someone mention popcorn?

int relay = 12; doesnt link relay to pin 12? Sorry Im new to this, I wrote it like this and it works. I have if (relay== low) turn relay high and vice versa and it does just that.

I will get read of delay and try.

HIGH has the value 1.
LOW has the value 0.

If you don’t believe me, you can check the source. Arduino.h, IIRC.

You have given relay the value 12.

Now, in which arithmetic system can 12 ever equal 0 or 1?

Finished my popcorn.

What do you think these do:

int relay = 12;

. . .

relay=digitalRead(12);


Did you add butter ?

larryd: you are right. My bad. It works anyways because my code changes the value under case(0x31).

Honestly I dont think that I will modify my code with that array thing with exception of led swing code to get the delay out. The code works, delay is my only problem, everything I change from this point just messes things up (with everything I modify i get wrong led behaviour) and I have a big problem that english isnt my first language, it just makes things even more complicated. I really don`t care how the code looks once I get it done I will install the circuit board with nano in speaker box and hopefully never look at the code again.

Software code is never finished ;)