Need help with a simple sketch error

Hello All,

I want to control 2 motors using a simple joystick connected to an Arduino that will sent a PWM signal to the L298N.

But for some simple reason , I can't compile my sketch, it will return me this error,
'xMap' was not declared in this scope.

I did copy/paste this sketch from a website, assuming that the coding was correct.

Any help appreciated, probably something very basic that I don't understand yet,

Thank You

#define MR1 2  //digital pin for controlling polarity motor1
#define MR2 3  //digital pin for controlling polarity motor1
#define ML1 4  //digital pin for controlling polarity motor2
#define ML2 5  //digital pin for controlling polarity motor2
const int ENA = 9;  //PWM pin to control the speed of motor1
const int ENB = 10; //PWM pin to control the speed of motor2

const int SW_pin = 8; //digital pin
const int X_pin = 19; //analog pin
const int Y_pin = 20; //analog pin

void setup()

{
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(9600);
pinMode(MR1, OUTPUT);
pinMode(MR2, OUTPUT);
pinMode(ML1, OUTPUT);
pinMode(ML2, OUTPUT);

pinMode (ENA, OUTPUT);
pinMode (ENB, OUTPUT);
}

void loop() 

{
Serial.print("Switch:  ");
Serial.print(digitalRead(SW_pin));
  
xMap=analogRead(X_pin); 
yMap=analogRead(Y_pin);
xMap1 = map(xMap, 0,1023, 0, 10);
yMap1 = map(yMap,0,1023,0,10);
Serial.print(" | ");
Serial.print("X-axis: ");
Serial.print(xMap1);
Serial.print(" | ");
Serial.print("Y-axis: ");
Serial.print(yMap1);
Serial.println(" | ");
  
if(xMap1==5  && ( yMap1==0 || yMap1 <=3))

{
  // forward
digitalWrite(MR1,HIGH);// clockwise rotation of motor1
digitalWrite(MR2,LOW);
digitalWrite(ML1,HIGH);// clockwise rotation of motor2
digitalWrite(ML2,LOW);
analogWrite(ENA, 255);// speed of motor1
analogWrite(ENB, 255);// speed of motor2

} 
else if(xMap1== 5 && (yMap1>=6 || yMap1 == 10))  
{ 
  //backward
digitalWrite(MR1,LOW);
digitalWrite(MR2,HIGH);// anticlockwise rotation of motor1
digitalWrite(ML1,LOW);
digitalWrite(ML2,HIGH);// anticlockwise rotation of motor2
analogWrite(ENA, 255);// speed of motor1
analogWrite(ENB, 255);// speed of motor2
}

else if((xMap1==0 || yMap1 <=3) && yMap1 ==4) 
{ 
  //left
digitalWrite(MR1,LOW);
digitalWrite(MR2,LOW);
digitalWrite(ML1,HIGH);
digitalWrite(ML2,LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 255);
}
else if((xMap1 == 10) && yMap1 == 4) 
{ 
  // right
digitalWrite(MR1,HIGH);
digitalWrite(MR2,LOW);
digitalWrite(ML1,LOW);
digitalWrite(ML2,LOW);
analogWrite(ENA, 255);
analogWrite(ENB, 0);
}
else  
{ 
  // stop
digitalWrite(MR1,LOW);
digitalWrite(MR2,LOW);
digitalWrite(ML1,LOW);
digitalWrite(ML2,LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0); 

  
}
}

Where is xMap defined :face_with_monocle:

1 Like

Above void setup() write a line like int xMap; Same for yMap

1 Like

Thank You, that was indeed very simple

Since 0 is less than 3, this expression can be simplified to:
yMap1 <= 3

Similarly, this can be simplified to:
yMap1 >= 6

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.