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
}
}