Trying to switch a relay with a Sharp IR sensor, keep getting error

Hi Folks,

I’m currently trying to switch a 5v relay with a Sharp IR sensor. The problem is that I keep getting an error message when I try to upload the code. Any help on this would be greatful. Below is the error message I keep getting.

Arduino: 1.8.5 (Mac OS X), Board: "Arduino/Genuino Uno"
In file included from /Users/liammadden/Documents/Arduino/Sharp IR and relay/sharp_IR_and_relay_1/sharp_IR_and_relay_1.ino:1:0:
/Users/liammadden/Documents/Arduino/libraries/SharpIR/src/SharpIR.h: In function ‘void loop()’:
/Users/liammadden/Documents/Arduino/libraries/SharpIR/src/SharpIR.h:40:13: error: ‘uint8_t SharpIR::distance’ is private
* uint8_t distance;
__
^__
sharp_IR_and_relay_1:26: error: within this context
__
int dis=SharpIR.distance(); // this returns the distance to the object you’re measuring*__
* ^*
sharp_IR_and_relay_1:26: error: expression cannot be used as a function
* int dis=SharpIR.distance(); // this returns the distance to the object you’re measuring*
* ^*
exit status 1
within this context
This report would have more information with
"Show verbose output during compilation"
option enabled in File → Preferences.[/b]
Can anyone point me in the right direction as to what is going on here.
Thanks folks

An here is the error with verbose output ticked:

Arduino: 1.8.5 (Mac OS X), Board: “Arduino/Genuino Uno”

/Applications/Arduino 2.app/Contents/Java/arduino-builder -dump-prefs -logger=machine -hardware /Applications/Arduino 2.app/Contents/Java/hardware -hardware /Users/liammadden/Library/Arduino15/packages -hardware /Users/liammadden/Documents/Arduino/hardware -tools /Applications/Arduino 2.app/Contents/Java/tools-builder -tools /Applications/Arduino 2.app/Contents/Java/hardware/tools/avr -tools /Users/liammadden/Library/Arduino15/packages -built-in-libraries /Applications/Arduino 2.app/Contents/Java/libraries -libraries /Users/liammadden/Documents/Arduino/libraries -fqbn=arduino:avr:uno -vid-pid=0X2A03_0X0043 -ide-version=10805 -build-path /var/folders/sg/xbwln7f53sgb6r1dgcj_gsk40000gn/T/arduino_build_964502 -warnings=none -build-cache /var/folders/sg/xbwln7f53sgb6r1dgcj_gsk40000gn/T/arduino_cache_624169 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=/Users/liammadden/Library/Arduino15/packages/arduino/tools/arduinoOTA/1.2.1 -prefs=runtime.tools.avrdude.path=/Users/liammadden/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino14 -prefs=runtime.tools.avr-gcc.path=/Users/liammadden/Library/Arduino15/packages/arduino/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2 -verbose /Users/liammadden/Documents/Arduino/Sharp IR and relay/sharp_IR_and_relay_1/sharp_IR_and_relay_1.ino
/Applications/Arduino 2.app/Contents/Java/arduino-builder -compile -logger=machine -hardware /Applications/Arduino 2.app/Contents/Java/hardware -hardware /Users/liammadden/Library/Arduino15/packages -hardware /Users/liammadden/Documents/Arduino/hardware -tools /Applications/Arduino 2.app/Contents/Java/tools-builder -tools /Applications/Arduino 2.app/Contents/Java/hardware/tools/avr -tools /Users/liammadden/Library/Arduino15/packages -built-in-libraries /Applications/Arduino 2.app/Contents/Java/libraries -libraries /Users/liammadden/Documents/Arduino/libraries -fqbn=arduino:avr:uno -vid-pid=0X2A03_0X0043 -ide-version=10805 -build-path /var/folders/sg/xbwln7f53sgb6r1dgcj_gsk40000gn/T/arduino_build_964502 -warnings=none -build-cache /var/folders/sg/xbwln7f53sgb6r1dgcj_gsk40000gn/T/arduino_cache_624169 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=/Users/liammadden/Library/Arduino15/packages/arduino/tools/arduinoOTA/1.2.1 -prefs=runtime.tools.avrdude.path=/Users/liammadden/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino14 -prefs=runtime.tools.avr-gcc.path=/Users/liammadden/Library/Arduino15/packages/arduino/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2 -verbose /Users/liammadden/Documents/Arduino/Sharp IR and relay/sharp_IR_and_relay_1/sharp_IR_and_relay_1.ino
Using board ‘uno’ from platform in folder: /Users/liammadden/Library/Arduino15/packages/arduino/hardware/avr/1.6.23
Using core ‘arduino’ from platform in folder: /Users/liammadden/Library/Arduino15/packages/arduino/hardware/avr/1.6.23
Detecting libraries used…
“/Users/liammadden/Library/Arduino15/packages/arduino/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR “-I/Users/liammadden/Library/Arduino15/packages/arduino/hardware/avr/1.6.23/cores/arduino” “-I/Users/liammadden/Library/Arduino15/packages/arduino/hardware/avr/1.6.23/variants/standard” “/var/folders/sg/xbwln7f53sgb6r1dgcj_gsk40000gn/T/arduino_build_964502/sketch/sharp_IR_and_relay_1.ino.cpp” -o “/dev/null”
“/Users/liammadden/Library/Arduino15/packages/arduino/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR “-I/Users/liammadden/Library/Arduino15/packages/arduino/hardware/avr/1.6.23/cores/arduino” “-I/Users/liammadden/Library/Arduino15/packages/arduino/hardware/avr/1.6.23/variants/standard” “-I/Users/liammadden/Documents/Arduino/libraries/SharpIR/src” “/var/folders/sg/xbwln7f53sgb6r1dgcj_gsk40000gn/T/arduino_build_964502/sketch/sharp_IR_and_relay_1.ino.cpp” -o “/dev/null”
“/Users/liammadden/Library/Arduino15/packages/arduino/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR “-I/Users/liammadden/Library/Arduino15/packages/arduino/hardware/avr/1.6.23/cores/arduino” “-I/Users/liammadden/Library/Arduino15/packages/arduino/hardware/avr/1.6.23/variants/standard” “-I/Users/liammadden/Documents/Arduino/libraries/SharpIR/src” “/Users/liammadden/Documents/Arduino/libraries/SharpIR/src/SharpIR.cpp” -o “/dev/null”
Generating function prototypes…
“/Users/liammadden/Library/Arduino15/packages/arduino/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR “-I/Users/liammadden/Library/Arduino15/packages/arduino/hardware/avr/1.6.23/cores/arduino” “-I/Users/liammadden/Library/Arduino15/packages/arduino/hardware/avr/1.6.23/variants/standard” “-I/Users/liammadden/Documents/Arduino/libraries/SharpIR/src” “/var/folders/sg/xbwln7f53sgb6r1dgcj_gsk40000gn/T/arduino_build_964502/sketch/sharp_IR_and_relay_1.ino.cpp” -o “/var/folders/sg/xbwln7f53sgb6r1dgcj_gsk40000gn/T/arduino_build_964502/preproc/ctags_target_for_gcc_minus_e.cpp”
“/Applications/Arduino 2.app/Contents/Java/tools-builder/ctags/5.8-arduino11/ctags” -u --language-force=c++ -f - --c+±kinds=svpf --fields=KSTtzns --line-directives “/var/folders/sg/xbwln7f53sgb6r1dgcj_gsk40000gn/T/arduino_build_964502/preproc/ctags_target_for_gcc_minus_e.cpp”
Compiling sketch…
“/Users/liammadden/Library/Arduino15/packages/arduino/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR “-I/Users/liammadden/Library/Arduino15/packages/arduino/hardware/avr/1.6.23/cores/arduino” “-I/Users/liammadden/Library/Arduino15/packages/arduino/hardware/avr/1.6.23/variants/standard” “-I/Users/liammadden/Documents/Arduino/libraries/SharpIR/src” “/var/folders/sg/xbwln7f53sgb6r1dgcj_gsk40000gn/T/arduino_build_964502/sketch/sharp_IR_and_relay_1.ino.cpp” -o “/var/folders/sg/xbwln7f53sgb6r1dgcj_gsk40000gn/T/arduino_build_964502/sketch/sharp_IR_and_relay_1.ino.cpp.o”
In file included from /Users/liammadden/Documents/Arduino/Sharp IR and relay/sharp_IR_and_relay_1/sharp_IR_and_relay_1.ino:1:0:
/Users/liammadden/Documents/Arduino/libraries/SharpIR/src/SharpIR.h: In function ‘void loop()’:
/Users/liammadden/Documents/Arduino/libraries/SharpIR/src/SharpIR.h:40:13: error: ‘uint8_t SharpIR::distance’ is private
uint8_t distance;
^
sharp_IR_and_relay_1:26: error: within this context
int dis=SharpIR.distance(); // this returns the distance to the object you’re measuring
^
sharp_IR_and_relay_1:26: error: expression cannot be used as a function
int dis=SharpIR.distance(); // this returns the distance to the object you’re measuring
^
Using library SharpIR at version 1.1.0 in folder: /Users/liammadden/Documents/Arduino/libraries/SharpIR
exit status 1
within this context

