Sketch words ‘not declared’

Hello,

I am using a published sketch (with a few mods of mine) to run a sensorless BLDC and have copied it into and compiled it in two different IDEs, Arduino’s own and also Microsoft’s Visual Editor (with VisualMicro installed) to check all is OK.

In both cases a list of errors occurred that seem to be about the use of words or phrases that are not recognised and ‘declared in this scope’.

So what I would like to know is can one just define a list of words in setup that will remove these errors or is something more involved required? What syntax is required?

Below the first block of text was made by Arduino's own IDE and the second with Visual Editor. The whole sketch is shown in the attached pdf.

Thank you

Jules

In function 'void loop()':

sketch_BLDC-JP1:73:1: error: 'cycle' was not declared in this scope

 cycle = PWM_START_DUTY

 ^~~~~

exit status 1
'cycle' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Compiling 'BLDC_JP1' for 'Arduino Uno'
 
BLDC_JP1.ino: 10:1: error: 'ESC' does not name a type
Error compiling project sources
   ESC)
Build failed for project 'BLDC_JP1'
   ^~~
 
BLDC_JP1.ino: In function void __vector_23()
 
BLDC_JP1.ino: 41:13: error: 'bldc_step' was not declared in this scope
   if (bldc_step & 1) {
   ^~~~~~~~~
BLDC_JP1.ino:41: note  suggested alternative  bldc_move
   if (bldc_step & 1) {
   ^~~~~~~~~
   bldc_move
 
BLDC_JP1.ino: 49:5: error: 'bldc_step' was not declared in this scope
   bldc_step++
   ^~~~~~~~~
BLDC_JP1.ino:49: note  suggested alternative  bldc_move
   bldc_step++
   ^~~~~~~~~
   bldc_move
 
BLDC_JP1.ino: In function void bldc_move()
 
BLDC_JP1.ino: 53:13: error: 'bldc_step' was not declared in this scope
   switch (bldc_step) {
   ^~~~~~~~~
BLDC_JP1.ino:53: note  suggested alternative  bldc_move
   switch (bldc_step) {
   ^~~~~~~~~
   bldc_move
 
BLDC_JP1.ino: In function void loop()
 
BLDC_JP1.ino: 82:5: error: 'cycle' was not declared in this scope
   cycle = PWM_START_DUTY
   ^~~~~
 
BLDC_JP1.ino: 88:9: error: 'bldc_step' was not declared in this scope
   bldc_step++
   ^~~~~~~~~
BLDC_JP1.ino:88: note  suggested alternative  bldc_move
   bldc_step++
   ^~~~~~~~~
   bldc_move
 
BLDC_JP1.ino: 92:5: error: 'motor_speed' was not declared in this scope
   motor_speed = PWM_START_DUTY
   ^~~~~~~~~~~

Arduino Motor Sketch-vJP1.pdf (53.2 KB)

I am using a published sketch

Does the original code compile without errors ?

Please Read this before posting a programming question and follow the advice on posting code and errors

In the second line of your Loop() function you have cycle = something.

But no where do you declare it... e.g. int cycle; or something similar.

cycle type has to be declared somewhere in the code and is not.
according to the Arduino reference, ++ must be either int or int long, so byte bldc_step++ not a valid type for using ++.

according to the Arduino reference, ++ must be either int or int long, so byte not a valid type.

Can you please provide a link to that in the reference because ++ is perfectly valid to use with a byte variable

DaleScott:
according to the Arduino reference, ++ must be either int or int long, so byte bldc_step++ not a valid type for using ++.

This is complete nonsense.

The operand could also be a pointer.

A tutorial on scope.

The source of the original published code is: Sensorless BLDC motor control with Arduino - DIY ESC - SIMPLE PROJECTS

The few mods I made did not raise any issues in the IDE but I will run the original and post anything that occurs.

I'm not sure how you downloaded the code, but the error messages you posted are caused by the text being formatted incorrectly. As an example, your first error message was from this section of code:

void loop() {
 SET_PWM_DUTY(PWM_START_DUTY); // Setup starting PWM with duty
cycle = PWM_START_DUTY
 i = 10000; //5000
 // Motor start

This is how it should appear:

void loop() {
  SET_PWM_DUTY(PWM_START_DUTY);    // Setup starting PWM with duty cycle = PWM_START_DUTY
  i = 10000; //5000;
  // Motor start

Ok I can see the difference in that a line break has occurred and that has been interpreted as a separate instruction. However, doesn't the compiler ignore white space after a // and so it shouldn't matter?

The sketch was exported into a pdf and I then copied it out of that.

Jules

The problem is that the comment has been broken up into two lines in the pdf file. As far as the compiler is concerned, the comment ends at the end of the 1st line, and the 2nd line is completely separate and is being interpreted as a line of code.

Attached is the original code I copied from the website you linked, formatted correctly so that it will compile.

Sensorless_BLDC.ino (4.75 KB)

Reference > Language > Structure > Compound operators > Increment
++
[Compound Operators]
Description
Increments the value of a variable by 1.

Syntax
x; // increment x by one and returns the old value of x+x; // increment x by one and returns the new value of x

Parameters
x: variable. Allowed data types: int, long (possibly unsigned).

////////////////\

DaleScott:
++ - Arduino Reference

Reference > Language > Structure > Compound operators > Increment
++
[Compound Operators]
Description
Increments the value of a variable by 1.

Syntax
x; // increment x by one and returns the old value of x+x; // increment x by one and returns the new value of x

Parameters
x: variable. Allowed data types: int, long (possibly unsigned).

////////////////\

That's appalling. It doesn't even get the syntax right, or mention the difference between pre- and post-increment.

That's appalling. It doesn't even get the syntax right, or mention the difference between pre- and post-increment.

You can raise the issue or submit a correction at GitHub - arduino/reference-en: Editable source for the Arduino Reference