I need a code when I press the pushbutton and change the position of the select

I need a code when I press the pushbutton and change the position of the selector switch to another position the dc motor will change the directions by using L298n .

I need a code when I press the pushbutton and change the position of the selector switch to another position the dc motor will change the directions by using L298n .Also, I used potentiometer to control the speed of dc motor (forward)

Here is my code:
int In1 =7;
int In2 =8;
int ENA =5;
int SPEED ;
int potVal = A0; // the number of the potentiometer pin

const int pushButton =10; // the number of the pushbutton pin
const int swi_pos2 =11; // the number of the selector switch pin
const int swi_pos3 =12; // the number of the selector switch pin
const int swi_pos4 =13; // the number of the selector switch pin
// variables will change:
int pushState=0 ;
int slecSwitch2= 0;
int slecSwitch3= 0 ;
int slecSwitch4 =0;

void setup()
{
int potVal = analogRead(A0); // Read potentiometer value
//int SPEED = map(potVal,0,1023 , 0 , 255); // Map the potentiometer value from 0 to 255
analogWrite(ENA, SPEED); // Send PWM signal to L298N Enable pin

// put your setup code here, to run once:
pinMode (In1 , OUTPUT);
pinMode (In2 , OUTPUT);
pinMode (ENA , OUTPUT);
pinMode(pushButton, INPUT);
pinMode (swi_pos2, INPUT);
pinMode (swi_pos3, INPUT);
pinMode (swi_pos4, INPUT);
}
void loop()
{
pushState = digitalRead (pushButton);
slecSwitch2 = digitalRead (swi_pos2);
slecSwitch3 = digitalRead (swi_pos3);
slecSwitch4 = digitalRead (swi_pos4);

// put your main code here, to run repeatedly:
if ( pushState == HIGH && slecSwitch2 == HIGH)
{
// delay(2000);
digitalWrite(In2,HIGH); // turn direction for forward direction
digitalWrite(In1,LOW);//motor voltage on
}

if (pushState== HIGH && slecSwitch3 == HIGH)
{
delay(2000);
digitalWrite(In2,LOW); // turn direction for reverse direction
digitalWrite(In1,HIGH);//motor voltage on
}

}

Hi and welcome to the forum! Before you make any more posts, please read how to use the forum. And would you be so kind to alter your post accordingly? Especially the part about code tags.

But what have you tried? And what needs to happen in all other situations?

  • What should happen when the switch is moved but the button not pressed?
  • What should happen when the button is presses but the switch not moved
  • Should the motor ever stop?
  • Should the motor turn when the Arduino is turned on? If yes, which direction?
  • When should it react to the pot?
  • From code it looks like the selector switch has more than two positions, so what should happen when?

PS You mother never learned you to say please? ::slight_smile: You would like that code :wink: