0

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.

//modeselectiion
const int buttonPin=22;

int buttonState = 0; 
int flag=0; 

//pinout setup for L298N motors 
#define trigpin 4 
#define echopin 3 

int ena1 = 5; 
int enb1 = 6; 

int in1A = 7; 
int in2A = 8; 
int in3A = 9; 
int in4A = 10; 

int ena2 = 11; 
int enb2 = 12; 

int in1B = 13; 
int in2B = 44; 
int in3B = 45; 
int in4B = 46; 

int led1 = 54; 
int led2 = 55; 
int led3 = 56; 
int led4 = 57; 
int led5 = 58; 

float distancem;

void setup()
{
    pinMode(buttonPin, INPUT_PULLUP)
}

void loop()
{
    //Read button state 
    buttonState = digitalRead(buttonPin);
    //if pressed 
    if (buttonState == LOW) { 
        if (flag == 0){
            Mode_A(); 
            flag=1;
        }
    }
}

void Mode_A()
{
    Serial.begin(9600);
    pinMode(trigpin, OUTPUT);
    pinMode(echopin, INPUT);

    pinMode(ena1, OUTPUT); 
    pinMode(enb1, OUTPUT); 

    pinMode(ena2, OUTPUT); 
    pinMode(enb2, OUTPUT);

    pinMode(in1A, OUTPUT); 
    pinMode(in2A, OUTPUT);
    pinMode(in3A, OUTPUT);
    pinMode(in4A, OUTPUT);

    pinMode(in1B, OUTPUT); 
    pinMode(in2B, OUTPUT);
    pinMode(in3B, OUTPUT);
    pinMode(in4B, OUTPUT);

    pinMode(led1, OUTPUT); 
    pinMode(led2, OUTPUT);
    pinMode(led3, OUTPUT);
    pinMode(led4, OUTPUT);
    pinMode(led5, OUTPUT);

    digitalWrite(led1, LOW); 
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led5, LOW);

    analogWrite(ena1, 0); 
    analogWrite(enb1, 0);
    analogWrite(ena2, 0); 
    analogWrite(enb2, 0);
    delay(1000); 

    int duration, distance;
    digitalWrite(trigpin, HIGH);

    delayMicroseconds(1000);  
    digitalWrite(trigpin, LOW);

    duration = pulseIn(echopin,HIGH);

    distance = ( duration / 2) / 29.1;
    Serial.println("inches:"); 
    Serial.println(distance);

    distance = map(distance, 0 , 197, 0 , 255 ); 

    if ((distance < 0)) {
        distance = 0; 
    } else if ((distance >= 0) && (distance <= 40)) {
        //motor 1 set
        analogWrite(ena1, 0); 
        analogWrite(enb1, 0);
        digitalWrite(in1A, LOW); 
        digitalWrite(in2A, LOW);
        digitalWrite(in3A, LOW);
        digitalWrite(in4A, LOW);
        //motor 2 set
        analogWrite(ena2, 0); 
        analogWrite(enb2, 0);
        digitalWrite(in1B, LOW); 
        digitalWrite(in2B, LOW);
        digitalWrite(in3B, LOW);
        digitalWrite(in4B, LOW);
        //led light
        digitalWrite(led1, LOW); 
        digitalWrite(led2, LOW);
        digitalWrite(led3, LOW);
        digitalWrite(led4, LOW);
        digitalWrite(led5, LOW);
    } else if ((distance > 40) && (distance <= 57)) {
        //motor 1 set
        analogWrite(ena1, distance); 
        analogWrite(enb1, distance);
        digitalWrite(in1A, HIGH); 
        digitalWrite(in2A, LOW);
        digitalWrite(in3A, LOW);
        digitalWrite(in4A, HIGH);
        //motor 2 set
        analogWrite(ena2, distance); 
        analogWrite(enb2, distance);
        digitalWrite(in1B, HIGH); 
        digitalWrite(in2B, LOW);
        digitalWrite(in3B, LOW);
        digitalWrite(in4B, HIGH);
        //led light 
        digitalWrite(led1, HIGH); 
        digitalWrite(led2, LOW);
        digitalWrite(led3, LOW);
        digitalWrite(led4, LOW);
        digitalWrite(led5, LOW);
    } else if((distance > 57) && (distance <= 74)) {
        //motor 1 set
        analogWrite(ena1, distance); 
        analogWrite(enb1, distance);
        digitalWrite(in1A, HIGH); 
        digitalWrite(in2A, LOW);
        digitalWrite(in3A, LOW);
        digitalWrite(in4A, HIGH);
        //motor 2 set
        analogWrite(ena2, distance); 
        analogWrite(enb2, distance);
        digitalWrite(in1B, HIGH); 
        digitalWrite(in2B, LOW);
        digitalWrite(in3B, LOW);
        digitalWrite(in4B, HIGH);
        //led light
        digitalWrite(led1, HIGH); 
        digitalWrite(led2, HIGH);
        digitalWrite(led3, LOW);
        digitalWrite(led4, LOW);
        digitalWrite(led5, LOW);
    } else if ((distance > 70) && (distance <= 91)) {
        //motor 1 set
        analogWrite(ena1, distance); 
        analogWrite(enb1, distance);
        digitalWrite(in1A, HIGH); 
        digitalWrite(in2A, LOW);
        digitalWrite(in3A, LOW);
        digitalWrite(in4A, HIGH);
        //motor 2 set
        analogWrite(ena2, distance); 
        analogWrite(enb2, distance);
        digitalWrite(in1B, HIGH); 
        digitalWrite(in2B, LOW);
        digitalWrite(in3B, LOW);
        digitalWrite(in4B, HIGH);
        //led light 
        digitalWrite(led1, HIGH); 
        digitalWrite(led2, HIGH);
        digitalWrite(led3, HIGH);
        digitalWrite(led4, LOW);
        digitalWrite(led5, LOW);
    } else if ((distance > 91) && (distance <= 108)) {
        //motor 1 set
        analogWrite(ena1, distance); 
        analogWrite(enb1, distance);
        digitalWrite(in1A, HIGH); 
        digitalWrite(in2A, LOW);
        digitalWrite(in3A, LOW);
        digitalWrite(in4A, HIGH);
        //motor 2 set
        analogWrite(ena2, distance); 
        analogWrite(enb2, distance);
        digitalWrite(in1B, HIGH); 
        digitalWrite(in2B, LOW);
        digitalWrite(in3B, LOW);
        digitalWrite(in4B, HIGH);
        //led light 
        digitalWrite(led1, HIGH); 
        digitalWrite(led2, HIGH);
        digitalWrite(led3, HIGH);
        digitalWrite(led4, HIGH);
        digitalWrite(led5, LOW);
    } else if ((distance > 108) && (distance <= 127)) {
        //motor 1 set
        analogWrite(ena1, distance); 
        analogWrite(enb1, distance);
        digitalWrite(in1A, HIGH); 
        digitalWrite(in2A, LOW);
        digitalWrite(in3A, LOW);
        digitalWrite(in4A, HIGH);
        //motor 2 set
        analogWrite(ena2, distance); 
        analogWrite(enb2, distance);
        digitalWrite(in1B, HIGH); 
        digitalWrite(in2B, LOW);
        digitalWrite(in3B, LOW);
        digitalWrite(in4B, HIGH);
        //led light
        digitalWrite(led1, HIGH); 
        digitalWrite(led2, HIGH);
        digitalWrite(led3, HIGH);
        digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
    } else if (distance > 127) {
        //motor 1 set
        analogWrite(ena1, distance); 
        analogWrite(enb1, distance);
        digitalWrite(in1A, HIGH); 
        digitalWrite(in2A, LOW);
        digitalWrite(in3A, LOW);
        digitalWrite(in4A, HIGH);
        //motor 2 set
        analogWrite(ena2, distance); 
        analogWrite(enb2, distance);
        digitalWrite(in1B, HIGH); 
        digitalWrite(in2B, LOW);
        digitalWrite(in3B, LOW);
        digitalWrite(in4B, HIGH);
        //led light 
        digitalWrite(led1, HIGH); 
        digitalWrite(led2, HIGH);
        digitalWrite(led3, HIGH);
        digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
    } 
}
  • redefinition of int ena1 ... this is all of the error message that you get? ... there is nothing else being shown? – jsotola Jul 6 at 17:38
  • sorry .. I copied the file in a separate folder and compiled it again and suddenly I got no errors. But I am not sure whether this will work or not! – Praveen Jul 7 at 16:39
