1 LED, multiple inputs

I am a newbie and I am sorry if this may be a duplicate post, but I could not find one for what I need. Also, sorry if I get long winded. I like to be detailed in my explinations.
My primary goal is shrinkify this circuit and integrate this circuit into my car's brake light system using home made LED fixtures.

I am trying to get 1 high power analog output to be controlled by 2 different digital inputs (1 switch, 1 button) but have different states depending the state of the digital inputs. Eventually the switch and button will be replaced by my car's 12VDC outputs for driving lights and brake light wires.

For future modifications, I would like the LED to strobe 2 or 3 times (delay(50); on and off) before staying on solid until the button is released.

A slide switch is connected to pin 12
A push button is connected to pin 7
The LED's are connected to pin 9 via a MOSFET Transistor (The LED's (196 total) are rather powerful and needs more power than the Arduino can supply)

I want the LED to turn on dim (50) when the slide switch is turned on
and/or
I want the LED to turn on bright (255) when the button is pressed

The LED will go bright (255) regardless of the state of the switch. The switch will simply control weather the LED is dimmed (50) or off (0) when the button is not pressed.

So far, have been able to make the LED start dim (50) and go bright (255) when the button is pressed, but I cannot figure out how to incorporate the slide switch at the same time.

Here's what I have so far that works:

#define LED 9               // The Brake Light LED is connected to pin 9
const int BUTTON = 7;       // A test button is connected to pin 7
const int SWITCH = 12;      // A test switch is connected to pin 12

int val = 0;
int OFF = 0;
int DIM = 50;               // Appropriate brightness when dimmed for this LED fixture
int BRIGHT = 255;

void setup() {
pinMode(13, OUTPUT);        // This LED isn't used for this setup, however it is installed
pinMode(LED, OUTPUT);       // Pin 9 is an output
pinMode(BUTTON, INPUT);     // The button connected to pin 7 is an input
pinMode(SWITCH, INPUT);     // The switch connected to pin 12 is an input
}

void loop()
{digitalWrite(13, LOW);     // Turn off the LED on pin 13
val = digitalRead(BUTTON);  // Read the ON/OFF state of the button
if (val == HIGH)            // If the button is being pressed
{analogWrite(LED, BRIGHT);  // turn the brake light on full power
delay(2000);}               // Wait 2 second before going back to dim (prevents the "strobing" effect)
else                        // otherwise
{analogWrite(LED, DIM);}}   // run the brake light in a dimmed state

first of all you need to debounce your buttons: http://arduino.cc/it/Tutorial/Debounce . Another thing; if you want this to truly work, do not use delay(); to blink the led as it stops the whole sketch(and thus the button press detection). see void loop for ONE way of doing the two button thing.

sorry if this is handing it to you on a silver platter, i like the practice :stuck_out_tongue:

// constants won't change. They're used here to 
// set pin numbers:
const int BUTTONpin = 12;     // the number of the pushbutton pin
const int SWITCHpin = 7;     // the number of the pushSWITCH pin

// Variables will change:
int BUTTONstate;             // the current BUTTONreading from the input pin
int lastBUTTONstate = LOW;   // the previous BUTTONreading from the input pin
long lastBUTTONDebounceTime = 0;  // the last time the output pin was toggled

int SWITCHstate;             // the current SWITCHreading from the input pin
int lastSWITCHstate = LOW;   // the previous SWITCHreading from the input pin
long lastSWITCHDebounceTime = 0;  // the last time the output pin was toggled

long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(BUTTONpin, INPUT);
  pinMode(SWITCHpin, INPUT);
}

void loop()
{
  getSWITCHreading();
  getBUTTONreading();
  
  //SWITCHstate is the current state of the SWITCH
  //BUTTONstate is the current state of the BUTTON
  
  if(SWITCHstate==1) //if SWITCH is pressed
  {
    if(BUTTONstate == 1) //if BUTTON is pressed
    {
      //do this
    }
    else //if BUTTON not pressed
    {
      //do that
    }
  }
  else //SWITCHstate=LOW
  {
    if(BUTTONstate == 1) //if BUTTON is pressed
    {
      //do this
    }
    else //if BUTTON not pressed
    {
      //do that
    }
  }
}

