Custom Library not working

Hey there

So i downloaded this custom library (http://playground.arduino.cc/Main/SharpIR) that is suppose to take X amount of readings from my sharp sensor and then average them out to get a more accurate distance. I tried testing it out using a simple two motor car code however i kept getting this error “expected initializer before sharp”. However if you look at the code I DO have an initializer (its just custom) as the SharpIR.

Is there a reason why the code will not compile? Is it because it is custom and maybe it can not ready the SharpIR initializer part?

Thanks for any help!

EDIT:

I did some tweaking and for some reason it will compile if i change the first bit to this:

SharpIR sharp(A2, 25, 93, 1080);

void setup() {

SharpIR sharp(A2, 25, 93, 1080);

Serial.begin(9600);
Serial.println(“Autonomous RC Car”);

pinMode(BRAKE_A, OUTPUT);
pinMode(BRAKE_B, OUTPUT);

pinMode(DIR_A, OUTPUT);
pinMode(DIR_B, OUTPUT);

I have no idea why repeating it twice helps.

#include <SharpIR.h>

const int
PWM_A = 3,
PWM_B = 11,
DIR_A = 12,
DIR_B = 13,
BRAKE_A = 9,
BRAKE_B = 8,
SNS_A = A0,
SNS_B = A1,

[b]SharpIR sharp(A2, 25, 93, 1080);[/b]

void setup() {
  
 Serial.begin(9600);
 Serial.println("Autonomous RC Car");
  
 pinMode(BRAKE_A, OUTPUT);
 pinMode(BRAKE_B, OUTPUT);

 pinMode(DIR_A, OUTPUT);
 pinMode(DIR_B, OUTPUT);
 
  
}

void loop() {
  
  int dis = sharp.distance();
  
  if(dis > 10) {  // If distance is greater than 10 cm do the following
 
  Serial.print("Path is clear OK to go");
 
  digitalWrite(BRAKE_A, LOW);
  digitalWrite(BRAKE_B, LOW);
  
  digitalWrite(DIR_A, HIGH); // motor 1 forward
  digitalWrite(DIR_B, HIGH); // motor 2 forward
  
  analogWrite(PWM_A, 200); // motor 1 speed 200
  analogWrite(PWM_B, 200); // mtoor 2 speed 200 
  delay(3000); // go for 3 seconds
  
  digitalWrite(BRAKE_A, HIGH); // motor 1 brake
  digitalWrite(BRAKE_B, HIGH); // motor 2 brake

  }
  else {
    
  Serial.print("Path is not clear reversing");  
    
  digitalWrite(BRAKE_A, LOW);
  digitalWrite(BRAKE_B, LOW);
    
  digitalWrite(DIR_A, LOW); // motor 1 reverse
  digitalWrite(DIR_B, LOW); // motor 2 reverse
    
  analogWrite(PWM_A, 200); // motor 1 speed 200
  analogWrite(PWM_B, 200); // mtoor 2 speed 200 
  delay(3000); // go for 3 seconds  
    
  digitalWrite(BRAKE_A, HIGH); // motor 1 brake
  digitalWrite(BRAKE_B, HIGH); // motor 2 brake
  delay(1000); // wait a second
  
  digitalWrite(BRAKE_A, LOW);
  digitalWrite(BRAKE_B, LOW);
 
  digitalWrite(DIR_A, LOW); // motor 1 reverse
  digitalWrite(DIR_B, HIGH); // motor 1 forward
  
  analogWrite(PWM_A, 200); // motor 1 speed 200
  analogWrite(PWM_B, 200); // motor 2 speed 200 
  delay(1600); // I am going to tweak this number to get as close to a 90 degree turn as possible
  
  digitalWrite(BRAKE_A, HIGH); // motor 1 brake
  digitalWrite(BRAKE_B, HIGH); // motor 2 brake
  
  }
}

HOW TO IMPORT/INSTALL
Put the SharpIR folder in "libraries".
Did you do it?

I have no idea why repeating it twice helps.

Perhaps if you'd actually read the error message, the light would have gone on. Creating a variable inside the setup() function means that it goes out of scope when the function ends. Creating another one at global scope means that the instance can be used in the loop() function. The one in the setup() function is doing nothing.

Perhaps if you'd actually read the error message, the light would have gone on. Creating a variable inside the setup() function means that it goes out of scope when the function ends. Creating another one at global scope means that the instance can be used in the loop() function. The one in the setup() function is doing nothing.

I can only hope that by repeating the answer twice that it will sink in.

I might be extremely new to Arduino and coding but I am not an idiot. Yes I already imported the library and yes it is already under my library tab alongside all the other built in libraries.

In regards to what PaulS said I did read the error I just didn't understand what to do or how to fix it, hence why i am asking you. And once again what you are saying does not make sense to me. I am sorry i am so new but please humor me for a second.

SharpIR sharp(A2, 25, 93, 1080);
void setup() {
pinMode(BRAKE_A, OUTPUT);
pinMode(BRAKE_B, OUTPUT);
pinMode(DIR_A, OUTPUT);
pinMode(DIR_B, OUTPUT);
}
void loop() {
int dis = sharp.distance();

This bit of code is returning this error:
sketch_land:14: error: expected initializer before 'sharp'
sketch_land.ino: In function 'void loop()':
sketch_land:29: error: 'sharp' was not declared in this scope

You said that if I created the variable 'sharp' in the global scope (up top) then it would be recognized and used by void loop(). However the error specifically says sharp was not declared in void loop. So I believe I need to move SharpIR sharp(A2, 25, 93, 1080); this part to void loop.

Please if i am wrong, help me. Do not make me feel belittled and stupid. If thats what your message wanted me to do then thank you. But I just moved it to the void loop scope and it compiled, i deleted the repeat too.

For some reason void loop can not read it from the global scope. I hope that makes sense.

thanks

This bit of code

doesn't include the header file.

Hi JD150

The original code you posted (minus the bold tags) had:

const int
PWM_A = 3,
PWM_B = 11,
DIR_A = 12,
DIR_B = 13,
BRAKE_A = 9,
BRAKE_B = 8,
SNS_A = A0,
SNS_B = A1,
SharpIR sharp(A2, 25, 93, 1080);

Try changing the comma after SNS_B = A1 to a semicolon.

Here is the current state of the code. I changed the comma and everything works aka it compiles.

EDIT: Also if you are wondering why I have SNS A and B but do not use it in the code it is because I am going to ad in current reading for the two motors after I solve this problem.

#include <SharpIR.h>


const int
PWM_A = 3,
PWM_B = 11,
DIR_A = 12,
DIR_B = 13,
BRAKE_A = 9,
BRAKE_B = 8,
SNS_A = A0,
SNS_B = A1;

void setup() {
  
 pinMode(BRAKE_A, OUTPUT);
 pinMode(BRAKE_B, OUTPUT);

 pinMode(DIR_A, OUTPUT);
 pinMode(DIR_B, OUTPUT);
 
  
}

void loop() {
  
  SharpIR sharp(A2, 25, 93, 1080);
  
  int dis = sharp.distance();
  
  if(dis > 10) {  // If distance is greater than 10 cm do the following
  digitalWrite(BRAKE_A, LOW);
  digitalWrite(BRAKE_B, LOW);
  
  digitalWrite(DIR_A, HIGH); // motor 1 forward
  digitalWrite(DIR_B, HIGH); // motor 2 forward
  
  analogWrite(PWM_A, 200); // motor 1 speed 200
  analogWrite(PWM_B, 200); // mtoor 2 speed 200 
  delay(3000); // go for 3 seconds
  
  digitalWrite(BRAKE_A, HIGH); // motor 1 brake
  digitalWrite(BRAKE_B, HIGH); // motor 2 brake

  }
  else {
    
  digitalWrite(BRAKE_A, LOW);
  digitalWrite(BRAKE_B, LOW);
    
  digitalWrite(DIR_A, LOW); // motor 1 reverse
  digitalWrite(DIR_B, LOW); // motor 2 reverse
    
  analogWrite(PWM_A, 200); // motor 1 speed 200
  analogWrite(PWM_B, 200); // mtoor 2 speed 200 
  delay(3000); // go for 3 seconds  
    
  digitalWrite(BRAKE_A, HIGH); // motor 1 brake
  digitalWrite(BRAKE_B, HIGH); // motor 2 brake
  delay(1000); // wait a second
  
  digitalWrite(BRAKE_A, LOW);
  digitalWrite(BRAKE_B, LOW);
 
  digitalWrite(DIR_A, LOW); // motor 1 reverse
  digitalWrite(DIR_B, HIGH); // motor 1 forward
  
  analogWrite(PWM_A, 200); // motor 1 speed 200
  analogWrite(PWM_B, 200); // motor 2 speed 200 
  delay(1600); // I am going to tweak this number to get as close to a 90 degree turn as possible
  
  digitalWrite(BRAKE_A, HIGH); // motor 1 brake
  digitalWrite(BRAKE_B, HIGH); // motor 2 brake
  
  }
}

Wouldn't global scope for the sensor be more sensible?

Is it not in the global scope? I am not too familiar with those terms. Is global scope things listed outside of void setup and void loop?

Is global scope things listed outside of void setup and void loop?

Yes. Creating a new instance of the class on every pass through loop() doesn't make sense.

I am not too familiar with those terms.

You really need to become familiar with them.