1

This is not an answer.

It is the simplest way to include code in a comment.

Your code is overly complex and long.

First section of void Mode_A() should be in setup() because it should execute only once.

The last series of if blocks contain identical motor control code.

Move the motor control code and LED control code into separate if-else blocks to get something like the following code snippet.

Also, move comments away from the code so that the code looks less cluttered.

distance = map(distance, 0 , 197, 0 , 255 );

if (distance < 0)  distance = 0;

if (distance <= 40) {
    analogWrite(ena1, 0);             // turn off motor 1
    analogWrite(enb1, 0);
    digitalWrite(in1A, LOW);
    digitalWrite(in2A, LOW);
    digitalWrite(in3A, LOW);
    digitalWrite(in4A, LOW);

    analogWrite(ena2, 0);             // turn off motor 2
    analogWrite(enb2, 0);
    digitalWrite(in1B, LOW);
    digitalWrite(in2B, LOW);
    digitalWrite(in3B, LOW);
    digitalWrite(in4B, LOW);

    digitalWrite(led1, LOW);          // turn off LEDs 
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led5, LOW);
}
else {
    analogWrite(ena1, distance);      // motor 1 set
    analogWrite(enb1, distance);
    digitalWrite(in1A, HIGH);
    digitalWrite(in2A, LOW);
    digitalWrite(in3A, LOW);
    digitalWrite(in4A, HIGH);

    analogWrite(ena2, distance);      // motor 2 set
    analogWrite(enb2, distance);
    digitalWrite(in1B, HIGH);
    digitalWrite(in2B, LOW);
    digitalWrite(in3B, LOW);
    digitalWrite(in4B, HIGH);

    digitalWrite(led1, HIGH);         // turn on first LED, the following lines turn on other LEDs
}

