Two buttons one led. Change behavior

The function should be like this:

1 latching toggle switch connected to pin 2 that changes the led behavior between puls or continuous. The second switch connected to pin3 is a "master switch" for the led to be on or off.

Works well with the toggle in low position and led start to pulse when the "master switch" is pressed.

But when I change the toggle switch (so its not low) the "master switch" function should be the same except that led should be continuous instad of pulse but that does not happen.

Hope my description is not too confusing!
Help needed

Thanks

int toggle;
int onoffswitch;

void setup(){

pinMode(2, INPUT_PULLUP);   // toggle switch pin "LED MODE" puls or continuous
pinMode(3, INPUT_PULLUP);   // led on/off pin
pinMode(11, OUTPUT);        // led pin

}
void loop()
{

  onoffswitch = digitalRead(3);

  if(onoffswitch == LOW) {
  
  toggle = digitalRead(2);

  digitalWrite(11, HIGH);

  if(toggle == LOW) {
  
  float in, out;
   
  for (in = 4.712; in < 10.995; in = in + 0.00700)
  {
    out = sin(in) * 127.5 + 127.5;
    analogWrite(11,out);
    delay(2);    
  }
}
}
}

Hi,
Can you please post a circuit diagram?
Why do you need two inputs to change the state of ONE LED?

Tom... :smiley: :+1: :coffee: :australia:

1 Like

consider

#undef MyHW
#ifdef MyHW
const byte PinPulse = A2;
const byte PinOnOff = A1;
#else
const byte PinPulse = 2;
const byte PinOnOff = 3;
#endif

const byte PinLed   = 11;

enum { Off = HIGH, On = LOW };

const unsigned long MsecPulseOn  = 100;
const unsigned long MsecPulseOff = 1000 - MsecPulseOn;
      unsigned long msecPeriod;
      unsigned long msecLst;

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    if (On == digitalRead (PinOnOff))  {
        if (On == digitalRead (PinPulse))  {
            if ( (msec - msecLst) > msecPeriod)  {
                msecLst = msec;

                if (On == digitalRead (PinLed))  {
                    digitalWrite (PinLed, Off);
                    msecPeriod = MsecPulseOff;
                }
                else {
                    digitalWrite (PinLed, On);
                    msecPeriod = MsecPulseOn;
                }
            }
        }
        else
            digitalWrite (PinLed, On);
    }
}

// -----------------------------------------------------------------------------
void setup (){
    pinMode (PinPulse, INPUT_PULLUP);
    pinMode (PinOnOff, INPUT_PULLUP);

    pinMode (PinLed,   OUTPUT);
    digitalWrite (PinLed,   Off);

}
1 Like

Hi,
Thanks.
Have you looked at switch.. case function.
You can have 3 cases,, master ON and mode ON, master ON and Mode OFF, master OFF.

Tom.. :smiley: :+1: :coffee: :australia:

1 Like

for (in = 1.5*Pi; in < 3.5*Pi; in = in + (Pi/450))

That loop is about 900 iterations long. At 2 milliseconds delay that will be about 1.8 seconds total. During that time your 'on/off' switch won't be active.

You could check the on/off switch each time through the loop so the delay would only be 2 milliseconds:

void loop()
{
  // Calculate the pulse 'out' value
  for (in = 4.712; in < 10.995; in = in + 0.00700)
  {
    out = sin(in) * 127.5 + 127.5;

    // Check the switches
    if (digitalRead(3) == LOW)
    {
      // on/off switch is off
      digitlalWrite(11, LOW); // Off
    }
    else
    {
      // on/off switch is on
      if (digitalRead(2) == LOW)
      {
        // pulse/solid switch is set to solid
        digitalWrite(11, HIGH);
      }
      else
      {
        // pulse/solid switch is set to pulse
        analogWrite(11, out);
        delay(2); // Pulse timing (very roughly, seconds per pulse)
      }
    }
  }
}

That's great. I just added

float in, out;

It works perfect now. Your code was exactly what I needed. Thanks for your help!

Full code

int toggle;
int onoffswitch;

void setup(){

pinMode(2, INPUT_PULLUP);   // toggle switch pin "LED MODE" puls or continuous
pinMode(3, INPUT_PULLUP);   // led on/off pin
pinMode(11, OUTPUT);        // led pin

}
void loop()
{
  // Calculate the pulse 'out' value
  float in, out;
  for (in = 4.712; in < 10.995; in = in + 0.00700)
  {
    out = sin(in) * 127.5 + 127.5;

    // Check the switches
    if (digitalRead(3) == HIGH)
    {
      // on/off switch is off
      digitalWrite(11, LOW); // Off
    }
    else
    {
      // on/off switch is on
      if (digitalRead(2) == LOW)
      {
        // pulse/solid switch is set to solid
        digitalWrite(11, HIGH);
      }
      else
      {
        // pulse/solid switch is set to pulse
        analogWrite(11, out);
        delay(2); // Pulse timing (very roughly, seconds per pulse)
      }
    }
  }
}

Thanks for the links.. Could be useful for another project but now it works great with the code John posted.

Just one thing I just noticed. With the old code the pulse led always started from 0 brightness, and that was very nice. Now it seems more random. How do I get that back?

Set 'in' to 4.712 when the LED is set to LOW (off) or HIGH (on solid). Then when you switch to 'on pulsing' the loop will be at the beginning and start from there.

1 Like

Thanks but I don't understand which part of the code to change?

  // Calculate the pulse 'out' value
  float in, out;
  for (in = 4.712; in < 10.995; in = in + 0.00700)
  {
    out = sin(in) * 127.5 + 127.5;

I thought 'in' already was 4.712

Only the first time through the loop. The second time it has had 0.00700 added to it. Setting it back to 4.172 will start the loop over at black: sin(1.5 Pi) * 127.5 - 127.5

Ok thanks. But I don't understand how to set it back to 4.172?

in = 4.172;

I'm going crazy here... We have 4.712 and 4.172

So this is the code we are talking about.

  // Calculate the pulse 'out' value
  float in, out;
  for (in = 4.172; in < 10.995; in = in + 0.00700)
  {
    out = sin(in) * 127.5 + 127.5;

Replace 4.712 with 4.172 does not help...

The "4.712" was a typo. 4.172 is the correct value (1.5 * Pi).

I gave up changing numbers and moved down

// Calculate the pulse 'out' value

code just before the analogWrite command and then it worked. Now Led pulse always start from 0 brightness on every new press

void setup(){

pinMode(2, INPUT_PULLUP);   // toggle switch pin. puls/solid
pinMode(3, INPUT_PULLUP);   // led on/off pin
pinMode(11, OUTPUT);        // led pin

}
void loop()
{

    // Check the switches
    if (digitalRead(3) == HIGH)
    {
      // on/off switch is off
      digitalWrite(11, LOW); // Off
    }
    else
    {
      // on/off switch is on
      if (digitalRead(2) == LOW)
      {
        // pulse/solid switch is set to solid
        digitalWrite(11, HIGH);
      }
      else
      {

          // Calculate the pulse 'out' value
      float in, out;
      for (in = 4.712; in < 10.995; in = in + 0.00700)
      {
      out = sin(in) * 127.5 + 127.5;

        // pulse/solid switch is set to pulse
        analogWrite(11, out);
        delay(2); // Pulse timing (very roughly, seconds per pulse)
        
      }
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.