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
#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
}
}
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?
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?
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);
}
}