Code for IR sensor
Code for Counter using IR sensor with NODEMCU
int irPin=2;
int count=0;
boolean state = true;
void setup() {
Serial.begin(9600);
pinMode(irPin, INPUT);
}
void loop() {
if (!digitalRead(irPin) && state){
count++;
state = false;
Serial.print("Count: ");
Serial.println(count);
delay(100);
}
if (digitalRead(irPin)){
state = true;
delay(100);
}
}
Code for Object detection using IR sensor with NODEMCU
int value; // variable to store value read on digital pin
void setup()
{
pinMode(2, INPUT); // initialize digital pin 2 as an input
pinMode(4, OUTPUT); //for LED as output
}
void loop()
{
value = digitalRead(2); // read value on pin 2
if(value==1) // if value is 1
{
digitalWrite(4, HIGH); //turn LED on
}
else // otherwise
{
digitalWrite(4, LOW); //turn LED off
}
}
Code for Detection of Object using IR sensor with NODEMCU
int value; // variable to store value read on digital pin
void setup()
{
pinMode(2, INPUT); // initialize digital pin 2 as an input
Serial.begin(9600);
}
void loop()
{
value = digitalRead(2); // read value on pin 2
Serial.println(value); // print value on serial terminal
delay(1000); // wait for 1 second
}
Comments
Post a Comment