Skip to content

Instantly share code, notes, and snippets.

@elktros
Created January 9, 2018 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elktros/99e3346bc44d02397f1a1ede9db5fbee to your computer and use it in GitHub Desktop.
Save elktros/99e3346bc44d02397f1a1ede9db5fbee to your computer and use it in GitHub Desktop.
Arduino code for controlling DC Motor with L298N Motor Driver.
int mot1 = 8;
int mot2 = 9;
int en1 = 10;
int dir = 6;
bool state = true;
int nob = A0;
int val=0;
void setup()
{
pinMode(mot1,OUTPUT);
pinMode(mot2,OUTPUT);
pinMode(en1,OUTPUT);
pinMode(dir,INPUT_PULLUP);
}
void loop()
{
val = analogRead(nob);
analogWrite(en1, val / 4);
if(digitalRead(dir)==LOW)
{
state=!state;
while(dir==LOW);
delay(300);
}
if(state)
{
digitalWrite(mot1,HIGH);
digitalWrite(mot2,LOW);
}
else
{
digitalWrite(mot1,LOW);
digitalWrite(mot2,HIGH);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment