Functions / IF Functions // Interrupts perhaps?

Hi all,

I’m getting myself in a right pickle today.

I’ve designed an 8 halo setup for some lights. There are 4 amber halo, with 4 white halo.

Each fog unit, has an amber and a white halo in.

The idea is to replicate the modern car lights, the switch-back systems that turn the white side lights off during indication.

I’m struggling to figure out how to get the indication input to trigger the amber halo to turn on, but also to replicate the input 5v and then a delay afterward upon which the side light will turn back on (on that side).

Pins 3, 5, 7 and 9 are the side lights. I want these to turn OFF during A3 input (I’ve used a voltage divider from 12v indication source to get 5v input).

I did try using digital inputs, but I couldn’t make it work. I was using pin 10/11

Pins 2, 4, 6 and 8 are the amber. When A3 indicates, I would like 2 and 4 to turn on, at the same time as the A3 input and then turn off at the same time. Ensuring that the side lights don’t turn on.

I’ve looked at interrupts, but not managed to use them correctly. Also tried to use void functions (which is in my current code) but it just gets stuck. So it may not be the best method either.

All help very much welcomed!

Thanks guys

int analogPin1 = A2;     // val
const int analogPin2 = A3;     // left indicator 5v input
const int analogPin3 = A4;     // right indicator 5v input
                       
int val = 0;           // Switch input value 
int inputVariable = 0;
int variable = 0;

int left = 0;
int right = 0;




void setup()
{


  
  Serial.begin(9600);              //  setup serial

  pinMode(A2, INPUT);   // Switch input, side light brightness and also max value setting for fade into variable function
  pinMode(A3, INPUT);   
  pinMode(A4, INPUT);

  digitalWrite (A2, LOW);  // internal pull-down resistor
  digitalWrite (A3, LOW);  // internal pull-down resistor
  digitalWrite (A4, LOW);  // internal pull-down resistor
  
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);

  digitalWrite (10, LOW);
  digitalWrite (11, LOW);

  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 4) {
    // sets the value (range from 0 to 255):
    analogWrite(4, fadeValue);
    analogWrite(8, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

delay(500);

  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 4) {
    // sets the value (range from 0 to 255):
    analogWrite(2, fadeValue);
    analogWrite(6, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  delay(500);
    
    // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 4) {
    // sets the value (range from 0 to 255):
    analogWrite(2, fadeValue);
    analogWrite(4, fadeValue);
    analogWrite(6, fadeValue);
    analogWrite(8, fadeValue);
    inputVariable = analogRead;
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

digitalWrite(2, LOW);
digitalWrite(4, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);

delay(300);

  val = analogRead(analogPin1);     // read the input pin

  if (val < 50) 
  { variable = 50;  }  
  else if (val < 600) 
  { variable = 126;  }
  else if (val < 800) 
  { variable = 192;  }
  else if (val > 800) 
  { variable = 255;  }

  Serial.print("Setup serial function, value is ");
  Serial.print(val);             // debug value
  Serial.print(variable);

  for (int fadeValue = 0 ; fadeValue <= variable; fadeValue += 6) {
    // sets the value (range from 0 to 255):
    analogWrite(3, fadeValue);
    analogWrite(5, fadeValue);
    analogWrite(7, fadeValue);
    analogWrite(9, fadeValue);
    
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }





}

void loop()
{
  
  val = analogRead(analogPin1);     // read the input pin
  Serial.print("Switch value is: ");
  Serial.println(val);             // debug value

  if (val < 50) { analogWrite (3, 126);  }  
  else if (val < 600) { analogWrite (3, 126);  }
  else if (val < 800) { analogWrite (3, 192);  }
  else if (val > 800) { analogWrite (3, 255);  }
  
  if (val < 50) { analogWrite (5, 50);  }  
  else if (val < 600) { analogWrite (5, 126);  }
  else if (val < 800) { analogWrite (5, 192);  }
  else if (val > 800) { analogWrite (5, 255);  }

  if (val < 50) { analogWrite (7, 50);  }  
  else if (val < 600) { analogWrite (7, 126);  }
  else if (val < 800) { analogWrite (7, 192);  }
  else if (val > 800) { analogWrite (7, 255);  }

  if (val < 50) { analogWrite (9, 50);  }  
  else if (val < 600) { analogWrite (9, 126);  }
  else if (val < 800) { analogWrite (9, 192);  }
  else if (val > 800) { analogWrite (9, 255);  }

  left = analogRead(analogPin2);     // read the input pin
  Serial.print("LEFT Indicator value is: ");
  Serial.println(left);             // debug value
 
  right = analogRead(analogPin3);     // read the input pin
  Serial.print("RIGHT Indicator value is: ");
  Serial.println(right);             // debug value

delay(500);

}

be a little neater using this

for ( byte x = 2 ; x < 10 ; x++ ){

 pinMode(x , OUTPUT);

}

instead of this

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);

Thanks for the advice, I'm fairly new to coding so I'm still learning. So just been writing everything in basic format.

I'll update it, thanks for your advice so far mate :slight_smile:

I'm kind of struggling what you want to do. And what KASSIMSAMJI says has to do with it. Express in the code what each pin does by giving it a explaining variable name. Besides output 2 to 9, what you do for the annalogs is completely useless:

const int analogPin2 = A3;     // left indicator 5v input

//make that
const byte LeftIndicatorInput = A3;

etc. Variable names are there to help YOU. To make it easy for YOU (and future you and now us ;p) to understand the code.

mrevolve:
I want these to turn OFF during A3 input (I've used a voltage divider from 12v indication source to get 5v input).

I did try using digital inputs, but I couldn't make it work. I was using pin 10/11

Doesn't make sense either because you use them purely digital...

About timing, if you want to do/monitor multiple things, stop using delay()'s, ever, anywhere :slight_smile: See Blink without delay.

And which board do you use?

septillion:
I'm kind of struggling what you want to do.

When the 4 side lights turn on, I want them to turn off when either side (A3 or A4) shows an input. So the car can indicate, with the side lights off.

septillion:
Besides output 2 to 9, what you do for the annalogs is completely useless:

I'm still learning what all the functions do, some of them I'm copying from other sketches, apologies for over-complicating some of the misunderstood entries in the code.

septillion:
And which board do you use?

Currently using a Mega 2560.

I explained how the circuit worked to try and explain there is a startup procedure, which works fine and could be ignored.

I think one way to solve my problem, would be interrupt to turn off the side lights and replicate any indicator input to the amber rings. I just don't know how to do that really.

mrevolve:
I’m still learning what all the functions do, some of them I’m copying from other sketches, apologies for over-complicating some of the misunderstood entries in the code.

Then don’t skip learning steps :wink: Improve the readability of your code. It makes debugging easier possible.

mrevolve:
I think one way to solve my problem, would be interrupt to turn off the side lights and replicate any indicator input to the amber rings. I just don’t know how to do that really.

No interrupt needed, just don’t hang up the micro controller in a useless delay(). A micro can just do a single thing at a time, even if that’s waiting doing absolutely nothing in a delay(). Like I said, see Blink without delay (open example/Google it).

I’ve cleaned the code up, removed the delays and seen a few more opportunities to remove repeating myself.

Can you please advise, how when A3 is high, pin 7 and 9 are low (or using PWM go to 0)
and pin while the analog pin is high also have pin 2 and 4 high.
After the analog pin is high (remember there is an on off on off cycle with indicators of course), the cycle goes back to the loop ; whereas the white halo pins 3,5,7,9 are PWM.

I hope that’s clearer. With one side one, I could do the other.

I really appreciate the help!

int analogPin1 = A2;     // val
const int analogPin2 = A3;     // left indicator 5v input
const int analogPin3 = A4;     // right indicator 5v input
                       
int val = 0;           // Switch input value 
int inputVariable = 0;
int variable = 0;

int left = 0;
int right = 0;




void setup()
{


  
  Serial.begin(9600);              //  setup serial

  pinMode(A2, INPUT);   // Switch input, side light brightness and also max value setting for fade into variable function
  pinMode(A3, INPUT);   
  pinMode(A4, INPUT);

  digitalWrite (A2, LOW);  // internal pull-down resistor
  digitalWrite (A3, LOW);  // internal pull-down resistor
  digitalWrite (A4, LOW);  // internal pull-down resistor

  
for ( byte x = 2 ; x < 10 ; x++ ){

 pinMode(x , OUTPUT);

}

  digitalWrite (10, LOW);
  digitalWrite (11, LOW);

  //fade amber inside pair halo first

  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 4) {
    // sets the value (range from 0 to 255):
    analogWrite(4, fadeValue);
    analogWrite(8, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

delay(500); // delay required for visual 

// fade amber outer pair halo second

  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 4) {
    // sets the value (range from 0 to 255):
    analogWrite(2, fadeValue);
    analogWrite(6, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  delay(500);// delay required for visual 
    
    // fade all amber halo off
    
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 4) {
    // sets the value (range from 0 to 255):
    analogWrite(2, fadeValue);
    analogWrite(4, fadeValue);
    analogWrite(6, fadeValue);
    analogWrite(8, fadeValue);
    inputVariable = analogRead;
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }



delay(300); // delay required for visual 

//read what value the adjustable switch is (four positions)

  Serial.print("Setup serial function, value is ");
  Serial.print(val);             // debug value
  Serial.print(variable);

// fade on white halo upto the value of the variable switch

  for (int fadeValue = 0 ; fadeValue <= variable; fadeValue += 6) {
    // sets the value (range from 0 to 255):
    analogWrite(3, fadeValue);
    analogWrite(5, fadeValue);
    analogWrite(7, fadeValue);
    analogWrite(9, fadeValue);
        // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

void loop()
{
  
  val = analogRead(analogPin1);     // read the input pin
  Serial.print("Switch value is: ");
  Serial.println(val);             // debug value

// if statements, to change each white halo (3,5,7,9) to one of four brightness settings from variable input on A2

  if (val < 50) { analogWrite (3, 126);  }  
  else if (val < 600) { analogWrite (3, 126);  }
  else if (val < 800) { analogWrite (3, 192);  }
  else if (val > 800) { analogWrite (3, 255);  }
  
  if (val < 50) { analogWrite (5, 50);  }  
  else if (val < 600) { analogWrite (5, 126);  }
  else if (val < 800) { analogWrite (5, 192);  }
  else if (val > 800) { analogWrite (5, 255);  }

  if (val < 50) { analogWrite (7, 50);  }  
  else if (val < 600) { analogWrite (7, 126);  }
  else if (val < 800) { analogWrite (7, 192);  }
  else if (val > 800) { analogWrite (7, 255);  }

  if (val < 50) { analogWrite (9, 50);  }  
  else if (val < 600) { analogWrite (9, 126);  }
  else if (val < 800) { analogWrite (9, 192);  }
  else if (val > 800) { analogWrite (9, 255);  }

}

how when A3 is high, pin 7 and 9 are low

if(digitalRead(A3) == HIGH) {
     digitalWrite(7, LOW);
     digitalWrite(9, LOW);
}

Delta_G:

if(digitalRead(A3) == HIGH) {

digitalWrite(7, LOW);
    digitalWrite(9, LOW);
}

Thanks for your help so far! It’s working partially now which is closer than I’ve got before!

For some reason it’s flickering. Is there anyway I could put the brightness setting if function, inside another IF function to allow me to digitalWrite 7/9 pins low and not even consider the brightness while A3 is HIGH please.

void loop()
{
  
  val = analogRead(analogPin);     // read the input pin
  Serial.println(val);             // debug value

// brightness setting for halo pin 3
  if (val < 50) { analogWrite (3, 50);  }  
  else if (val < 600) { analogWrite (3, 126);  }
  else if (val < 800) { analogWrite (3, 192);  }
  else if (val > 800) { analogWrite (3, 255);  }

// brightness setting for halo pin 5  
  if (val < 50) { analogWrite (5, 50);  }  
  else if (val < 600) { analogWrite (5, 126);  }
  else if (val < 800) { analogWrite (5, 192);  }
  else if (val > 800) { analogWrite (5, 255);  }


// brightness setting for halo pin 7
  if (val < 50) { analogWrite (7, 50);  }  
  else if (val < 600) { analogWrite (7, 126);  }
  else if (val < 800) { analogWrite (7, 192);  }
  else if (val > 800) { analogWrite (7, 255);  }

// brightness setting for halo pin 9
  if (val < 50) { analogWrite (9, 50);  }  
  else if (val < 600) { analogWrite (9, 126);  }
  else if (val < 800) { analogWrite (9, 192);  }
  else if (val > 800) { analogWrite (9, 255);  }

if(digitalRead(A3) == HIGH) {
     digitalWrite(7, LOW);
     digitalWrite(9, LOW);
}
if(digitalRead(A4) == HIGH) {
     digitalWrite(3, LOW);
     digitalWrite(5, LOW);
}

}

I did try this, it managed to end up turning ALL the halo off and not just the focused pins :open_mouth:

if(digitalRead(A3) == LOW) {

// brightness setting for halo pin 3
  if (val < 50) { analogWrite (3, 50);  }  
  else if (val < 600) { analogWrite (3, 126);  }
  else if (val < 800) { analogWrite (3, 192);  }
  else if (val > 800) { analogWrite (3, 255);  }

}

Yea, put that part that analog writes to those pins in an else after the if we just added so that part only happens when the pin is LOW.

mrevolve:
I’ve cleaned the code up,

I see no change, let alone you did something with the advise…

Good luck! I’m out…

septillion:
I see no change, let alone you did something with the advise...

Good luck! I'm out....

I deleted some of the delays and replaced the pin modes. Also added explanations. Like you mentioned.

Apart from that, I can't magic knowledge I haven't got that's why I came asking for help. Perhaps you were expecting me to suddenly re-write it :frowning:

I was asking for help, still am please.

Anymore help please guys?

Have you addressed the advice from #9? Post the complete code as it is after you try that and then describe what happens with it.