Error scope not declared

Hii i'm wanna find out what is resulting into the following error:

E:\TCC\Programação\Contador.sensor.optico\Contador.sensor.optico.ino: In function 'void setup()':
E:\TCC\Programação\Contador.sensor.optico\Contador.sensor.optico.ino:15:3: error: 'motorcSetup' was not declared in this scope
E:\TCC\Programação\Contador.sensor.optico\Contador.sensor.optico.ino:18:3: error: 'motorcAttachPin' was not declared in this scope

exit status 1

Compilation error: 'motorcSetup' was not declared in this scope

I'm trying to do a speed control for a DC motor using a PWM exit on a Wemos D1 R32, and this error insists into annoying me, i don't know what i'm doing wrong, if you can help i would be very grateful. you can see my code right below:

int pinoLed = 23; //PINO DIGITAL UTILIZADO PELO LED  
int pinoSensor = 19; //PINO DIGITAL UTILIZADO PELO SENSOR
int contador=0;
const int motor = 18;
const int freq = 1000;
const int Channel = 0;
const int resolution = 8;

void setup(){  
  pinMode(pinoSensor, INPUT); //DEFINE O PINO COMO ENTRADA
  pinMode(pinoLed, OUTPUT);  
  digitalWrite(pinoLed, LOW); //LED INICIA DESLIGADO
  Serial.begin(115200);
  // Configuraçao do LED PWM (Passos 1, 2, 3)
  motorcSetup(Channel, freq, resolution);
  
  // Conecte o canal ao GPIO a ser controlado (passo 4)
  motorcAttachPin(motor, Channel);

}  
   
void loop(){
  for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
    ledcWrite(Channel, dutyCycle);
    delay(20);
  }
  if (digitalRead(pinoSensor) == 1){ //SE A LEITURA DO PINO FOR IGUAL A HIGH, FAZ
        digitalWrite(pinoLed, 1); //ACENDE O LED
        while(digitalRead(pinoSensor)==1){
        contador++; // Ou pode ser count++; que é a mesma coisa
        Serial.print("Vezes que o contador foi ativado => ");
        Serial.println(contador);
        }
  }else{//SENÃO, FAZ
        digitalWrite(pinoLed, 0); //APAGA O LED
        //while(digitalRead(pinoSensor)==0){
        //contador--; // Ou pode ser count++; que é a mesma coisa
        //Serial.print("Vezes que o contador foi ativado => ");
        //Serial.println(contador);
        //delay(50);})
      }
}

PS: I might have mixed English and portuguese in the code, so sorry for being a little confusing.

Looks like you're calling a couple of functions that don't exist.

Where did you get the code? What is motorcAttachPin supposed to do? Where is that function defined? Was there supposed to be a library included?

It sounds like you've completely misunderstood what you watched. I don't know what you watched so I can't comment on that. There are a lot of bad tutorials out there.

I do know that you can't use a function without telling what that function should do. You definitely need to define the function before you use it.

Are you trying to use the functions ledcSetup and ledcAttachPin associated with the LEDC (LED control) peripheral? If so, you can't just make up your own names and expect it to work.

And if so, there are breaking changes between the v2 and v3 cores, and the ledcSetup and ledcAttachPin functions have themselves gone away.

You get the initial error message of

Because it was defined in the setup function, and therefore can only be used in the setup function. Everywhere else this function can't be used.
So to make it a global function you need to define it outside of any function, so the code reads:-

const int resolution = 8;
  // Configuraçao do LED PWM (Passos 1, 2, 3)
  motorcSetup(Channel, freq, resolution);

void setup(){  

However that just shifts the problem (error message) to a new concern.

sketch_jun26a:9:14: error: expected constructor, destructor, or type conversion before '(' token
   motorcSetup(Channel, freq, resolution);

This is probably because, as @Delta_G said, you are missing a library, so the compiler has no idea what the motorSetup is.

Also @van_der_decken has pointed out your code has other problems as well.

You probably missed / skipped the bit where they mentioned about this missing library, and where to get it from.

Sorry, but it seems to be a complete nonsense for me. motorSetup is a function, so it can't be "defined in the setup` as you wrote.

1 Like

I think you are misunderstanding that comment. The OP was declaring it in the setup, and I was telling him if he did that it would not be usable outside the setup function. I used the word declaring in an attempt to simplify the concept. Where in actual fact he is initialising a class, sorry if this offends you.

But I never told him to do this,I told him what he was doing.

That is simply wrong. He NEVER DEFINES them, he tries to CALL them, as they are clearly meant to be functions, not classes.

Please read what I said, and the explanation of why I said it. Everyone has gone ballistically pedantic all of a sudden.

I think @Grumpy_Mike believes those lines were creating local instances of some class.

But there's no class involved. It's really a moot point. Whether those are class instances or function calls, the simple fact is that nothing exists by that name in this code.

Sorry, but it's not simply pedantic. Everything you said is factually wrong, so how does it help the OP, who is already confused? That was my initial reaction, and after reading your posts several more times, that hasn't changed.

No, he was not "declaring" anything, as you cannot declare a function within another function. What you wrote suggests it is possible to declare a function within another function, and I'm sure you know that. It is clear the two lines in question are attempts to CALL the functions motorcSetup and motorcAttachPin. No classes involved. I very much doubt the OP understands classes, nor the concepts of definition and declaration.

It would not be usable ANYWHERE, inside or outside of setup, since the symbols motorcSetup and motorcAttachPin are never defined anywhere. Again, you know this, so why give such a misleading answer?

And by doing so, you muddied the waters considerably, by mis-informing someone who already has little, if any, understanding. Mis-using the word "declaring", in all likelihood, has no meaning to the OP. How does mis-information "simplify"?

I see no indication he is initializing a class. mototcSetup and motorcAttachPin are clearly function calls, NOT classses. Even the names do not sound like classes. What makes you think they are classes?

Technically, the OP wasn't "doing" anything. He clearly copied this code from some online tutorial (and we all know what many of those are worth), and does't really understand much, if anything, about how the code works.

1 Like

Since this topic has seriously gone off the rails, I'm just going to point out that if all occurrences of 'motorc' in the original sketch are replaced with 'ledc' to make the function calls match the names for the LEDC peripheral, the sketch compiles error free for a D1 R32 with a V2 core.

int pinoLed = 23; //PINO DIGITAL UTILIZADO PELO LED  
int pinoSensor = 19; //PINO DIGITAL UTILIZADO PELO SENSOR
int contador=0;
const int motor = 18;
const int freq = 1000;
const int Channel = 0;
const int resolution = 8;

void setup(){  
  pinMode(pinoSensor, INPUT); //DEFINE O PINO COMO ENTRADA
  pinMode(pinoLed, OUTPUT);  
  digitalWrite(pinoLed, LOW); //LED INICIA DESLIGADO
  Serial.begin(115200);
  // Configuraçao do LED PWM (Passos 1, 2, 3)
  ledcSetup(Channel, freq, resolution);
  
  // Conecte o canal ao GPIO a ser controlado (passo 4)
  ledcAttachPin(motor, Channel);

}  
   
void loop(){
  for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
    ledcWrite(Channel, dutyCycle);
    delay(20);
  }
  if (digitalRead(pinoSensor) == 1){ //SE A LEITURA DO PINO FOR IGUAL A HIGH, FAZ
        digitalWrite(pinoLed, 1); //ACENDE O LED
        while(digitalRead(pinoSensor)==1){
        contador++; // Ou pode ser count++; que é a mesma coisa
        Serial.print("Vezes que o contador foi ativado => ");
        Serial.println(contador);
        }
  }else{//SENÃO, FAZ
        digitalWrite(pinoLed, 0); //APAGA O LED
        //while(digitalRead(pinoSensor)==0){
        //contador--; // Ou pode ser count++; que é a mesma coisa
        //Serial.print("Vezes que o contador foi ativado => ");
        //Serial.println(contador);
        //delay(50);})
      }
}
arduino-cli compile -b esp32:esp32:d1_uno32 --warnings all --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Firebeetle32/test)
Sketch uses 268481 bytes (20%) of program storage space. Maximum is 1310720 bytes.
Global variables use 21464 bytes (6%) of dynamic memory, leaving 306216 bytes for local variables. Maximum is 327680 bytes.
Used platform Version Path
esp32:esp32   2.0.14  /home/me/.arduino15/packages/esp32/hardware/esp32/2.0.14
Compilation finished successfully.
1 Like

Sorry but you are.

Are you a native speaker of English?

Yep I agree, sadly.

So where on earth are ledcSetup and ledcAttachPIn defined??? Certainly not in the posted code, and there are no #includes.

They are defined in cores/esp32/esp32-hal-ledc.h and cores/esp32/esp32-hal-ledc.c. The header is included by the always automagically included Arduino.h header via cores/esp32/esp32-hal.h.

And this all gets changed in the V3 core...

Ah! So automagically included by the Arduino build. I've used ESPs a lot, but never came across those.

And they ARE functions, not classes.

Thanks!

Yes. Too bad

[quote="van_der_decken, post:5, topic:1275560"]
you can't just make up your own names and expect it to work.
[/quote]

THX for taking the trouble.

a7

1 Like

Thanks to all you guys, the board library that i was using had the function that a i need in older version (not the one i was using too), and for some reason they don´t exist in the most recent version, i´ts working rn

1 Like