Project LED with Dip Switch

Hello!

I'm doing an Arduino study and would like to see a longer LED activation time with the Dips Switches. But first of all I want to thank you @noiasca for having previously helped another participant in the forum.

Link: Using a 4-pin dip switch to set value - #18 by johnerrington

At this moment I made some changes to the code that in the Serial Monitor is showing the time correctly.

As the image above is the connection diagram and below in the Serial Monitor what it shows.
image

The problem with this is that the LED always stays on and it doesn't result in the timing actions I selected on the Dip Switch.

Anyone with another view of what is incorrect?

Here is the code. :slight_smile:

int pinLED = 13;

byte LED;
byte oldStatusLED;
boolean statusLED_On = true;
byte status;

// declare a structure of pin and interval
struct DipSwitch
{
  const uint8_t pin;        // the gpio
  const uint16_t interval;  // the interval in seconds
};

// make an array of dipswitches and initialize the values
DipSwitch dipSwitch[]
{
  {8, 2}, // calculate (2 * factor) = 20000
  {12, 4}, // calculate (4 * factor) = 40000
};

// calculate the setted interval
uint32_t getInterval()
{
  const uint16_t factor = 10000; // millis for seconds
  uint32_t interval = 0;

  for (auto &i : dipSwitch)
  {
    LED = digitalRead(i.pin);
    if (digitalRead(i.pin) == LOW) interval += i.interval;
      if (LED = !oldStatusLED)
      {
        if (LED)
        {
          statusLED_On = !statusLED_On;
          digitalWrite(pinLED, statusLED_On);
        }
      }
  }
  return interval * factor;
}

void setup()
{
  Serial.begin(115200);
  for (auto &i : dipSwitch)
  {
    pinMode(i.pin, INPUT_PULLUP);
    pinMode(pinLED, OUTPUT);
  }
}

void loop()
{
  getInterval();
  Serial.println(getInterval());
  //delay(1000); // dirty delay just to make the output visible
}

Try == instead of =.

Test for equality, not assignment...

Tough error to spot. Go to the preferences in the IDE and crank up the compiler verbosity and warnings.

This is a common mistake and therefor the compiler can be asked to clue you so you look and make sure you knew what you were doing.

a7

1 Like

BTW if that was from a wokwi simulation, it can be effective to share it using the Share button.

If it wasn't, let me say that the wokwi simulator is excellent.

a7

Curious why SW1 has only one connection ?

Tested, no effect.

I didn't find something with only two copies in the website's library.

don't see where you turn the led off..
played with it a bit..

You code simmed..

blinking led in main loop using millis timer based on interval calculated..

hope this helps..

good luck.. ~q

Thanks for playing around with the code.

Now I will make adjustments to the code that the LED stays on within the time.

LOL. I use a different verb for what you did.

a7

Sorry, I'm celibate..

truthfully..~q

Looks to me like each switch has 2 connections:
image

  • Yellow to SW1;
  • Brown to SW2.

Sorry !
The reason Fritzies (and old people) should be banned!
I didn’t see the yellow on grey wire ! at all.

Real schematics show detail, not try to hide it !

3 Likes

@qubits-us you said the code in the wokwi was the OP's. That is very kind, but inaccurate.

You actually returned the function that someone elsewhere had handed her to its original single purpose, to read some DIP switches.

In doing, you removed also the obvious error and the entirety of mistaken attempt to add to it the additional task of blinking an LED that error was in.

You took the loop() back to where it probably was when handed to the OP, a simple test of what had been that simple DIP switch function.

And you wrote there for her the only part that was being added, that is trying to use the value derived from the DIP switches to blink an LED. Which naturally turned out to work as opposed to her incorrect at least and demonstrably buggy on a peep-hole basis (==, =) attempt.

You code well enough so that you might have been asleep through all that.

I call that work, not play.

The code now has very little to nothing that the OP wrote. I am as sure she will study what you have done and learn from it as I am that she has already turned up the compiler warnings in the IDE, and so will benefit from the time you spent "playing".

a7

Well, I guess we just have different opinions..
anything less than 20k lines is play time for me, sorry..
I do try to play nice with others..
Don't feel like I'm causing any harm and I try not too..
If I bother you personally sorry, just block me, don't be a bully..

~q

2 Likes

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