Pages: 1 [2]   Go Down
Author Topic: New with sensors  (Read 1080 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So i think what I'm doing is sending a voltage to the sensor in order for it to read a distance?  (which seems a bit jumpy)


And then how do I get it to trigger the lights?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int IRpin = 0;
    // select the input pin for the potentiometer
int fanPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

// analog pin for reading the IR sensor
void setup() {
  Serial.begin(9600); 
 // start the serial port
}
void loop() {
  float volts = analogRead(IRpin)*0.0048828125;   // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
  float distance = 65*pow(volts, -1.10);          // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
  Serial.println(distance);                       // print the distance
  delay(100);   
sensorValue = analogRead(IRpin);   
 

if (sensorValue >75)

digitalWrite(fanPin, HIGH);
delay(5000);
digitalWrite(fanPin, LOW);


}





The problem im having with this is it not triggering at the same spot (where im holding my hand) each time
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8442
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry but I can't keep up, that's about 4 totally different pieces of code, we still don't know what values the sensor is returning, and now we have complicated floating point maths just (I gather) to turn on a LED/fan when something gets close.

Quote
sending a voltage to the sensor
You are (I assume, still haven't seen a schematic) supplying the sensor with 5v but you aren't "sending" it anything, you are reading the sensor's output.

Quote
And then how do I get it to trigger the lights?
What lights, a minute ago it was a fan. Or do you mean the LED on pin 13?

State the project goals clearly, is it movement or proximity you need to measure? Does the fan/whatever start when X happens and stop 5 seconds later even if X is still happening, or should it start when X happens then stop 5 seconds after X stops. etc etc.

EDIT: Make that 5 pieces of code smiley
_____
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8442
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What's the floating point for?

What values are being received from the sensor?

Quote
is it not triggering at the same spot (where im holding my hand) each time
Check the data sheet

Quote
Detection Accuracy @ 80 cm: ±10 cm

It's not very accurate. Is you hand within 20cm of the same place each time?

It doesn't say what the repeatability is but this is obviously not an accurate sensor.


______
Rob

« Last Edit: November 15, 2011, 10:13:24 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Yea sorry I'm probably jumping around and I think Ive just totally confused myself too.


Okay this fan/led pin basically its going in pin 13  (I want it to be a fan but at the moment I am usage an LED as I have not got the fan yet)


What I want to achieve is when ANY motion is detected, I want to trigger the fan/led to go on for 5 seconds

I don't care about distance or anything.



Basically I'm completely clueless.

What do I have to do to make the sensor work and have it trigger the fan/led for 5 seconds.



Sorry
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8442
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's getting late here but this might have a chance of working

Code:
int IRpin = 0;
    // select the input pin for the potentiometer
int fanPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

#define TRIG_VAL 100  // play with this value

void setup() {
  Serial.begin(9600); 
}
void loop() {
sensorValue = analogRead(IRpin);   
 
if (sensorValue > TRIG_VAL) {
digitalWrite(fanPin, HIGH);
delay(5000);
digitalWrite(fanPin, LOW);
while (analogRead(IRpin) > TRIG_VAL); // now wait for the object to leave before allowing a repeat performance
}
}

It's based on my reading of the data sheet, the TRIG_VAL should be a value just over the reading you get with nothing in front of the sensor. That's why I wanted to know the values.

This is not very clever code as it stops the processor from doing anything else, but it should do for now.

Note also that the sensor output dives at about 15cm, that will be difficult to deal with because that will look the same as the object moving away really fast.

______
Rob
« Last Edit: November 15, 2011, 11:08:24 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey thanks this works really well thank you so much!!
Logged

Pages: 1 [2]   Go Up
Jump to: