Programing for voltage-triggered LED

Hi. I am trying to write code to make an LED fade in and out when a voltage is detected in pin A1, and that same LED fade in and stay on when voltage is detected in pin A3, and fade out when another touch of voltage is detected in pin A3.

Here’s what I have so far…

//defining variables
//led is light; vs supplies voltage; vr receives and reads voltage
const int led = 10;
const int vr = A1;
const int vs = 5;
const int vr2 = A3;
int voltage = 0;
int voltage2 = 0;
boolean on_off = false;

void setup() 
{
  //led is output to power light; vr must be input to use analogRead; vs must be output to supply voltage to vr
  pinMode(led, OUTPUT);
  pinMode(vr, INPUT);
  pinMode(vs, OUTPUT);
  pinMode(vr2, INPUT);
}

void loop() 
{
    voltage = analogRead(A1);
    
    //950-1029 ensures only a voltage ~350mV under 5V triggers LED 
    if(voltage > 950 && voltage <= 1029)
    {
      for(int brightness = 0; brightness <255; brightness +=20)
      {
        analogWrite(led, brightness);
        delay(5);
      }
      
      for(int brightness = 255; brightness > 0; brightness -=4)
      {
        analogWrite(led, brightness);
        delay(50);
      }
    }
    
    voltage2 = analogRead(A3);
    
    if(voltage2 > 950 && voltage2 <= 1029)
    {
      if(on_off == false)
      {
        digitalWrite(led, HIGH);
        on_off == true;
        
        delay(2000);
      }
      else
      {
        digitalWrite(led, LOW);
        on_off == false;
      }
    }
    
}

I’ve tried controling each action as a seperate LED successfully, but have not been able to get one LED to perform all functions as desired. With the above version of the code, the A1 fade-in and fade-out works, the A3 fade-in and stay on works, but the A3 fade-out doesn’t work.

Any ideas?

analogRead(…) always returns a value from 0 through 1023, so this:

if(voltage > 950 && voltage <= 1029)

is identical to

if(voltage > 950)

and

if(voltage2 > 950 && voltage2 <= 1029)

is identical to

if(voltage2 > 950)

I see a fade-in and fade-out for A1, but I don’t see any for A3. Did I miss it?

delay(2000) is going to get you into trouble. During the two seconds that this executes, NOTHING else can happen. Your other delays are shorter and less problematic but should be eliminated as well. Please see the Blink Without Delay example, learn from it, embrace it. Also see

and

http://www.gammon.com.au/forum/?id=11411

and

http://www.gammon.com.au/forum/?id=11425

I have the digitalWrite for the second part just to simplify things until I can get the LED to go on with one input and off with another. The delay was a mistake, thanks for pointing that out :)

That first link is very helpful and should be enough to get me through. I'm going to try to use Switch statements rather than tons of nested if-else statements, too. Thanks!

I applaud your use of state machines and your elimination of delay(…).

If you continue to have issues, I look forward to seeing a copy of your latest code. Good Luck!

Our code ended up being much simpler than before. As of now, we need a continuous connection for the LED to switch off (its default state is on), which is done by connecting the end of the black wire (other end is in 5V) to the read in pin 2. Rather than having to hold this connection, it would be nice to be able to simply touch it once, and have the LED stay in its position.

Our code:

int voltage = 0;
int fade = 9;
int led = 13;
int pin2 = 2;
long previousMillis = 0;
long interval = 1000;
int analog = 2;

void setup()
{
   pinMode(fade, OUTPUT);
   pinMode(led, OUTPUT);
   pinMode(analog, INPUT);
   pinMode(pin2, INPUT);
}

void loop()
{
  whoopie();
  button();
}

void whoopie()
{
   voltage = analogRead(analog);
    
    if(voltage > 950 && voltage <= 1029)
    {
      for(int brightness = 0; brightness <255; brightness +=20)
      {
        analogWrite(fade, brightness);
        delay(5);
      }
      
      for(int brightness = 255; brightness > 0; brightness -=4)
      {
        analogWrite(fade, brightness);
        delay(50);
      }
    }
  }

void button()
{
    unsigned long currentMillis = millis();
    int sensorVal = digitalRead(2);
 
    if(currentMillis - previousMillis > interval)
    {
       previousMillis = currentMillis;  
       
       if(sensorVal == HIGH) 
       {
         digitalWrite(led, LOW);
       }
       else 
       {
         digitalWrite(led, HIGH);
       }
    }
}

Set-up:

(with 13 triggered to OFF)

Our goal is to have an input trigger a fade in one LED, and a separate input to trigger another LED to fade into full brightness and stay this way until triggered again, at which point it will fade out and stay off. We have no issue with the singular LED on pin 9 (green one) which, when triggered, fades bright quickly, and out slowly, as we want.