Where is YOUR code?

And where did you get the SharpIR library?

Here is the code below

#include <SharpIR.h>


#define ir A0
#define model 1080
// ir: the pin where your sensor is attached
// model: an int that determines your sensor:  1080 for GP2Y0A21Y
//                                            20150 for GP2Y0A02Y
//                                            (working distance range according to the datasheets)

SharpIR SharpIR(ir, model);
int relay =  7;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
   
  pinMode( relay, OUTPUT );
  digitalWrite( relay, HIGH );
}

void loop() {
  delay(200);   

  unsigned long pepe1=millis();  // takes the time before the loop on the library begins

  int dis=SharpIR.distance();  // this returns the distance to the object you're measuring


  Serial.print("Mean distance: ");  // returns it to the serial monitor
  Serial.println(dis);
  
  unsigned long pepe2=millis()-pepe1;  // the following gives you the time taken to get the measurement
  Serial.print("Time taken (ms): ");
  Serial.println(pepe2);  
  
 if ((dis > 10) && ( dis< 40)) {
    digitalWrite(relay, LOW);
    delay(15000);// leave on for 15 seconds
    digitalWrite(relay, HIGH); // Turn Relay OFF

   
  }


}

Thanks for getting back to me Paul S, I got the library here:

distance is not a function. There IS a public function, getDistance(), that the example uses. Why are you trying to get the data in the private field, as though the member was a function?

Apologies Paul S, I'm new to coding, can you explain this simply? Should I remove that line of code?

Should I remove that line of code?

Yes. And all the lines before it, and all the lines after it.

Then, look at the example that came with the library, and see how your code used to differ from the example.

Thanks Paul S, I got the code to upload, but now I have another issue. Essentially I have the relay turning on for 5 seconds, it immediatly switches off, then turns on again a for 5 seconds.

What I’m looking to achieve is when the hand is between 10cm and 40cm away from the sensor the relay turns on. Its stays on for 5 seconds then turns off. I don’t want it to turn on again until the sensor senses an object within 10cm and 40cm.

Here is the code at this point

#include <SharpIR.h>


#define ir A0
#define model 1080
// ir: the pin where your sensor is attached
// model: an int that determines your sensor:  1080 for GP2Y0A21Y
//                                            20150 for GP2Y0A02Y
//                                            (working distance range according to the datasheets)

//SharpIR SharpIR(ir, model);
SharpIR sensor(GP2YA41SK0F, A5);
int relay =  7;
int SharpIR =  A5;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(7, OUTPUT );
  digitalWrite( relay, HIGH );
}

