I'm having trouble comiling this code. I thought the break command would work in Arduino IDE but I get an error. is this a missing library or is my code the issue.
The Project: Soft starter for a kids ride-on car. I'm using arduino (I'll probably use the nano) and some motor drivers to run the motors. Motors will run off 18 volts (normally they run off 12v, using soft start to save the motors and my son's neck from the inrush current from the lithium batteries) I've coded it with an if statement in the loop and a break to prevent the loop from running all iterations if my kid lets off the pedal.
The Issue: Haven't tested code yet because when I go to compile I get:
exit status 1
'Break' was not declared in this scope
The code:
//define pin numbers
int motor = 6;
int pedal = 5;
void setup() {
// no setup code
}
void loop() {
// runs repeatedly:
if (digitalRead) (pedal == LOW); {
analogWrite(motor, LOW); //Sets the motor pin low
}
if (digitalRead) (pedal == HIGH); {
for (int i = 0; i<=255; i++) {
analogWrite(motor, i);
delay(12); //delay to ensure approximately 3 seconds till full power is reached
if (digitalRead) (pedal == LOW); {
Break; //to prevent running all iterations if pedal is released
}
}
}
else {
analogWrite(Motor, LOW)
}
}
Let's do a bit of code review since there are some issues.
if (digitalRead) (pedal == LOW); {
This has two problems.
It's a syntax error and shouldn't compile. I'm surprised that this wasn't the first thing the compiler found, but C/C++ can be very forgiving sometimes.
digitalRead(pedal == LOW) is probably what you intended, but that will always read D0 since pedal == LOW will evaluate to 0.
I believe what you intended was
if (digitalRead(pedal) == LOW)
analogWrite(motor, LOW); //Sets the motor pin low
This is technically OK, but it is better practice to write what you intend, which is
analogWrite(motor, 0);
In addition to
Break; //to prevent running all iterations if pedal is released
Yes and initially I wrote it without the semicolon and I got a syntax error. I think it was "Expected ; before (. I'll try it again. perhaps it was all my other errors that were causing issues.
Thank you. I was getting syntax error telling me to add the semicolon but its probably because I didn't imput the arguments correctly. This worked great.
Also, great catch on the
analogWrite(motor, 0);
break;
I actually changed this because I initially had it listed in an else statement AFTER break so...... yea didn't work.
current code looks functional, just have to test.
//define pin numbers
int motor = 6;
int pedal = 5;
void setup() {
// no setup code
}
void loop() {
// loop
if (digitalRead (pedal) == LOW) {
analogWrite(motor, 0); //Sets the motor pin low
}
if (digitalRead (pedal) == HIGH) {
for (int i = 0; i<=255; i++) {
analogWrite(motor, i);
delay(12); //delay to ensure 3 seconds till full power is reached
if (digitalRead (pedal) == LOW) {
analogWrite(motor, 0);
break; //to prevent running all iterations if pedal is released
}
}
}
}
when you have concerns regarding acceleration, you might also think about a soft deceleration.
/*
Soft Starter Code
https://forum.arduino.cc/t/soft-starter-code/1292579/7
2024-08-18 by noiasca
*/
// a generic class for motors with soft start/soft end
class Motor {
const uint8_t pin;
uint32_t previousMillis = 0;
uint8_t speed = 0;
public:
Motor (const uint8_t pin) : pin(pin) {}
void accelerate(uint32_t currentMillis = millis()) {
if (speed < 255 && previousMillis - currentMillis > 12) {
previousMillis = currentMillis;
speed++;
analogWrite(pin, speed);
Serial.println(speed); // debug only
}
}
void decelerate(uint32_t currentMillis = millis()) {
if (speed > 0 && previousMillis - currentMillis > 6) {
previousMillis = currentMillis;
speed--;
analogWrite(pin, speed);
Serial.println(speed); // debug only
}
}
void stop() {
if (speed > 0) {
speed = 0;
analogWrite(pin, speed);
}
}
};
//create an instance of a motor and assign the pin
Motor motor(6); // PWM pins UNO (R3 and earlier), Nano, Mini 3, 5, 6, 9, 10, 11
const uint8_t pedal = 5; // the pedal connects to VCC, needs an external pulldown
void setup() {
Serial.begin(115200);
}
void loop() {
if (digitalRead (pedal) == HIGH) {
motor.accelerate();
}
else {
motor.decelerate(); // or motor.stop() without any deceleration
}
}
//
The code is written "non blocking" without delays.
Other code could be run at the same time as the acceleration takes place in the 3 seconds.
Wouldn't it be more realistic if the kid has the possibility to HOLD a specific speed?
Currently it can only increase the speed, hold full speed (by pressing the pedal) and come to a stop (by releasing the pedal).
Okay I know millis () is held in an unsigned long but I ran it in the simulator you linked to and updating previousMillis and making the condition currentMillis - previousMillis made it take the full 3 seconds to start (And likewise approximately 1500ms to stop).
This code is super awesome and I'll probably end up using it instead because if he bounces off the pedal for a moment, your code allows picking up where it left off(More or less).
The reason I didn't include a soft stop is because when the pedal is released, the motor terminals are shorted through a power resistor to cause rapid deceleration (Should the kid need to stop abruptly for a car or something else disastrous.