// Define the pin numbers
const int irSensorPin = 2; // IR sensor input pin
const int motorPin = 3; // Motor control pin
void setup() {
// Initialize the IR sensor pin as an input
pinMode(irSensorPin, INPUT);
// Initialize the motor pin as an output
pinMode(motorPin, OUTPUT);
// Start with the motor off
digitalWrite(motorPin, LOW);
// Begin serial communication at 9600 bits per second
Serial.begin(9600);
}
void loop() {
// Read the state of the IR sensor
int sensorValue = digitalRead(irSensorPin);
// Print the sensor value to the Serial Monitor
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
// Check if the sensor detects an object
if (sensorValue == HIGH) {
// Turn on the motor
Serial.println("Motor ON");
digitalWrite(motorPin, HIGH);
// Keep the motor on for 500 milliseconds
delay(500); // Delay for 500ms
// Turn off the motor
Serial.println("Motor OFF");
digitalWrite(motorPin, LOW);
// Optional: Add a small delay to prevent bouncing issues
delay(100);
}
// Small delay to avoid flooding the serial monitor
delay(500);
}
You didn’t read the guides to getting the most from your post… CODE tags would be a good start, along with explaining what you want, and what isn’t happening.
My car doesn’t work either, can you tell me how to fix it ?
Welcome!
I see a big problem in your code you apparently did not understand the forum guidelines when you read them. If you post your code using code tages it will become readable instead of gibberish which I now see.
welcome to the arduino-forum.
I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.
Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.
Your project has hardware. So the cause for not working can be the hardware.
You should really read this tutorial to learn what informations are needed to really help you
I have modified your code to do the monitoring without delay(), below. You will note that it only sends a message if the input has changed. This may result in a few messages at each transition, if your input 'flickers', and we can make changes to 'debounce' the input, if that happens. The new code also follows the Input, Process, Output model. You may find it useful to follow this design pattern in future. Debouncing for the input would be done in the readInputs() function.
Note, this is not truly tested, as I haven't your hardware. Instead, I put it up in Wokwi, a simulator for Arduino projects, which you may also find useful:
The button simulates your IR input, and the LED simulates your motor output.
Hope that helps!
Hello, can you show your #includes at the top of your code? Can you also edit the code so that the entire thing goes into the code font to help it become more legible
The debouncing in the simulation is handled by unselecting the bounce characteristic of the simulated pushbutton. I played for some time wondering why there was no bouncing problem.
I wish they made buttons like that for realz.
The IR signal the pushbutton is a proxy for might have brief periods of instability. More likely it will have clean edges and behave like the bounceless pushbutton.
A very handy attribute for the pushbutton component in the simulator.