I am trying to interface a proximity sensor with atmega328. I know that the proximity sensor is working and I can see the serial port values change when distance,etc.
However, I want to make it such that, depending on the distance, it would turn on an LED that I have connected.
The problem is that I wrote the code below and it just keeps the LED always ON.
The circuit is pretty simple so I am sure there is no error there..
Can any of the coding experts look at this and guide me please?
Thanks
Angie..
Here is the code
int sensePin=5; // proximity sensor output obtained at this pin
int ledPin1 = 4; // LED connected to digital pin 4
int a=0; //just a variable to check the current value of sensePin
void setup()
{
pinMode(ledPin1,OUTPUT);
// sets the digital pin as output
Serial.begin(9600);
digitalWrite(ledPin1,LOW);
delay(5000);
}
void loop()
{
a = analogRead(sensePin);
if(a<=50)
{
digitalWrite(ledPin1, HIGH); // sets the LED on
delay(2000);
}
else {
digitalWrite(ledPin1,LOW);
delay(2000);
}
Serial.println(analogRead(sensePin));
delay(800);
}
Read a value. Make a decision based on that value. 2.8 seconds later print something else. Hardly makes sense to me.
WHAT?
I don't really know where you got that code segment from.. lol
if (a<=50), turn the LED ON else keep it OFF.
That serial.printIn(analogRead(sensePin)) statement is just to monitor the COM port output and that is not inside the if statement.
A cup of coffee might help
I don't really know where you got that code segment from.. lol
Those statements are not contiguous in your code, but all I snipped was the if block and the else statement and block.
That serial.printIn(analogRead(sensePin)) statement is just to monitor the COM port output and that is not inside the if statement.
True, but it does NOT print the value that was the basis for the decision to turn the LED on or off.
Serial.println(a); would have done that.
Instead, you are reading another value, 2 seconds after having read the previous value, and assuming that they are the same value.
A cup of coffee might help
Wouldn't do anything for me...
ok, lets forget about the print statement.. it doesn't really matter what is printed. I can delete those statements. What I want is that the LED should turn ON when the signal on the analog pin is less than 50. That is not happening.
What I want is that the LED should turn ON when the signal on the analog pin is less than 50. That is not happening.
Then, perhaps the issue is with how the LED is wired. Just run the blink sketch, using pin
int ledPin1 = 4; // LED connected to digital pin 5
well, whatever the correct pin number is.
Does the LED work, then?
Paul,
As you can see, in the set up part of my code, i am turning the LED OFF, it stays off. If I turn it on there, it turns on but it doesnt respond thereafter to the conditional statements.. It shld turn ON/OFF when I see values on the COM port that are less than/more than 50 - that never happens.
It shld turn ON/OFF when I see values on the COM port that are less than/more than 50
No,there you're putting the cart before the horse - the values seen on the COM port have very little to do with the criteria for the LED state - see the early comments above.
const int sensePin=5; // proximity sensor output obtained at this pin
const int ledPin1 = 4; // LED connected to digital pin 5
void setup()
{
pinMode(ledPin1,OUTPUT);
digitalWrite(ledPin1,LOW);
Serial.begin(9600);
delay(5000);
}
void loop()
{
int a = analogRead(sensePin);
Serial.println(a);
if(a<=50) {
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1,LOW);
}
delay(2800);
}
a = analogRead(sensePin);
if(a<=50)
{
digitalWrite(ledPin1, HIGH); // sets the LED on
delay(2000);
Whatever else it does, you are going to have to wait 2 seconds before the LED turns on or off again. What is the purpose of that?
Here:
a = analogRead(sensePin);
...
delay(2000);
...
Serial.println(analogRead(sensePin));
As PaulS said, you are NOT displaying the value you are making a decision on. Now of course you can take the print out. But this is supposed to help you with debugging right? If you get a confusing debugging message, the technique is not to delete the confusing message. It is to work out what is going on.
The purpose to wait for 2secs is to make sure that the LED is turning on. If I do not put a delay, i wont be able to detect the ON/OFF operation. I can reduce the delay to 1sec.
I am not getting any random values. The proximity sensor and hence the values displayed are perfectly alright. What PaulS says about the values being different is right but ultimately, as I bring my hand closer to the proximity sensor, the LED shld turn ON and when I take it away it should turn OFF -- That doesn't happen.
the LED Is connected to the digital pin where as the sensor is on the analog pin (someone asked me about this earlier)
It shld turn ON/OFF when I see values on the COM port that are less than/more than 50
No,there you're putting the cart before the horse - the values seen on the COM port have very little to do with the criteria for the LED state - see the early comments above.
const int sensePin=5; // proximity sensor output obtained at this pin
const int ledPin1 = 4; // LED connected to digital pin 5
void loop()
{
int a = analogRead(sensePin);
Serial.println(a);
if(a<=50) {
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1,LOW);
}
delay(2800);
}
I agree that the com port values may not be the same that are used to make the decision. Now if I keep my thumb on the proxi sensor for say 15secs, the sensor and the arduino code has enuf time to detect that and light the LED right? Under such circumstances, it hardly matters what the value of the proxi sensor is on the COM port. Similarly, when I take my thumb off the proxi sensor and make sure there is no obstacle in its path, the LED shld be off. Doesnt happen!
So, add some more meaningful debug.
Write a simple sketch to blink the LED, to test the LED connections.
You're the one sitting in front of the hardware.
Ok, it works now.. I did it by reducing that delay to 1sec. Now the response time is less and the result is quickly displayed by the LED turning ON and OFF as the case may be. Thank you very much for helping me solve this puzzle..
It does look good. PaulS asked earlier if you've confirmed the LED works? Just to eliminate that, and the wiring, and D4 from the equation, change the LED to D13 to trigger the onboard LED.
// const int ledPin1 = 4
const int ledPin1 = 13
Do you get the result you are looking for, firing off the on-board status LED?
crusaider:
The purpose to wait for 2secs is to make sure that the LED is turning on. If I do not put a delay, i wont be able to detect the ON/OFF operation. I can reduce the delay to 1sec.
No you don't. When you turn a light switch on, you don't wait 2 seconds to detect it went on, right?
Since the LED is supposed to turn on/off depending on proximity, no delay is required. If the hand (or cat or whatever) is close the LED is on, if it moves away it goes off. No delays required.