Why does my if function keep looping?


sorry if has been addressed before, can't find other thread.  i'm building cosplay prop (lightning gun) , having issues if function in code.  when pull trigger (momentary switch), cues lightning light.  it's supposed turn off, fade on , stay on.  when pull trigger, fades on, loops if statement forever.

in looking @ serial monitor, keeps spitting out alternate 1s , 0s works through if statement on , over.  it's program thinks trigger held down (or pulled repeatedly), it's not.  digitalread(trigger) should notice value no longer 0 , not perform if function, right?  appreciated.

code: [select]
int lightning = 10;
int trigger = 2;
//int triggerval = 1;

void setup() {
  serial.begin(9600);
  pinmode(trigger, input_pullup);
  pinmode(lightning, output);
}

void loop() {
  int triggerval = digitalread(trigger);
  if(triggerval == 0) {
    serial.println(triggerval);     // shows me 0 in serial monitor trigger pulled
    analogwrite(lightning,0);
    for(int fadevalue1 = 0 ; fadevalue1 <= 255; fadevalue1 +=20) {
      analogwrite(lightning,fadevalue1);
      delay(50);
    }
    triggerval = 1;     // trying reset trigger value "off"
    serial.println(triggerval);     // part shows me 1 in serial monitor
  }
}

then triggerpin must reading low.  perhaps due noise.  try decent external pullup resistor such as
2.2k, internal pullup may weak.


Arduino Forum > Using Arduino > Programming Questions > Why does my if function keep looping?


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts