Debouncing on setup

Hi all,

Apologies if this is considered a stupid question, its my first time playing with the arduino.

So currently I have it setup as attached.

The mosfet is actually a mosfet board, but it makes little difference.

So it is working perfectly, apart from the fact that on start up, the led strip flashes on and off a few times regardless of the switch position.

My code is as follows:

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
  
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 50;   // the debounce time, increase if the output flickers
  
void setup()
{
  pinMode(inPin, INPUT_PULLUP);  //enable the internal pullup resistor 
  pinMode(outPin, OUTPUT);
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  Serial.println("Starting");
  digitalWrite(outPin, 0);  
}
  
void loop()
{
  int switchstate;
  reading = digitalRead(inPin);

  if ((reading != previous) && ((millis() - time) > debounce)) {
     previous = reading;
     digitalWrite(outPin, reading);
     Serial.println(reading);
     time = millis();
  }  
}

Can anyone recommend anything to help prevent this? I added the digitalWrite(outPin, 0); in the setup in the hope it may, but no luck.

Thanks

Switch_bb.jpg

paulharman:
Hi all,

Apologies if this is considered a stupid question, its my first time playing with the arduino.

So currently I have it setup as attached.

The mosfet is actually a mosfet board, but it makes little difference.

So it is working perfectly, apart from the fact that on start up, the led strip flashes on and off a few times regardless of the switch position.

My code is as follows:

int inPin = 2;         // the number of the input pin

int outPin = 13;       // the number of the output pin

int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
 
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 50;   // the debounce time, increase if the output flickers
 
void setup()
{
 pinMode(inPin, INPUT_PULLUP);  //enable the internal pullup resistor
 pinMode(outPin, OUTPUT);
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 Serial.println("Starting");
 digitalWrite(outPin, 0);  
}
 
void loop()
{
 int switchstate;
 reading = digitalRead(inPin);

if ((reading != previous) && ((millis() - time) > debounce)) {
    previous = reading;
    digitalWrite(outPin, reading);
    Serial.println(reading);
    time = millis();
 }  
}




Can anyone recommend anything to help prevent this? I added the digitalWrite(outPin, 0); in the setup in the hope it may, but no luck.

Thanks

Pin 13 is the one connected to the LED (which blinks at bootup). Therefore, you are also sending those "blinks" to the MOSFET. Use a different pin to drive it.

And, hopefully your drawing is wrong, because it looks like you have the MOSFET drain connected to Pin 13... which is obviously wrong.

Sorry I didn't explain it very well.

Ignore the mosfet on the diagram, it is actually a prebuilt mosfet relay board. This is driving an RGB strip controller. GND on the board is connected to ground on my UNO, and input 1 on the board is connected to pin 13 on my UNO.

thanks

The mosfet is actually a mosfet board, but it makes little difference.

Could make a big difference. A guess would be i.e. SparkFun Power Driver Shield Kit which uses N-channel MOSFETS and has pulldown resistors on the gate (important). The pulldown resistors ensure the MOSFETS are off when the signal controlling the gate is high impedance, like when the Arduino starts up and its pins are configured as input.

Have you changed the "outPin" to something other than pin 13 as suggested by Krupski? (this is essential). Try pin 12.

Not sure if this is essential, but in the setup, I would put these 2 lines in sequence:

pinMode(outPin, OUTPUT);
digitalWrite(outPin, LOW);

Ahhh! My apologies, I completely missed your point Krupski. You are saying that in normal operation, the arduino will flash the onboard led to show the bootloader is loading, which also sends a signal to pin 13.

Changing it to pin 12 has resolved the issue. I presume the only way to change this is to modify the bootloader?

Thanks to you both for your help.

I presume the only way to change this is to modify the bootloader?

Or the pin that you use. I wonder which is easier.