IR proximity sensor and LED

Hello,

I'd found on the web a very interesting project to make the IR proximity sensor form IR led and Ir reciver. Everything was ok with the code but when i tried to implement it to my code something is wrong.

The Sensors should act like normal switches, so when the don't sense object they should start the program to fade led up, when they are sensing obcject the program should go oposite.

So, just to be straight program nr 1 fading led up and down with switches was ok, program nr2 just to test the sensor with arduino built in led was also ok, but both together they dont work.

here is the code:

#include <IRremote.h>

#define PIN_IR1 9
#define PIN_IR2 10
#define PIN_IR3 12
#define PIN_DETECT1 2
#define PIN_DETECT2 4
#define PIN_DETECT3 7
#define PIN_STATUS 13

const int ledPin1 = 3;
const int ledPin2 = 5;
const int ledPin3 = 6;

int brightness1 = 0;
int brightness2 = 0;
int brightness3 = 0;

IRsend irsend;
void setup(){
  pinMode(PIN_DETECT1, INPUT);
  pinMode(PIN_DETECT2, INPUT);
  pinMode(PIN_DETECT3, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  
  pinMode(PIN_STATUS, OUTPUT);
  irsend.enableIROut(17); //IR LED Transmitter PWM Freq (probably in kHz)
  irsend.mark(0);
}

void loop() 
{
  {
  int buttonVal1 = !digitalRead(PIN_DETECT1);
  if (buttonVal1 == HIGH)
  {
  	if (brightness1 < 255) brightness1++;
  }
  else 
  {
    if (brightness1 > 0) brightness1--;
  }
  analogWrite(ledPin1,brightness1);
  delay(20);
  }

  {
  int buttonVal2 = !digitalRead(PIN_DETECT2);
  if (buttonVal2 == HIGH)
  {
    if (brightness2 < 255) brightness2++;
  }
  else 
  {
    if (brightness2 > 0) brightness2--;
  }
  analogWrite(ledPin2,brightness2);
  delay(20);
  }

  {
  int buttonVal3 = !digitalRead(PIN_DETECT3);
  if (buttonVal3 == HIGH)
  {
    if (brightness3 < 255) brightness3++;
  }
  else 
  {
    if (brightness3 > 0) brightness3--;
  }
  analogWrite(ledPin3,brightness3);
  delay(20);
  }
}

Hi Shalvan

but both together they dont work.

What does happen when you run the program?

Maybe try reducing the program to just the code for one sensor and LED, and include Serial.println statements to check that the values of buttonVal and brightness are what you expect.

Regards

Ray

i think that the IR LED isnt working because when i am looking throught camera i dont see any light, earlier when i checked only the sensor program it was working, and i connected eveything like it should be.

Shalvan:
i think that the IR LED isnt working because when i am looking throught camera i dont see any light, earlier when i checked only the sensor program it was working, and i connected eveything like it should be.

Please post the code for the sensor that did work, and a diagram of how your hardware is connected.

  {
  int buttonVal1 = !digitalRead(PIN_DETECT1);
  if (buttonVal1 == HIGH)
  {
  	if (brightness1 < 255) brightness1++;
  }
  else 
  {
    if (brightness1 > 0) brightness1--;
  }
  analogWrite(ledPin1,brightness1);
  delay(20);
  }

The unnecessary curly braces (the first and last ones; the others are not strictly necessary but are a good idea), the piss-poor indenting, and the complete lack of comments makes your code hard to read.

Tools + Auto Format will take care of the indenting in nothing flat. Deleting the useless curly braces and adding comments is up to you.