How to run 3 mode of operation with push button for mode selection ?

I need to run 3 modes of operation if 3 push buttons are pushed in arduino. I have made the initiate code logic for one push button and one operation but I got an error message of “redefinition of int ena1”.

for eg:

if button 1 is pushed then the motor should rotate in slow speed.
if button 2 is pushed then the motor should rotate in medium speed.
if button 3 is pushed then the motor should rotate in high speed.
.
If I got some suggestion on how to define the code block for the above logic it would be a great help for me to derive at the solution.

Please help me to correct the code.

trial_2.ino (5.73 KB)

You say you get an error message but the code you've posted only has one button and a) doesn't compile because of a missing semi-colon and b) doesn't show anything like that error message.

Without seeing how you have tried to add the other buttons it's impossible to tell what you did wrong in order to get that error message.

Steve

Thanks a lot, Steve and sorry.

Actually, after posting in the forum I copied the .ino file to a separate folder and compiled again it worked without any errors. The code which is posted has 2 L3298N motor driver to drive 4 motors of an RC car. Based on the distance from an ultrasonic distance the speed of the motor is controlled.

The problem is that I need to set the motor at 200rpm for mode 1 and 300rpm for mode 2 using push button. I am much confused with the void loop and defining the coding structure. If I can get an overview for at least one mode I will update the other mode code accordingly.

I had made the code right now with a single push button for trial purpose. But I am not sure whether I have created the right void loop() function to run the program correctly as expected. I need some sort of suggestion to know the program structure at least for one mode so that I can update it for the other modes of operation, please.

Regards,
Praveen

If you really want the program to do exactly the same thing but just with different motor speeds then the logic in loop() should be something like:

Read all the buttons then
if button 1 is pressed set speed = some number
if button 2 is pressed set speed = some other number

motorRun(speed) // to run the motors using the speed you’ve just set.

Your existing function can be turning into motorRun() as it seems to have all the motor running code. You just need to pass in the different values for speed and use them in the analogWrite() commands.

Give it a try and if you have problems try again, this time with your best attempt including several buttons.

Steve