How to diminish the flickering of the LED?Is it even possible?

I had checked my whole circuit.The components are not short circuited.How I want it to work? When i program it, the Green LEDs lights up whereas the Red LEDs supposedly not to turn on. As I touch the sensor, the Red LEDs then turn on followed by the Green LEDs being turn off. However, it did not functioned as I wanted it to be. When I program it, the Green LEDs did turned on but the Red LEDs somewhat flickered. I am not sure what is the cause of this. Can someone help me?

Nardilla: Can someone help me?

Probably, but you will need to post your code and a schematic of the circuit, as well as details of "the sensor".

This is the program code:

#define Red 7
#define Green 8
int sensorState = 0;

void setup()
{
pinMode(3,INPUT); // distance sensor
pinMode(Red,OUTPUT); // red LEDs
pinMode(Green,OUTPUT); // green LEDs
}

void loop()
{
sensorState = analogRead(3);
// set sensor(pinMode 3) as analog

if (sensorState > 100 && sensorState < 540)
{// the sensor does not sense
digitalWrite(Red, LOW); // red LEDs
digitalWrite(Green, HIGH); // green LEDs

}

else if (sensorState <= 100)
{
digitalWrite(Green, LOW); // green LEDs
digitalWrite(Red, HIGH); // red LEDs
}
}

The type of distance sensor I use is SHARP.

d6af361b766436f41f8238b349c2e8e969a16079.jpg
bdc10e2b2057f9ea4fe99c1c4a5663289b036ee1.jpg
5fcce586cdb238a472c5562f7d5810f04c119a44.jpg

Hi, always put your code in code tags please!

Paul

Follow the instructions for posting.

Use "Auto Format" on your code before you post it.

The LEDs are flickering because your sensor output is fluctuating. Try a sketch which simply continuously prints the analog values to the serial monitor and see what it is doing.

Since you have elected not to explain to us what this sensor actually is, we cannot help further at this point.

#define Red 7
#define Green 8
int sensorState = 0;


void setup()
{
  pinMode(3, INPUT); //  distance sensor
  pinMode(Red, OUTPUT); // red LEDs
  pinMode(Green, OUTPUT); // green LEDs
}

void loop()
{
  sensorState = analogRead(3);
  // set sensor(pinMode 3) as analog

  if (sensorState > 100 && sensorState < 540)
  { // the sensor does not sense
    digitalWrite(Red, LOW); // red LEDs
    digitalWrite(Green, HIGH); // green LEDs

  }

  else  if (sensorState <= 100)
  {
    digitalWrite(Green, LOW); // green LEDs
    digitalWrite(Red, HIGH); // red LEDs
  }
}

The type of sensor that I used is the IR distance sensor(SHARP).

Nardilla: The type of sensor that I used is the IR distance sensor(SHARP).

I'm pretty sure Sharp has a whole lot of different sensors: perhaps post a link to the datasheet.

Paul_B's idea is a good one: use serial print to output the sensor's value so you can see what it's actually saying.

Maybe the output's hovering around say 99-101 and causing the LEDs to switch.

Nardilla: The type of sensor that I used is the IR distance sensor(SHARP).

There are at least 5 types according to this site. Which do you have?

Does the flickering happen at all times or only when an object is at a certain distance in front of the sensor?

Nardilla: As I touch the sensor

None of these sensors will give a reliable reading at point-blank range. See the link above for the useable range for each type of sensor.