if (distance > 57) {
    digitalWrite(led2, HIGH);         // led light
}
else {
    digitalWrite(led2, LOW);
}

if (distance > 70) {
    digitalWrite(led3, HIGH);         // led light
}
else {
    digitalWrite(led3, LOW);
}

// following two "if-else" blocks are shown with reduced number of code lines as an example only
// it is meant to show how to reduce the number of code lines
// feel free to format them same as above   

if (distance > 91) {  digitalWrite(led4, HIGH); }        // compacted "if-else" block
else               {  digitalWrite(led4, LOW ); }

if (distance > 108) {  digitalWrite(led5, HIGH);  } else {  digitalWrite(led5, LOW);  }     // single line "if-else" block
0

The code you posted seems kind of unrelated to the question you ask. To answer just the question, I would use an approach as follows:

uint8_t speed = LOW_SPEED;

void loop()
{
    if (digitalRead(BUTTON_SLOW) == LOW)
        speed = SLOW_SPEED;
    else if (digitalRead(BUTTON_MEDIUM) == LOW)
        speed = MEDIUM_SPEED;
    else if (digitalRead(BUTTON_FAST) == LOW)
        speed = FAST_SPEED;
    analogWrite(EN_PIN, speed);
}

Note that this is one of those easy situations where you don't even need to debounce the buttons.

  • Thanks a lot for the suggestion _/_ . 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. – Praveen Jul 7 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.