We know that LED 13 is not a PWM pin (and therefore cannot fade); we’ll add the code for fade once we first figure out the issue of having the LED stay in either the on/off position after one single trigger, rather than a continuous one.

plausiblephysicist: it would be nice to be able to simply touch it once, and have the LED stay in its position.

Where you current read the input pin directly, instead read the HIGH/LOW value from a global variable. Add some code elsewhere that reads the actual input, does whatever debouncing may be required, detects when the input changes state and toggles the state of the global variable each time you make the connection. The state change detection example shows how to do this.

Sweet! Thanks for all the help; we finally got the read to work as desired. All that’s left is to instead have our digitalWrite as an analogWrite implementing the fade function for the button-activated LED.

The code:

int voltage = 0;
int fade = 9;
int led = 13;
int buttonPin = 2;
long previousMillis = 0;
long interval = 1000;
int analog = 2;
int buttonPushCounter = 0; 
int buttonState = 0;       
int lastButtonState = 0;

void setup()
{
   pinMode(fade, OUTPUT);
   pinMode(led, OUTPUT);
   pinMode(analog, INPUT);
   pinMode(buttonState, INPUT);
}

void loop()
{
  whoopie();
  button();
}

void whoopie()
{
   voltage = analogRead(analog);
    
    if(voltage > 950 && voltage <= 1029)
    {
      for(int brightness = 0; brightness <255; brightness +=20)
      {
        analogWrite(fade, brightness);
        delay(5);
      }
      
      for(int brightness = 255; brightness > 0; brightness -=4)
      {
        analogWrite(fade, brightness);
        delay(50);
      }
    }
  }

void button()
{
    buttonState = digitalRead(buttonPin);
    if (buttonState != lastButtonState) 
    {
    
      if (buttonState == HIGH) 
      {
        buttonPushCounter++;
      }
    
      delay(50);
    }
 
  lastButtonState = buttonState;

  if (buttonPushCounter % 2 == 0) 
  {
    digitalWrite(led, HIGH);
  } 
  else 
  {
    digitalWrite(led, LOW);
  }
}

EDIT:

Going back through the code trying to change blink to fade has proved more difficult than we thought, as buttonState is defined in terms of HIGH/LOW. We’ll try defining it in terms of brightness and post an update of how that goes, but if you have a better idea, please let us know. Thanks ~ plausiblephysicist

We’ve decided to not use the fade in the void button () section. Now I’ve added code for 5 LEDs. I know that only one can be triggered at a time. Now an issue is with the void button () section, that, when the arduino is powered LEDs fade0, fade1, and fade2 are initally on HIGH; fade3 and fade4 are initially LOW; and triggering the respective buttonPins doesn’t switch the mode of its respective single LED. Sometimes the trigger does no visible action, other times one trigger turns off two LEDs, or LEDs sputter and return to their previous setting.

Here’s the code

int voltage0 = 0;
int voltage1 = 0;
int voltage2 = 0;
int voltage3 = 0;
int voltage4 = 0;
int fade0 = 11;
int fade1 = 10;
int fade2 = 9;
int fade3 = 6;
int fade4 = 5;
int buttonPin0 = 4;
int buttonPin1 = 3;
int buttonPin2 = 2;
int buttonPin3 = 1;
int buttonPin4 = 0;
int analog0 = 0;
int analog1 = 1;
int analog2 = 2;
int analog3 = 3;
int analog4 = 4;
int buttonPushCounter0 = 0;
int buttonPushCounter1 = 0; 
int buttonPushCounter2 = 0; 
int buttonPushCounter3 = 0; 
int buttonPushCounter4 = 0; 
int buttonState0 = 0;
int buttonState1 = 0; 
int buttonState2 = 0; 
int buttonState3 = 0; 
int buttonState4 = 0; 
int lastButtonState0 = 0;
int lastButtonState1 = 0;
int lastButtonState2 = 0;
int lastButtonState3 = 0;
int lastButtonState4 = 0;

void setup()
{
   pinMode(fade0, OUTPUT);
   pinMode(fade1, OUTPUT);
   pinMode(fade2, OUTPUT);
   pinMode(fade3, OUTPUT);
   pinMode(fade4, OUTPUT);
  
   pinMode(analog0, INPUT);
   pinMode(analog1, INPUT);
   pinMode(analog2, INPUT);
   pinMode(analog3, INPUT);
   pinMode(analog4, INPUT);
   
   pinMode(buttonPin0, INPUT);
   pinMode(buttonPin1, INPUT);
   pinMode(buttonPin2, INPUT);
   pinMode(buttonPin3, INPUT);
   pinMode(buttonPin4, INPUT);
}

void loop()
{
  whoopie();
  button();
}

void whoopie()
{
   voltage0 = analogRead(analog0);
    
    if(voltage0 > 950 && voltage0 <= 1029)
    {
      for(int brightness = 0; brightness <255; brightness +=20)
      {
        analogWrite(fade0, brightness);
        delay(5);
      }
      
      for(int brightness = 255; brightness > 0; brightness -=4)
      {
        analogWrite(fade0, brightness);
        delay(50);    
      }
    }
    voltage1 = analogRead(analog1);
    
    if(voltage1 > 950 && voltage1 <= 1029)
    {
      for(int brightness = 0; brightness <255; brightness +=20)
      {
        analogWrite(fade1, brightness);
        delay(5);
      }
      
      for(int brightness = 255; brightness > 0; brightness -=4)
      {
        analogWrite(fade1, brightness);
        delay(50);    
      }
    }
     voltage3 = analogRead(analog3);
    
    if(voltage3 > 950 && voltage3 <= 1029)
    {
      for(int brightness = 0; brightness <255; brightness +=20)
      {
        analogWrite(fade3, brightness);
        delay(5);
      }
      
      for(int brightness = 255; brightness > 0; brightness -=4)
      {
        analogWrite(fade3, brightness);
        delay(50);    
      }
    }
    voltage2 = analogRead(analog2);
    
    if(voltage2 > 950 && voltage2 <= 1029)
    {
      for(int brightness = 0; brightness <255; brightness +=20)
      {
        analogWrite(fade2, brightness);
        delay(5);
      }
      
      for(int brightness = 255; brightness > 0; brightness -=4)
      {
        analogWrite(fade2, brightness);
        delay(50);    
      }
    }
    voltage4 = analogRead(analog4);
    
    if(voltage4 > 950 && voltage4 <= 1029)
    {
      for(int brightness = 0; brightness <255; brightness +=20)
      {
        analogWrite(fade4, brightness);
        delay(5);
      }
      
      for(int brightness = 255; brightness > 0; brightness -=4)
      {
        analogWrite(fade4, brightness);
        delay(50);    
      }
    }
  }

void button()
{
  
   buttonState0 = digitalRead(buttonPin0);
   buttonState1 = digitalRead(buttonPin1);
   buttonState2 = digitalRead(buttonPin2);
   buttonState3 = digitalRead(buttonPin3);  
   buttonState4 = digitalRead(buttonPin4); 
    
    
    
    if (buttonState0 != lastButtonState0) 
    {
      if (buttonState0 == HIGH) 
      {
        buttonPushCounter0++;
      }
    
      delay(50);
    }
 
  lastButtonState0 = buttonState0;

  if (buttonPushCounter0 % 2 == 0) 
  {
    digitalWrite(fade0, HIGH);
  } 
  else 
  {
    digitalWrite(fade0, LOW);
  }
   
    if (buttonState1 != lastButtonState1) 
    {
      if (buttonState1 == HIGH) 
      {
        buttonPushCounter1++;
      }
    
      delay(50);
    }
 
  lastButtonState1 = buttonState1;

  if (buttonPushCounter1 % 2 == 0) 
  {
    digitalWrite(fade1, HIGH);
  } 
  else 
  {
    digitalWrite(fade1, LOW);
  }
  
    if (buttonState2 != lastButtonState2) 
    {
      if (buttonState2 == HIGH) 
      {
        buttonPushCounter2++;
      }
    
      delay(50);
    }
 
  lastButtonState2 = buttonState2;

  if (buttonPushCounter2 % 2 == 0) 
  {
    digitalWrite(fade2, HIGH);
  } 
  else 
  {
    digitalWrite(fade2, LOW);
  }
 
    if (buttonState3 != lastButtonState3) 
    {
      if (buttonState3 == HIGH) 
      {
        buttonPushCounter3++;
      }
    
      delay(50);
    }
 
  lastButtonState3 = buttonState3;

  if (buttonPushCounter3 % 2 == 0) 
  {
    digitalWrite(fade3, HIGH);
  } 
  else 
  {
    digitalWrite(fade3, LOW);
  }
  
    if (buttonState4 != lastButtonState4) 
    {
      if (buttonState4 == HIGH) 
      {
        buttonPushCounter4++;
      }
    
      delay(50);
    }
 
  lastButtonState4 = buttonState4;

  if (buttonPushCounter4 % 2 == 0) 
  {
    digitalWrite(fade4, HIGH);
  } 
  else 
  {
    digitalWrite(fade4, LOW);
  }

}

Here’s my set-up: