An inquiry about the if statement

Hey guys,

I’m doing some tests with Proteus ISIS and Arduino UNO, the code is as follows:

int i = 0;
void setup() {
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
pinMode(1,INPUT);
}
void P1(){


  digitalWrite(12,1);
  digitalWrite(13,1);
  digitalWrite(11,0);
  digitalWrite(10,0);
}

void P2(){
  digitalWrite(12,1);
  digitalWrite(13,0);
  digitalWrite(11,1);
  digitalWrite(10,0);
}
void P3(){
  digitalWrite(12,0);
  digitalWrite(13,0);
  digitalWrite(11,1);
  digitalWrite(10,1);
}
void P4(){
  digitalWrite(12,0);
  digitalWrite(13,1);
  digitalWrite(11,0);
  digitalWrite(10,1);
}


void loop() {
if (digitalRead(1)== 1) { delay(50); i++;}
if (i == 1) {P1();}
if (i == 2) {P2();}
if (i == 3) {P3();}
if (i == 4) {P4();}
if (i >3) { i = 0;}

  }

I’m having an issue with the first statement in the loop function when, it activates it stays activated which means that the i var gets in an infinite loop of incrementation,

So what do you think is the problem here?

Your code... You tell it to do so. If you don't want that, don't do that...

PS Use a real Arduino, simulators suck...
PSS Use variables!
PSSS Don't try to cram everything on a single line! NOTHING after a { } or ;

And use HIGH and LOW with digitalWrite, not 0 and 1. The API takes HIGH and LOW and those happen to be defined as 0 and 1 for the time being and on the processors we have right now but that may not always be the case.

So, what you really have is a problem with reading the state of the pin. You are incrementing the value when the pin, without the pullup resistor, IS high, rather than when the pin BECOMES high. Look at the state change detection example, AND tell us how the external resistor, which you absolutely need, is wired.