void loop() {
  delay(900);

  unsigned long pepe1 = millis(); // takes the time before the loop on the library begins

  int distance = sensor.getDistance(); // this returns the distance to the object you're measuring


  Serial.print("Mean distance: ");  // returns it to the serial monitor
  Serial.println(distance);

  unsigned long pepe2 = millis() - pepe1; // the following gives you the time taken to get the measurement
  Serial.print("Time taken (ms): ");
  Serial.println(pepe2);

  if ((distance > 8) && ( distance < 40)) {
    digitalWrite(relay, LOW);
    delay(5000);// leave on for 5 seconds
    digitalWrite(relay, HIGH); // Turn Relay OFF


  }

Is there a way to make the relay turn on when sensed by the sharp ir?

Thanks

void loop() {
  delay(900);

Pissing away almost a second on every pass through loop() is a really poor idea.

  Serial.print("Mean distance: ");  // returns it to the serial monitor
  Serial.println(distance);

Are you sure the distance is mean? Perhaps it just got up on the wrong side of the bed today.

I don't want it to turn on again until the sensor senses an object within 10cm and 40cm.

If something is still in front of the sensor when the relay is turned off, your code will immediately turn it on again.

If you want to not turn the relay on again until there is a reading outside of that range, and the reading is again in that range, create a boolean variable, initialized to false. Set it to true when the reading is in the range, after the relay has been turned off. Set it to false when the reading isn't in the desired range.

Then, before turning the relay on, check the value in the boolean variable. If it is false, turn the relay on. Otherwise, do nothing.

Thanks Paul S, appreciate you getting back to me. I have it working so as when my hand moves over the sensor the light turns on and stays on for 5 seconds. I’m just wondering how I would go about coding it, so as if I moved my hand away from the sensor the light would turn off, (as in not stay on for 5 seconds), any help would be greatful, thanks. Here’s the code thus far:

#include <SharpIR.h>


#define ir A5
#define model 1080
// ir: the pin where your sensor is attached
// model: an int that determines your sensor:  1080 for GP2Y0A21Y
//                                            20150 for GP2Y0A02Y
//                                            (working distance range according to the datasheets)

//SharpIR SharpIR(ir, model);
SharpIR sensor(GP2YA41SK0F, A5);
int relay =  7;
int SharpIR =  A5;
boolean sharpIR = false;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(7, OUTPUT );
  digitalWrite( relay, HIGH );
}

void loop() {
  delay(2000);

  unsigned long pepe1 = millis(); // takes the time before the loop on the library begins

  int distance = sensor.getDistance(); // this returns the distance to the object you're measuring




  Serial.print(" distance: ");  // returns it to the serial monitor
  Serial.println(distance);

  unsigned long pepe2 = millis() - pepe1; // the following gives you the time taken to get the measurement
  Serial.print("Time taken (ms): ");
  Serial.println(pepe2);

  if ((distance < 10) && ( distance > 1)) {
    digitalWrite(relay, LOW);
    delay(5000);// leave on for 5 seconds
    digitalWrite(relay, HIGH); // Turn Relay OFF
    //delay(000);


  }


}

Here’s the code thus far:

void loop() {
  delay(2000);

Why are you stuffing your head in the sand for 2 seconds on EVERY pass through loop()?

    digitalWrite(relay, LOW);
    delay(5000);// leave on for 5 seconds
    digitalWrite(relay, HIGH); // Turn Relay OFF

If you can figure out how to turn the relay on for 5 seconds, and then off, surely you can figure out how to just turn it on.

If you can figure out when to turn it on (when the distance is between 1 and 10 exclusive, surely you can figure out when to turn it off.

if(distance > 1 && distance < 10) // because that's how I think
{
   digitalWrite(relay, LOW);
}
else
{
   digitalWrite(relay, HIGH);
}

Ok, got it, thanks a million for you help on this Paul S, really appreciate your patience.