I use them with a feather NRF52832 board, red wires connected to 3.3 V pins, black wires to ground, a yellow wire to physical pin 4 (0.27). I use the following script and it loops at "print 1111" stage.
How can I bring it to "print unbroken" state?
- Code: Select all | TOGGLE FULL SIZE
/*
IR Breakbeam sensor demo!
*/
#define LEDPIN 31
#define SENSORPIN 4
// variables will change:
int sensorState = 0, lastState=0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup
Serial.begin(9600);
}
void loop(){
// read the state of the pushbutton value:
sensorState = digitalRead(SENSORPIN);
// check if the sensor beam is broken
// if it is, the sensorState is LOW:
if (sensorState == LOW) {
// turn LED on:
digitalWrite(LEDPIN, HIGH);
Serial.println("1111");
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
Serial.println("2222");
}
if (sensorState && !lastState) {
Serial.println("Unbroken");
}
if (!sensorState && lastState) {
Serial.println("Broken");
}
lastState = sensorState;
}