I need help creating or fixing an Arduino code for my project.
Post things here.
Where?
HI, @kerroncreg
Welcome to the forum.
Thanks.. Tom..
![]()
How do I delete. Am not getting to delete the topic?
Why do you want to delete the topic.
Just explain that you don't need assistance if you have solved your problem and click the "Solution" box.
Thanks.. Tom..
![]()
Can someone guide me to the right forum to get help with my adruino code please
We can but we need to know;
Can you please tell us your electronics, programming, arduino, hardware experience?
What hardware do you have?
What is your project, what is its application?
Thanks.. Tom..
![]()
The hardware are:
(1) L293D motor driver
(3) NO push buttons
(3) LEDs (green, white, red)
(2) DC motors
(1) Arduino UNO
The criteria for the car is:
-
When the car is at rest, wait for a button press to determine forward or backward motion. Motor is off, all LED’s are off.
-
If forward button is pressed while car is stationary, motor turns in clockwise direction and green LED is on.
-
If car is in forward motion and the forward button is pressed, motor accelerates. The motor can decelerate to 60rpm using the brake button. Green LED is on once car is in forward motion.
-
If reverse button is pressed while car is stationary, motor turns in anti-clockwise direction and the white LED indicator flashes continuously at a frequency of 2Hz.
-
When in forward/backward motion, if brake button is pressed, motor stops spinning. Green/white LEDs turns off and red LED turns on. If brake button is pressed while car is stationary, red LED turns on (no change in DC motor).
// Pin numbers for buttons
const int forwardButtonPin = A5;
const int reverseButtonPin = A4;
const int stopButtonPin = A3;
// Pin numbers for LEDs
const int greenLedPin = 5;
const int whiteLedPin = 6;
const int redLedPin = 13;
// Driver enable pins
const int speedPin1 = 9;
const int speedPin2 = 10;
// Motor direction control pins to L293D motor driver
const int motor1Pin1 = 8;
const int motor1Pin2 = 7;
const int motor2Pin1 = 12;
const int motor2Pin2 = 11;
const int normalMotorSpeed = 100; // RPM for Normal speed
const int maxMotorSpeed = 255; // RPM to accelerated speed
const int decelerationSpeed = 60; // RPM to decelerated speed
// Variables for reading button status
int forwardButtonState = 0;
int reverseButtonState = 0;
int stopButtonState = 0;
// Variables to store LED states
bool redLedOn = false;
bool whiteLedOn = false;
bool greenLedOn = false;
// Variable to keep track of motor speed
int currentMotorSpeed = 0;
void setup()
{
// Push buttons are inputs
pinMode(forwardButtonPin, INPUT_PULLUP);
pinMode(reverseButtonPin, INPUT_PULLUP);
pinMode(stopButtonPin, INPUT_PULLUP);
// LEDs are outputs
pinMode(greenLedPin, OUTPUT);
pinMode(whiteLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
// Motor controls are outputs
pinMode(speedPin1, OUTPUT);
pinMode(speedPin2, OUTPUT);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
// All LEDs are turned off initially
digitalWrite(greenLedPin, LOW);
digitalWrite(whiteLedPin, LOW);
digitalWrite(redLedPin, LOW);
//Initial state of buttons
digitalWrite(forwardButtonPin, 0);
digitalWrite(reverseButtonPin, 0);
digitalWrite(stopButtonPin, 0);
// Initialize motor speed
analogWrite(speedPin1, 0);
analogWrite(speedPin2, 0);
Serial.begin(9600);
}
void loop()
{
// Read button states
forwardButtonState = digitalRead(forwardButtonPin);
reverseButtonState = digitalRead(reverseButtonPin);
stopButtonState = digitalRead(stopButtonPin);
// Handle motor control based on button presses
if (forwardButtonState == HIGH)
{
// Move forward at full speed
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
analogWrite(speedPin1, normalMotorSpeed);
analogWrite(speedPin2, normalMotorSpeed);
redLedOn = false;
whiteLedOn = false;
greenLedOn = true;
}
else if (reverseButtonState == HIGH)
{
// Move in reverse at full speed
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(speedPin1, normalMotorSpeed);
analogWrite(speedPin2, normalMotorSpeed);
redLedOn = false;
whiteLedOn = true;
greenLedOn = false;
}
else if (stopButtonState == HIGH)
{
// Stop the motor
stopMotor();
redLedOn = true;
whiteLedOn = false;
greenLedOn = false;
}
// Update LED states
digitalWrite(redLedPin, redLedOn ? HIGH : LOW);
digitalWrite(whiteLedPin, whiteLedOn ? HIGH : LOW);
digitalWrite(greenLedPin, greenLedOn ? HIGH : LOW);
// If the white LED is on, make it blink
if (whiteLedOn)
{
digitalWrite(whiteLedPin, HIGH);
delay(500); // On for 500ms = 2Hz
digitalWrite(whiteLedPin, LOW);
delay(500); // Off for 500ms = 2Hz
}
}
// Function to stop the motor
void stopMotor()
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
analogWrite(speedPin1, 0);
analogWrite(speedPin2, 0);
}
I am not getting the code to carryout all the required functions.
Hi,
Please read the intro.
This is how to display your code;
// Pin numbers for buttons
const int forwardButtonPin = A5;
const int reverseButtonPin = A4;
const int stopButtonPin = A3;
// Pin numbers for LEDs
const int greenLedPin = 5;
const int whiteLedPin = 6;
const int redLedPin = 13;
// Driver enable pins
const int speedPin1 = 9;
const int speedPin2 = 10;
// Motor direction control pins to L293D motor driver
const int motor1Pin1 = 8;
const int motor1Pin2 = 7;
const int motor2Pin1 = 12;
const int motor2Pin2 = 11;
const int normalMotorSpeed = 100; // RPM for Normal speed
const int maxMotorSpeed = 255; // RPM to accelerated speed
const int decelerationSpeed = 60; // RPM to decelerated speed
// Variables for reading button status
int forwardButtonState = 0;
int reverseButtonState = 0;
int stopButtonState = 0;
// Variables to store LED states
bool redLedOn = false;
bool whiteLedOn = false;
bool greenLedOn = false;
// Variable to keep track of motor speed
int currentMotorSpeed = 0;
void setup()
{
// Push buttons are inputs
pinMode(forwardButtonPin, INPUT_PULLUP);
pinMode(reverseButtonPin, INPUT_PULLUP);
pinMode(stopButtonPin, INPUT_PULLUP);
// LEDs are outputs
pinMode(greenLedPin, OUTPUT);
pinMode(whiteLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
// Motor controls are outputs
pinMode(speedPin1, OUTPUT);
pinMode(speedPin2, OUTPUT);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
// All LEDs are turned off initially
digitalWrite(greenLedPin, LOW);
digitalWrite(whiteLedPin, LOW);
digitalWrite(redLedPin, LOW);
//Initial state of buttons
digitalWrite(forwardButtonPin, 0);
digitalWrite(reverseButtonPin, 0);
digitalWrite(stopButtonPin, 0);
// Initialize motor speed
analogWrite(speedPin1, 0);
analogWrite(speedPin2, 0);
Serial.begin(9600);
}
void loop()
{
// Read button states
forwardButtonState = digitalRead(forwardButtonPin);
reverseButtonState = digitalRead(reverseButtonPin);
stopButtonState = digitalRead(stopButtonPin);
// Handle motor control based on button presses
if (forwardButtonState == HIGH)
{
// Move forward at full speed
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
analogWrite(speedPin1, normalMotorSpeed);
analogWrite(speedPin2, normalMotorSpeed);
redLedOn = false;
whiteLedOn = false;
greenLedOn = true;
}
else if (reverseButtonState == HIGH)
{
// Move in reverse at full speed
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(speedPin1, normalMotorSpeed);
analogWrite(speedPin2, normalMotorSpeed);
redLedOn = false;
whiteLedOn = true;
greenLedOn = false;
}
else if (stopButtonState == HIGH)
{
// Stop the motor
stopMotor();
redLedOn = true;
whiteLedOn = false;
greenLedOn = false;
}
// Update LED states
digitalWrite(redLedPin, redLedOn ? HIGH : LOW);
digitalWrite(whiteLedPin, whiteLedOn ? HIGH : LOW);
digitalWrite(greenLedPin, greenLedOn ? HIGH : LOW);
// If the white LED is on, make it blink
if (whiteLedOn)
{
digitalWrite(whiteLedPin, HIGH);
delay(500); // On for 500ms = 2Hz
digitalWrite(whiteLedPin, LOW);
delay(500); // Off for 500ms = 2Hz
}
}
// Function to stop the motor
void stopMotor()
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
analogWrite(speedPin1, 0);
analogWrite(speedPin2, 0);
}
Have you written basic code that JUST tests to motor?
Have you written your code in stages?
Can you please tell us your electronics, programming, arduino, hardware experience?
Thanks.. Tom..
![]()
I am a beginner. I don't have much experience with Arduino
Yes I have written basic code that just test the motor.
Yes the code was written in stages
That's no shame. All new guys are the same.
But.... Read the advice how to use this forum instead of only asking for forum to give You code, connections......
The intention of forum is to help You learn and grow in programming. Forum is not a private clinic fixing whatever problems You have. Also, solving Your problems will be available for oncoming, new members. Together we build a library for future members.
Excellent..
![]()
What is not working?
Thanks.. Tom..
![]()
When the car is reversing and I push the brake button it suppose to stop, but wont.
Can you please show what type of "buttons" you are using, and how you have them wired?
There are some strange things in your code
First you set the pinMode to INPUT_PULLUP and then you take the pullup out of the circuit and just have the pins as input
pinMode(forwardButtonPin, INPUT_PULLUP);
pinMode(reverseButtonPin, INPUT_PULLUP);
pinMode(stopButtonPin, INPUT_PULLUP);
//these next statements remove the pullups
//Initial state of buttons
digitalWrite(forwardButtonPin, 0);
digitalWrite(reverseButtonPin, 0);
digitalWrite(stopButtonPin, 0);
Your logic then expects the buttons to read HIGH when pressed. This will depend on how they are wired.
When the car is reversing and I push the brake button it suppose to stop, but wont.
Does the brake button stop the car when it is going forward?
If you comment out the white led which uses delay, does the car respond to the buttons as you want?
if (forwardButtonState == HIGH)
…
else if (reverseButtonState == HIGH)
…
else if (stopButtonState == HIGH)
-
Do you know what else if ( … ) does ?
-
Do you know the difference between detecting a switch level versus detecting a switch change in state.
-
And do you know why delay( ) should be avoided.
No, i don't know the difference.