Hi can anyone please check this sketch and make it work as i am unable to fix the error.
kind regards
/*with this sketch
* IR sensor will keep the LED on all time and when object is detected
* the light will be off and start again.
*
*/
void setup() {
// put your setup code here, to run once:
pinMode(A0,INPUT); // IR sensor reading pin
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(11,OUTPUT); // initializing LED to pin 11
digitalWrite(A2,HIGH);
digitalWrite(A1,LOW);
Serial.begin(9600); // open Serial port
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(analogRead(A0)); // reads the analog signals from IR sensor
delay(100);
if(analogRead(A0) < 250) // if sensor detects an object
{digitalWrite(11,LOW); // LED is OFF [ or motors are off ]
delay (2000);
digitalWrite (11,HIGH); // LED starts again
delay (3000);} // for 3 seconds so that object passes the sensor
else
digitalWrite(11,HIGH); // LED is always on [ or motor are always on ]
}
This is what the IR sensor and arduino uno need to do...
when IR sensor encounters an object the reading goes below 250
if analogRead is <250 ..the LED stops and Count = 1.
when seconds objects comes to the IR sensor count = 2
when 3rd object comes to the IR sensor count = 3
and when 4th object comes to the IR sensor count = 4.
when count == 4..the inbuilt LED starts
and when the 4th object moves past the IR sensor the count = 4
when the 3rd object moves past the IR sensor the count = 3
when the 2nd object moves past the IR sensor the count = 2
when the 1st object moves past the IR sensor the count = 1
and when count == 1..the LED starts again
The value of 0-4 is the total number of objects..
its the count limit of 4 ..
Hope that make sense.
kind regards
Your logic and your code don't line up. The LED is always ON except for 2 seconds when A0 is less than 250 - the else statement is irrelevant.
thank you DKWATSON for your reply
which part of the sketch is the "logic" section and how can i fix it?
kind regards
The logic is what you wrote in your second post describing what you think it should do.
There is nothing in your code to be 'fixed' as it appears to be for some completely different purpose - flashing an LED based on a reading of A0.
Thank you Mr DKWATSON for replying to my post.
can you please re-write the code so it reflects the logic on my second post.
i dont know how to achieve that..
kind regards
No.
You re-write the code and if it doesn't work, come back for a second opinion. This is not a code-writing service. If you feel that your coding skills are not up to scratch, we can direct you to several very useful resources that will help your understanding. Even within the Forums there is an excellent resource here that you should perhaps have a look through.
Thank you Mr DKWATSON for your help.
/*with this sketch
- IR sensor will keep the LED on and when object is detected
- the light will be off and after delay start again.
- The Sketch will also count how many time the led is being off
- THIS IS TEST CODE
*/
int redLed = 2;
int yellowLed = 3;
int greenLed = 4;
int led = 11; // declaring LED to pin 11 of Arduino.
int ledOff; // declaring LED Off open integer .
int count = 0;
void setup() // put your setup code here, to run once:
{
Serial.begin (9600);
pinMode(redLed,OUTPUT);
pinMode(yellowLed,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(A0,INPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(led,OUTPUT);
digitalWrite(A2,HIGH);
digitalWrite(A1,LOW);
}
void loop ()
{
if(analogRead(A0) < 250) // less then 250 means there is an obstacle
{
digitalWrite(led,LOW); // LED will be OFF
count++;
delay (5000); // LED off for 5 seconds
Serial.println (count);
}
else
{ for (digitalWrite(A0)==LOW);
{digitalWrite (led,HIGH); // LED ON again
delay (2000);} // for 3 seconds
}
}
That doesn't compile. Therefore it doesn't work.
Among other errors, what do you think this line is going to do?
{ for (digitalWrite(A0)==LOW);
Steve