Cant increase variable.... please help.

Please help... (i also tried calling the variable at the start of code before the startup.)
I am trying to get the variable i to increase every time motion is sensed. but when motion is sensed, i get this output:

Count: 0
Value: 1

Immediate: 1

Count: 0
Value: 0

My code is blow.

int motion = 2;//motion pin
int LED = 3;//led pin



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(motion, INPUT);
  digitalWrite(LED, LOW);

}

void loop() {
  // put your main code here, to run repeatedly:
  int i;
  int value = digitalRead(motion);
  Serial.print("\n\n\n Count: ");
  Serial.print(i);
  Serial.print("\n Value: ");
  Serial.print(value);
  if (value == HIGH)
  {
    digitalWrite(LED, HIGH);
    delay(3000);
    int i = ++i;
  Serial.print("\n\n\n Immediate: ");
  Serial.print(i);
  }
  else
  {
    digitalWrite(LED, LOW);
  }
  delay(100);

}
  int i;

This declares a variable named i and sets the variable to a random starting value each time through loop(). Is that what you want ?

   int i = ++i;

This declares a different variable named i with a different scope to a random starting value then tries to increment it as part of the initialisation which is undefined behaviour

Make the first declaration of i

 static int i;

to preserve its values between executions of the loop() function and

   ++i;

where you want to increment the value of that variable

Hi micahr,

I recommend reading this tutorial

Arduino Programming Course
https://startingelectronics.org/software/arduino/learn-to-program-course/

Arduino Programming Course

It is easy to understand and a good mixture between writing about important concepts and get you going.

As I'm no longer a beginner I can't judge if this tutorial is really easy to understand. So I would love to read your opinion
about it.

best regards Stefan