# 'i' not declared in this scope

**URL:** <https://forum.arduino.cc/t/i-not-declared-in-this-scope/520943>\
**Category:** Programming\
**Created:** [April 17, 2018, 9:37pm UTC](https://forum.arduino.cc/t/i-not-declared-in-this-scope/520943 "2018-04-17T21:37:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonman5](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@jonman5](https://forum.arduino.cc/u/jonman5)\
**Post date:** [April 17, 2018, 9:37pm UTC](https://forum.arduino.cc/t/i-not-declared-in-this-scope/520943/1 "2018-04-17T21:37:41Z")

</div>

I don't understand I am getting the error message "'i' is not declared in this scope" even though it should be.  
It is originating from the line `if ((i)==1){`

Here is my code:

```cpp
#include <AFMotor.h>
//setting pin numbers
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
const int trigpin = 53;
const int echopin = 51;

//setting up other variables
int range;
long duration;
int distance;
int start_turn=-15001;
//defining threshold distance in cm
const int t_d=20;

void setup() {
  Serial.begin(9600);          // set up Serial library at 9600 bps
  
  //set up sensor input, output
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT);
  Serial.println("Start");
  
  // turn on motors and set the speed of motors
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor3.setSpeed(255);
  motor4.setSpeed(255);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  
}

//defining the function for getting the range to nearest obstacle in cm

int get_range() {
  digitalWrite(trigpin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigpin, LOW);

  duration = pulseIn(echopin, HIGH);
  distance = duration*0.034/2;
  return distance;
}

void loop() {

  range=get_range();
  while ((range)>=(t_d)){
    //Serial.print("forward");
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
    range=get_range();
  }
  Serial.println(range);
  while ((range)<(t_d)){
    if (abs((millis()-(start_turn)))>10000){
      randomSeed(analogRead(9));
      const int i = (random(1, 256))%2;
    }
    if ((i)==1){
      Serial.print("right");
      motor1.run(BACKWARD);
      motor2.run(BACKWARD);
      motor3.run(FORWARD);
      motor4.run(FORWARD);
      range=get_range();
      start_turn=millis();
    }
    else{
      motor1.run(FORWARD);
      motor2.run(FORWARD);
      motor3.run(BACKWARD);
      motor4.run(BACKWARD);
      range=get_range();
      start_turn=millis();
    } 
    if (range>t_d){
      delay(3000);
    }
    
  }
}

```

---

<div class="post-metadata">

**Author:** ![pert](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pert/32/217111_2.png) [@pert](https://forum.arduino.cc/u/pert)\
**Post date:** [April 17, 2018, 9:42pm UTC](https://forum.arduino.cc/t/i-not-declared-in-this-scope/520943/2 "2018-04-17T21:42:16Z")

</div>

Do some research on variable scope:  
[https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/](https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/)  
You've declared i within this scope:

```cpp
    if (abs((millis()-(start_turn)))>10000){
      randomSeed(analogRead(9));
      const int i = (random(1, 256))%2;
    }

```

so you can only use it within that scope.

---

<div class="post-metadata">

**Author:** ![CrossRoads](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/crossroads/32/214033_2.png) [@CrossRoads](https://forum.arduino.cc/u/CrossRoads)\
**Post date:** [April 17, 2018, 9:42pm UTC](https://forum.arduino.cc/t/i-not-declared-in-this-scope/520943/3 "2018-04-17T21:42:45Z")

</div>

add  
byte i;  
before setup()  
so it's a global variable.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 17, 2018, 9:44pm UTC](https://forum.arduino.cc/t/i-not-declared-in-this-scope/520943/4 "2018-04-17T21:44:39Z")

</div>

> CrossRoads:  
> add  
> byte i;  
> before setup()  
> so it's a global variable.

and don't put const int in front of it when you assign the random number to it otherwise you will have 2 different variables with the same name but different scopes

---

<div class="post-metadata">

**Author:** ![jonman5](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@jonman5](https://forum.arduino.cc/u/jonman5)\
**Post date:** [April 17, 2018, 10:02pm UTC](https://forum.arduino.cc/t/i-not-declared-in-this-scope/520943/5 "2018-04-17T22:02:19Z")

</div>

Thanks guys, I'm not very experienced with coding for Arduino or in C++ in general so I didn't understand the scope thing totally because it is different from other languages I've learned so far.

I'm going to use what you guys suggested.

---

<div class="post-metadata">

**Author:** ![arduino\_new](https://avatars.discourse-cdn.com/v4/letter/a/dc4da7/32.png) [@arduino\_new](https://forum.arduino.cc/u/arduino_new)\
**Post date:** [April 17, 2018, 11:14pm UTC](https://forum.arduino.cc/t/i-not-declared-in-this-scope/520943/6 "2018-04-17T23:14:30Z")

</div>

> jonman5:  
> Thanks guys, I'm not very experienced with coding for Arduino or in C++ in general so I didn't understand the scope thing totally because it is different from other languages I've learned so far.
> 
> I'm going to use what you guys suggested.

General rule is everything declared inside {} will only live in that {}.

If you want to pursue further, I suggest to learn more about scoping and not giving up by using global variables when it's not necessary because it's a potentially hazard.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:08pm UTC](https://forum.arduino.cc/t/i-not-declared-in-this-scope/520943/7 "2021-05-05T23:08:21Z")

</div>

Closed on May 5, 2021, 11:08pm UTC


