My code isn't working. (IR Sensor and LED)


void setup() {
  pinMode(4, INPUT); //arduino pin
  pinMode(2, OUTPUT); //arduino pin
  Serial.begin(9600); //Enable serial monitor
}

void loop(){
  value = digitalRead(2); // Get value and save boolean veriable
  if(value == 0){ //Verify condition
    Serial.println("ON"); //print serial monitor ON
    digitalWrite(2,HIGH);//LED on

  }else{
    Serial.println("OFF"); //print serial monitor OFF
    digitalWrite(2,LOW); //LED off
  }
}

Should this have been pin 4 ?

How is the input pin 4 wired ?

1 Like

Omgg!! Thankss Larry, ill try again

Hmmm, but when I put:

value = digitalRead(4)

It says error: value was not declared in this scope.

I have to apply an int function or what?

Thanks.


void loop(){
  byte value = digitalRead(2); // Get value and save boolean veriable
  if(value == 0){ //Verify condition

Yeah now there is no error. But, my IR sensor is crazy now. In the monitor serial it shows OFF ON OFF ON OFF ON lots of times.

I just want the LED light ON when sensor value is HIGH, then if value is LOW just off the LED light.

Thank you!

You need to respond to switch/sensor changes not to switch/sensor level.

Read this:

1 Like

Okey, I understand some things but i really don't know how to program my system. I will start with the int function, pinMode and the code after the void loop, it will be difficult for me (I'm a begginer :slight_smile: )

Thanks Larry.

Yes, what you need to do is examine the input for a change in level.

If the input goes from LOW to HIGH or HIGH to LOW, then you act accordingly.

You do not respond to the input WHEN it is HIGH or LOW.

1 Like

Perfect, now I'm going to change the code!

If I understood correctly, this has to be correct right?

int sensor = 4;
int LED = 2;
int value = 0;


void setup() {
  pinMode(4, INPUT); //arduino pin
  pinMode(2, OUTPUT); //arduino pin
  Serial.begin(9600); //Enable serial monitor
}

void loop(){
  byte  value = digitalRead(4); // Get value
  if(value == HIGH){ //Verify condition
    Serial.println("ON"); //print serial monitor ON
    digitalWrite(2,HIGH);//LED on

  }else{
    Serial.println("OFF"); //print serial monitor OFF
    digitalWrite(2,LOW); //LED off
  }
}

Why bother giving your pins names, and then not use them?

You need to see if the last time you read the input is different from what is read now.

The last time the sensor has read HIGH

What is the difference betwen == and = ??

Read these:

Added your code to a standard piece of code.

Ask questions on things you do not understand.

#define LEDon                       LOW
#define LEDoff                      HIGH

const byte sensor                 = 4;

const byte indicatorLED           = 2;
const byte heartbeatLED           = 11;

byte value;
byte lastSensorState;

//timing stuff
unsigned long lastHeartbeatMillis;
unsigned long inputsMillis;

//*********************************************************************************************
void setup()
{
  Serial.begin(9600); //Enable serial monitor
  
  pinMode(heartbeatLED, OUTPUT);
  pinMode(indicatorLED, OUTPUT);
  
  pinMode(sensor, INPUT_PULLUP); 

} //END of   setup()


//*********************************************************************************************
void loop()
{
  //****************************************               h e a r t b e a t   T I M E R
  //toggle the heartbeat LED every 500ms
  if (millis() - lastHeartbeatMillis >= 500)
  {
    //restart this TIMER
    lastHeartbeatMillis = millis();

    //toggle the LED so the user can see there is no blocking code
    digitalWrite(heartbeatLED, ! digitalRead(heartbeatLED));

  } //END of   this TIMER

  //****************************************               c h e c k I n p u t s    T I M E R
  //check the switches every 20ms, is it time now ?
  if (millis() - inputsMillis >= 20)
  {
    //restart this TIMER
    inputsMillis = millis();

    checkInputs();
  }

} //END of   loop()

//*********************************************************************************************
void checkInputs()
{
  //****************************************
  byte thisState = digitalRead(sensor);

  //has the input change state ?
  if (lastSensorState != thisState)
  {
    //update to the new state
    lastSensorState = thisState;

    //did the sensor go LOW ?
    if (thisState == LOW)
    {
      Serial.println("ON");
      digitalWrite(indicatorLED, LEDon);
    }

    //the sensor must have gone HIGH 
    else
    {
      Serial.println("OFF"); 
      digitalWrite(indicatorLED, LEDoff);
    }
  }

} //END of   checkInputs()




This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.