Pages: [1] 2 3   Go Down
Author Topic: name lookup of 'i' changed for new ISO 'for' scoping  (Read 4887 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 2
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've written a simple brushless motor testing program, but i'm getting a weird error I've never seen before  smiley-eek-blue!

What does this error mean and how do I fix it? Thanks in advance!
BTW the error happens somewhere in the for loop where 'i' is between 1500 and 1000 I think. The code works fine up till that point.

Code:
#include <LiquidCrystal.h>
#include <Servo.h> //Notice how you can use the standard servo library!

Servo bldc_motor; //creates a "servo" object (the ESC and motor)
LiquidCrystal LCD (2, 3, 4, 5, 6, 7); ///create an LCD


void setup()
{
  bldc_motor.attach (9); //attach the motor to pin 9
  LCD.begin (16, 2); //initialise the LCD
  LCD.clear();
}

void loop()
{
  bldc_motor.writeMicroseconds(1500); //provides a "neutral" pulse. The ESC won't start without this.
  LCD.print ("BLDC Motor Test");
  LCD.setCursor (0, 1);
  LCD.print ("Starting in ");
 
  for (int i = 4; i >= 0; i --) //countdown timer
  {
    LCD.setCursor (12, 1);
    LCD.print (i+1);
    LCD.print ("s");
    delay (1000);
  }
 
  LCD.setCursor (0, 1);
  LCD.print ("                "); //clears the bottom line of the LCD
 
  LCD.setCursor (5, 1);
  LCD.print ("forward"); //writing it here so it doesn't keep rewriting this in the for loop
 
  while(1)
  {
    for (int i = 1500; i <= 2000; i++)
    {
      int speed_value = map (i, 1500, 2000, 0, 100); //maps the signal pulse to a percentage
      LCD.setCursor (0, 1);
      LCD.print ("   "); //clears any previous number;
      LCD.setCursor (0, 1);
      LCD.print (speed_value); //prints the percentage to the LCD
      LCD.print ('%'); //notice the single quotation marks. I've just sent a char value, not a string because it's only 1 character
     
      bldc_motor.writeMicroseconds (i); //actually write the value to the motor
      delay(10);// small delay so you can see what's happening
    }
   
    LCD.setCursor (0, 1);
    LCD.print ("                "); // clears the bottom of the LCD screen;
    LCD.setCursor (0, 1);
    LCD.print ("braking in");
   
    for (int i = 4; i >= 0; i --) //countdown timer
    {
      LCD.setCursor (12, 1);
      LCD.print (i+1);
      LCD.print ("s");
      delay (1000);
    }
   
    bldc_motor.writeMicroseconds (1000); //full reverse. The ESC will automatically brake the motor.
    delay (500); //small delay to let things happen
    bldc_motor.writeMicroseconds (1500); //give a neutral signal
    LCD.setCursor (0, 1);
    LCD.print ("motor stopped");
    delay (3000); //keep it that way for a while
   
    LCD.setCursor (0, 1);
    LCD.print ("                "); //clear the bottom line of the LCD
    LCD.setCursor (5, 1);
    LCD.print ("backward");
   
    for (int i = 1500; i >= 1000; i --);
    {
      int speed_value = map (i, 1500, 1000, 0, 100); //maps the signal pulse to a percentage
      LCD.setCursor (0, 1);
      LCD.print ("   "); //clears any previous number;
      LCD.setCursor (0, 1);
      LCD.print (speed_value); //prints the percentage to the LCD
      LCD.print ('%'); //notice the single quotation marks. I've just sent a char value, not a string because it's only 1 character
     
      bldc_motor.writeMicroseconds (i); //actually write the value to the motor
      delay(10);// small delay so you can see what's happening
    }
   
    LCD.setCursor (0, 1);
    LCD.print ("                "); //clear bottom line of LCD
   
    bldc_motor.writeMicroseconds (1500);
    LCD.setCursor (0, 1);
    LCD.print ("motor stopped");
    delay (3000);   
  }
     
   
 
}


Error messages:

name lookup of 'i' changed for new ISO 'for' scoping


sketch_nov03a.cpp: error: In funcion 'void loop()':
sketch_nov03a:77: error: name lookup of 'i' changed for new ISO 'for' scoping
sketch_nov03a:37: error: using obsolete binding at 'i' (what on earth does this mean too?)
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Add a declaration for "i" after loop, eg.

Code:
void loop()
{
  int i;
 

Then remove the multiple declarations of "i" elsewhere, eg.

Change:

Code:
  for (int i = 4; i >= 0; i --) //countdown timer

to:

Code:
  for (i = 4; i >= 0; i --) //countdown timer


Effectively it is warning you about multiple redeclarations of "i".
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 506
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    for (int i = 1500; i >= 1000; i --);
    {
You've accidentally put a ; after the for line.
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You've accidentally put a ; after the for line.
and then referencing i after the end of the for loop, which isn't allowed.
Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah, thanks! That stray semicolon really gave me a headache  smiley-roll-sweat
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Any decent C++ compiler will warn about that stray semicolon, but unfortunately the Arduino IDE turns most of the gcc compiler warnings off.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well spotted, PaulS!

Older versions of the compiler would allow the "i" declared in the loop to remain in scope, so it would be valid to do this:

Code:
void setup ()
  {
  for (int i = 0; i < 42; i++)
    {
    // whatever
    }
 
  int j = i;      // <------ now raises an error
  }  // end of setup
 
void loop ()  {    }  // end of loop

However more recently "i" is scoped to the loop only, and thus the assignment to "j" fails.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any decent C++ compiler will warn about that stray semicolon, but unfortunately the Arduino IDE turns most of the gcc compiler warnings off.

Why?  That's a perfectly valid thing to do, and I do it often.  Especially when traversing to the end of a linked list:

Code:
for(ptr=head; ptr->next; ptr=ptr->next);
ptr->next = newelement;
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
That's a perfectly valid thing to do, and I do it often.  Especially when traversing to the end of a linked list
Wouldn't it be easier and faster to keep a pointer to tail?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
That's a perfectly valid thing to do, and I do it often.  Especially when traversing to the end of a linked list
Wouldn't it be easier and faster to keep a pointer to tail?
faster?  yes.  easier?  no.

You have to always ensure you update said pointer every time to add something to the tail.  It just takes one moment of forgetfulness when you don't add the tail pointer updating, and you have a rather nasty bug.  Memory-leaks-r-us.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
faster?  yes.  easier?  no.
I'll take your word for it. I've never bothered with singly linked lists. Very little extra memory and effort is required to create doubly linked lists that can then be traversed in either direction. With doubly linked lists, you don't have the luxury of forgetfulness.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Standard Template Library.

Then you don't have to re-invent the wheel every time you want a linked list for a different data type.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Standard Template Library.

Then you don't have to re-invent the wheel every time you want a linked list for a different data type.
Fine if you want to use C++ I guess.  Not if you either can't or don't want to.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
God Member
*****
Karma: 32
Posts: 506
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any decent C++ compiler will warn about that stray semicolon, but unfortunately the Arduino IDE turns most of the gcc compiler warnings off.

Why?  That's a perfectly valid thing to do, and I do it often.  Especially when traversing to the end of a linked list:

Code:
for(ptr=head; ptr->next; ptr=ptr->next);
ptr->next = newelement;

Yes but have you ever intentionally written for(LinkedList * ptr=head; ptr->next; ptr=ptr->next); and then immediately followed it with a { ?

As far as I know there is no GCC warning for this kind of mistake anyway.
Logged


UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any decent C++ compiler will warn about that stray semicolon, but unfortunately the Arduino IDE turns most of the gcc compiler warnings off.

Why?  That's a perfectly valid thing to do, and I do it often.  Especially when traversing to the end of a linked list:

Code:
for(ptr=head; ptr->next; ptr=ptr->next);
ptr->next = newelement;

Yes but have you ever intentionally written for(LinkedList * ptr=head; ptr->next; ptr=ptr->next); and then immediately followed it with a { ?

As far as I know there is no GCC warning for this kind of mistake anyway.

Can't say as I have, no, however there is no syntactical reason why that would be wrong.

There is nothing to say that a { HAS to follow a for/while/if etc - it can just be on its own.  You can use it to group blocks of code to make it easier to read.  I don't know anyone that actually does that, but it's perfectly valid.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Pages: [1] 2 3   Go Up
Jump to: