Hi I would like to ask why i always get same value distance even if the object is near or far as long as it is detected by the IR sensor? TIA for the reply.
Based on the infromation provided, it is either a programming problem or a hardware setup problem.
This is my code:
#include<Servo.h>
int proxSensorPin= A0; // Proximity sensor pin
int proxSensorValue= 0; // variable to store the value coming from the sensor
int irSensorPin = A1; //IR sensor pin
int irSensorValue;
Servo servo1;
void setup() {
// declare the ledPin as an OUTPUT:
Serial.begin(9600);
servo1.attach(11);
}
void loop() {
// read the value from the sensor:
proxSensorValue= analogRead(proxSensorPin);
irSensorValue= analogRead(irSensorPin);
Serial.print("\nPlasticsensor = ");
Serial.println(proxSensorValue);
Serial.print("Papersensor = ");
Serial.println(irSensorValue);
// turn the ledPin on if triggered
//
if (proxSensorValue <200){
Serial.println("Plastic Detected");
servo1.write(20);
}
else if (irSensorValue <100){
Serial.println("Paper Detected");
servo1.write(180);
}
else{
Serial.print("\nNo Item Detected");
servo1.write(100);
}
delay(1000);
}
Although the Arduino has multiple input pins, it only has one A/D converter. When reading multiple analog pins, you need to pre-read and throw away the value read to ensure the sample circuit has acquired and settled on the new analog pin.
junkread= analogRead(proxSensorPin);
proxSensorValue= analogRead(proxSensorPin);
junkread= analogRead(irSensorPin);
irSensorValue= analogRead(irSensorPin);
It could be a hardware problem as well.
Thanks for replying sir it will really help me in my project but i want to ask where to put those lines you replied to me
Put them where you have the analogRead instructions.
Change
proxSensorValue= analogRead(proxSensorPin);
irSensorValue= analogRead(irSensorPin);
to
junkread= analogRead(proxSensorPin);
proxSensorValue= analogRead(proxSensorPin);
junkread= analogRead(irSensorPin);
irSensorValue= analogRead(irSensorPin);
Don't forget to declare junk_read as a local int variable.