void getSWITCHreading()
{
  // read the state of the switch into a local variable:
  int SWITCHreading = digitalRead(SWITCHpin);
  
  // check to see if you just pressed the SWITCH 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (SWITCHreading != lastSWITCHstate) {
    // reset the debouncing timer
    lastSWITCHDebounceTime = millis();
  } 
  
  if ((millis() - lastSWITCHDebounceTime) > debounceDelay) {
    // whatever the SWITCHreading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    SWITCHstate = SWITCHreading;
  }
  
  // save the SWITCHreading.  Next time through the loop,
  // it'll be the lastSWITCHstate:
  lastSWITCHstate = SWITCHreading;
}

void getBUTTONreading()
{
  // read the state of the switch into a local variable:
  int BUTTONreading = digitalRead(BUTTONpin);
  

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (BUTTONreading != lastBUTTONstate) {
    // reset the debouncing timer
    lastBUTTONDebounceTime = millis();
  } 
  
  if ((millis() - lastBUTTONDebounceTime) > debounceDelay) {
    // whatever the BUTTONreading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    BUTTONstate = BUTTONreading;
  }

  // save the BUTTONreading.  Next time through the loop,
  // it'll be the lastBUTTONstate:
  lastBUTTONstate = BUTTONreading;
  
}

BRILLIANT!!!
It works wonderfully!
I guess my problem was I didn't know how to use multiple "If, Else" statements.
Thank you!
You have helped a newbie expand his mind!
Also, I don't mind silver platters!
This is the final result:

// constants won't change. They're used here to 
// set pin numbers:
const int BUTTONpin = 12;         // the number of the pushbutton pin
const int SWITCHpin = 7;          // the number of the pushSWITCH pin

// Variables will change:
int BUTTONstate;                  // the current BUTTONreading from the input pin
int lastBUTTONstate = LOW;        // the previous BUTTONreading from the input pin
long lastBUTTONDebounceTime = 0;  // the last time the output pin was toggled

int SWITCHstate;                  // the current SWITCHreading from the input pin
int lastSWITCHstate = LOW;        // the previous SWITCHreading from the input pin
long lastSWITCHDebounceTime = 0;  // the last time the output pin was toggled

long debounceDelay = 100;          // the debounce time; increase if the output flickers

void setup() {
  pinMode(BUTTONpin, INPUT);
  pinMode(SWITCHpin, INPUT);
}

void loop()
{
  getSWITCHreading();
  getBUTTONreading();
  
  //SWITCHstate is the current state of the SWITCH
  //BUTTONstate is the current state of the BUTTON
  
  if(SWITCHstate==1)     //if SWITCH is pressed
  {
    if(BUTTONstate == 1) //if BUTTON is pressed
    {
      analogWrite(9, 255);
    }
    else //if BUTTON not pressed
    {
      analogWrite(9, 50);
    }
  }
  else //SWITCHstate=LOW
  {
    if(BUTTONstate == 1) //if BUTTON is pressed
    {
      analogWrite(9, 255);
    }
    else //if BUTTON not pressed
    {
      analogWrite(9, 0);
    }
  }
}

void getSWITCHreading()
{
  // read the state of the switch into a local variable:
  int SWITCHreading = digitalRead(SWITCHpin);
  
  // check to see if you just pressed the SWITCH 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (SWITCHreading != lastSWITCHstate) {
    // reset the debouncing timer
    lastSWITCHDebounceTime = millis();
  } 
  
  if ((millis() - lastSWITCHDebounceTime) > debounceDelay) {
    // whatever the SWITCHreading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    SWITCHstate = SWITCHreading;
  }
  
  // save the SWITCHreading.  Next time through the loop,
  // it'll be the lastSWITCHstate:
  lastSWITCHstate = SWITCHreading;
}

void getBUTTONreading()
{
  // read the state of the switch into a local variable:
  int BUTTONreading = digitalRead(BUTTONpin);
  

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (BUTTONreading != lastBUTTONstate) {
    // reset the debouncing timer
    lastBUTTONDebounceTime = millis();
  } 
  
  if ((millis() - lastBUTTONDebounceTime) > debounceDelay) {
    // whatever the BUTTONreading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    BUTTONstate = BUTTONreading;
  }

  // save the BUTTONreading.  Next time through the loop,
  // it'll be the lastBUTTONstate:
  lastBUTTONstate = BUTTONreading;
  
}

