Is there anything wrong with this code, everything looks fine, just doesn't work

// 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);
}

What do you expect this sketch to do?
What devices are you using?

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 ?

1 Like

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.

Hi @trainguy53

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 would have thought 1/2 second for the motor to be on would be barely noticeable.
I mean, if you blink you would miss it surely.

Can you provide a circuit diagram of your complete setup including what you use for power.
(NO Fritzing thanks)

Show what each device is also.

With no schematic, I'll have to guess - you're powering the motor from the 5V pin? Don't do that, it's not capable of operating motors.

One MR to another - other than that, the delay() statements must go; they become an extremely bad habit, and within days, it will limit what you can do far too much. Please do some reading, below, and come back with questions about what you don't understand:
https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay/?queryID=2da37ffcb8b190febaf1237803b4d8e0

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. :expressionless:

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.

a7

1 Like

Really, the audience wasn't you, but you're correct, I didn't explain that super-useful debounce feature, did I? Oh well.

1 Like

I use it all the time, and never think to look right away when things that should misbehave aren't. Misbehaving.

a7

1 Like

Initially I set it off, for algorithm testing. Intent was to explain both behaviors to our OP, but, well, coffee wasn't strong enough today :expressionless:

Hey, @trainguy53 , Did you ever even return to this thread to see if anyone replied to your post?

1 Like

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