Click here to Skip to main content
14,544,858 members
Rate this:
Please Sign up or sign in to vote.
See more:
/*  Arduino DC Motor Control - PWM| L293d
*/

#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7

int motorSpeedA = 0;
int motorSpeedB = 0;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}
void loop() {
  int 1 button = analogRead(A0);
  int 2 button = analogRead(A1); 
  int 3 button = analogRead(A2); 
  int 4 button = analogRead(A3); // Read value

  // 1,2 button used for forward and backward control
     if (2 button) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
  }
}
  else if (1 button) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
  }
  }

  // 3,4 butten used for left and right control
  if (3 button) {
     // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B stop
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    }
  if (4 button) {
     // Set Motor A stop
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}


What I have tried:

exit status 1
expected unqualified-id before numeric constant
Posted
Updated 23-May-20 22:42pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

These :
int 1 button = analogRead(A0);
int 2 button = analogRead(A1);
int 3 button = analogRead(A2);
int 4 button = analogRead(A3); // Read value
are not valid expressions.

You could make an array of four integers and do something like this :
int buttons[4];
buttons[0] = analogRead(A0);
buttons[1] = analogRead(A1);
buttons[2] = analogRead(A2);
buttons[3] = analogRead(A3); // Read value
-edit-

Also, as phil.o mentioned, the conditional expressions are incorrect. They need to be of the form :
if( buttons[0] )
{
}
else if( buttons[1] )
{
}
What you wrote for those is not valid in the C or C++ languages. Make sure you understand the differences.

One other thing - usually buttons states are binary with values of 1 or 0. Are you sure it is correct to acquire them using analogRead()? I see you are using digitalWrite, is there a corresponding digitalRead()?
   
v3
Comments
phil.o 23-May-20 16:43pm
   
5'd
You could have told him to modify his conditions in if..else blocks as well.
Rick York 23-May-20 18:14pm
   
Yes, I didn't look that far. The first errors were so obvious I didn't bother.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100