In continuation to my original post, how can I get the LED to strobe on and off 3 times before going to a solid state then wait for the button to be released before going back to the off state?

Just to see if I get this right: you want to modify the brake light of your car in a way that it starts to blink before it goes solid? Here in Germany this would be most probably illegal and for good reason. In case you would be involved in an accident that might be correlated to your invention you would be in deep shit. Maybe this is different in your country but you should consider this.

Anyway what you are creating will most likely not work reliably in a car. It may work initially but eventually it will start to fail in strange ways. The reason being that the power supply inside a car is "very dirty". You may want to read this http://www.littelfuse.com/data/en/Application_Notes/an9312.pdf before you actually put your contraption into a car.

Your concern is valid and noted. To the best of my knowledge, it is not illegal to have a break light that flashes a couple times before going solid. I have seen several cars in which the owners have modified the 3rd eye to flash continuously using a turn signal relay when the break is pressed. This is also not against the law. I had one police officer tell me that it would be good because it would get the attention of the driver behind you better than with regular solid on break lights. As long as the strobing isn't so excessive to the point that I could be mistaken for a police vehicle, it isn't against the law. This is why I want to have the lights flash no more than 3 total times and no longer than ¼ second total time before going solid. In addition, I still plan to have 2 standard brake lights functioning normally in the instance that my circuits fail.

As for the dirty power issue, I have already considered fixing this problem using electrical filters and regulators. The LED's are designed to run on a car's 12VDC electrical system and suck up a bit of amperage just due to the shear quantity of LED's I'm using, which is also why I am using MOSFET transistors. The ATtiny85 chip will run comfortably on 5VDC. I have already used a 5V linear regulator with a 10µF capacitor on input & ground, another 10µF capacitor on output & ground, and a .1µF ceramic capacitor also on output & ground. This gives me a very stable 5VDC output with a variance of no more than .001VDC, even with an input voltage variance of 9 - 16VDC. When put on an oscilloscope, this showed to be a very clean, stable, and quiet power source even with the engine running.

So, with that said, I am good with the wiring and circuit building side of it, but I am a newbie to the script writing side of things. Back to my original question. Using my "Final Result" script as a base which works fine, can someone help me figure out how to make the LED's go HIGH two or three times within ¼ second (250 ms) before going solid, then wait for the "Button" to go low before going to either the dimmed of off state controlled by the switch? Any attempts I have made so far have resulted in continuous strobing when the button is pressed. Any help would be appreciated.

I am aware that police officers are by definition an authority. However if they really properly can judge the effect of flashing lights on reaction time is another matter. I doubt this.

Actually what will happen is that your flashing lights will draw the attention of drivers in other lanes significantly more. Thus increasing the risk that they will not notice the lights of drivers ahead of them because you are drawing their attention. This is one of the side effects of "peripheral vision". I doubt that his police officer actually is aware of things like this.

If flashing lights like this would have any reasonable benefit I am pretty much sure they would be already required by law.

Then lets say that I'm not putting these on my car, but instead, using it for Halloween special effects when someone steps on a switch walking into the room.
How could I make the LED strobe 2 times then go on solid until the switch is released?

again, this is one way of doing it.

put a if statement in your loop that has a boolean(lightEffects) in it. the boolean is changed by the button press code i gave earlier.

boolean lightEffects = true;
boolean twa = true;
long lastTime;
int flashTime = 500;

if(lightEffects)
  {
    if(lastTime ==0)
    {
      Serial.println("ON");
      lastTime = millis();
    }
    else if(millis() - lastTime > flashTime) 
    {
      Serial.println("OFF"); 
      if(twa)
      {
      lastTime = millis()+flashTime;
      twa= false;
      }
    }
    else if(millis() - lastTime < flashTime)
    {
      if(!twa)
      {
      lastTime = millis();
      }
      Serial.println("ON"); 
    }   
  }

Why didn't I see this in your 1st post. Sorry.
Thanks again!

this wasnt in my first post... before where you used analog write to turn them on/off set the lightEffect boolean on/off then the flashing code will only run when lightEffects is true. it doesnt use delays too :slight_smile: