A "help" forum as we are here, generally implies you have started your project, studied your requirements (and maybe school notes), do the appropriate research, attempt to implement the program logic into your sketch ... as a minimum. Like a homework assignment, you do not want to turn-in a blank response.
- Show the code for lighting a LED from the IR sensor.
- Show us the wiring you have implemented (schematic)
.... You should be able to do the following by simply finding your (specific) IR sensor model on the Internet. Most of the time, sellers that cater to experimenters also have sample code that is the basis for starting.
When you have a working circuit/program the info I previously provided can be added.
Procedure in action. Assume a break-beam sensor. I can search for: "Arduino" + "Infrared" + break-beam
https://duckduckgo.com/?q=%22Arduino%22+%2B+%22Infrared%22+%2B+breal-beam&ia=web
I know from "experience" that Adafruit & Sparkfun sell such sensors and provide support (they charge a higher price to cover the support.) So, just add "Adafruit" to the above search:
https://duckduckgo.com/?q=%22Arduino%22+%2B+%22Infrared%22+%2B+break-beam+%2B+%22Adafruit%22&ia=web
Our first return results is a goldmine:
https://learn.adafruit.com/ir-breakbeam-sensors?view=all
We not have a generic IR sensor and full test code!
/*
IR Breakbeam sensor demo!
*/
#define LEDPIN 13
// Pin 13: Arduino has an LED connected on pin 13
// Pin 11: Teensy 2.0 has the LED on pin 11
// Pin 6: Teensy++ 2.0 has the LED on pin 6
// Pin 13: Teensy 3.0 has the LED on pin 13
#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);
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
}
if (sensorState && !lastState) {
Serial.println("Unbroken");
}
if (!sensorState && lastState) {
Serial.println("Broken");
}
lastState = sensorState;
}
You should run the test code to make certain that your PC and solderless-project-board and pin-jumpers are all wired correctly.
At this point, you have your base code that can be modified.
You did not tell us which Arduino board you are using nor did you give us the model number or link to the IR sensor, so I really cannot provide exact code as different boards/sensors may require minor modification to the test sketch.
But, all of the elements that need to be added are easily found on the Arduino reference pages:
https://www.arduino.cc/reference/en/
bool
millis()
Give it a try.
If you need to come back to the forum, be certain to provided everything needed to assist:
- Existing code (post using the "</>" code tags
- make/model of Arduino and IR sensor (link helpful)
- a drawing of how the wiring is hooked-up
- what you have accomplished
- what help you specifically need