Sorry. I am new.
Please edit your post to enclose the code in code tags.
See the "How to use this forum" post.
Which sensors are you trying to "calibrate"? What IS your problem, exactly? Aside from not being able to read and properly post code, that is.
Here is my schematic. I already have made one of these sensors. Oh and sorry again Paul, no need to be rude. I just am not familiar with the forum yet. Thanks.
I just am not familiar with the forum yet.
Then please take a few minutes to read and follow the directions in the "How to use this forum" post.
What is your sensor supposed to do, and why do you want to calibrate it?
It is an IR proximity sensor. I am attempting to make a table to control LEDS when something is in proximity of the IR sensor. The calibration cancels out ambient light so there is no interfearence, I was trying to figure out how to calibrate multiple sensors.
This is the code I have used to get the single proximity sensor to work for me. I am still learning basic functions for the arduino. Would I have to write multiple functions to control multiple sensors?
int IRpin = A0; // IR photodiode on analog pin A0
int IRemitter = 2; // IR emitter LED on digital pin 2
int ambientIR; // variable to store the IR coming from the ambient
int obstacleIR; // variable to store the IR coming from the object
int value[10]; // variable to store the IR values
int distance; // variable that will tell if there is an obstacle or not
void setup() {
Serial.begin(9600); // initializing Serial monitor
pinMode(IRemitter, OUTPUT); // IR emitter LED on digital pin 2
digitalWrite(IRemitter, LOW); // setup IR LED as off
pinMode(11, OUTPUT); // LED in digital pin 11
}
void loop() {
distance = readIR(5); // calling the function that will read the distance and passing the "accuracy" to it
Serial.println(distance); // writing the read value on Serial monitor
led(); // LED activation
}
int readIR(int times) {
for (int x = 0; x < times; x++) {
digitalWrite(IRemitter, LOW); // turning the IR LEDs off to read the IR coming from the ambient
delay(1); // minimum delay necessary to read values
ambientIR = analogRead(IRpin); // storing IR coming from the ambient
digitalWrite(IRemitter, HIGH); // turning the IR LEDs on to read the IR coming from the obstacle
delay(1); // minimum delay necessary to read values
obstacleIR = analogRead(IRpin); // storing IR coming from the obstacle
value[x] = ambientIR - obstacleIR; // calculating changes in IR values and storing it for future average
}
for (int x = 0; x < times; x++) { // calculating the average based on the "accuracy"
distance += value[x];
}
return (distance / times); // return the final value
}
//-- Function to turn on an LED --//
void led() {
if (distance > 3) { // if the obstacle is too close
digitalWrite(11, HIGH);
}
else { // off if there is no obstacle
digitalWrite(11, LOW);
}
}
Would I have to write multiple functions to control multiple sensors?
No. Write one function and call it with sensor-specific parameters.
Do you mean something like this?
distance = readIR1(5);
distance = readIR2(5);
distance = readIR3(5);
....
sorry...that was the wrong way again!!!
Kal22378:
sorry...that was the wrong way again!!!
So, have you discovered the right way?
Kal22378:
Do you mean something like this?distance = readIR1(5);
distance = readIR2(5);
distance = readIR3(5);
No, those are three different functions. Write one function and pass it a parameter that tells which sensor to read.
distance = readIR(1);
distance = readIR(2);
distance = readIR(3);
Write one function and pass it a parameter that tells which sensor to read.
Of course, saving the results from making three function calls in the same variable doesn't make a lot of sense.
So do I need to read all of the sensors in this function?....
int readIR(int times) {
for (int x = 0; x < times; x++) {
digitalWrite(IRemitter, LOW); // turning the IR LEDs off to read the IR coming from the ambient
delay(1); // minimum delay necessary to read values
ambientIR = analogRead(IRpin); // storing IR coming from the ambient
digitalWrite(IRemitter, HIGH); // turning the IR LEDs on to read the IR coming from the obstacle
delay(1); // minimum delay necessary to read values
obstacleIR = analogRead(IRpin); // storing IR coming from the obstacle
value[x] = ambientIR - obstacleIR; // calculating changes in IR values and storing it for future average
}
for (int x = 0; x < times; x++) { // calculating the average based on the "accuracy"
distance += value[x];
}
return (distance / times);
Im trying to learn Paul...thanks!!!
So do I need to read all of the sensors in this function?....
The idea being proposed was that the value passed to the function was the pin number to read from, not the number of times to read the sensor connected to the pin. It was assumed that you read the pin the same number of times, regardless of which pin it was.
Understanding and applying arrays will make this a piece of cake.
Yes, I didn't know the 5 was the number of times.
My example functions should have taken two arguments, with one of them being the pin and one being the number of times.
The fundamental lesson is that if you ever catch yourself putting numbers on the beginning or end of variable names or function names, then there is a better way to do it. You have to make those numbers mean something that the compiler can work with. It can't see variable names.
Does value x in this part of the function :
int readIR(int times) {
for (int x = 0; x < times; x++) {
digitalWrite(IRemitter, LOW); // turning the IR LEDs off to read the IR coming from the ambient
delay(1); // minimum delay necessary to read values
ambientIR = analogRead(IRpin); // storing IR coming from the ambient
digitalWrite(IRemitter, HIGH); // turning the IR LEDs on to read the IR coming from the obstacle
delay(1); // minimum delay necessary to read values
obstacleIR = analogRead(IRpin); // storing IR coming from the obstacle
value[x] = ambientIR - obstacleIR; // calculating changes in IR values and storing it for future average
}
refer to this value in the declaration?
int value[10]; // variable to store the IR values