Controlling L298n with button

Hello,

I want to use a button as on/off switch so when i press the button the motor should start spinning. when ever i press the button again it should stop spinning. I used the example StateChangeDetection. but right now it doesnt work. could someone help me?

Thanks for your time.

const int buttonPin = 2;

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int motor1pin1 = 0;
int motor1pin2 = 0;

void setup() {
  // put your setup code here, to run once:
pinMode (buttonPin, INPUT);
pinMode (motor1pin1, OUTPUT);
pinMode (motor1pin2, OUTPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
  if (buttonState == HIGH) {
    buttonPushCounter++;
    Serial.println("on");
    Serial.print ("1:");
    Serial.println(buttonPushCounter);    
  } else {
    Serial.println("off");
  }
  delay (50);
}
lastButtonState = buttonState;

if (buttonPushCounter % 1 == 0) {
  digitalWrite (motor1pin1, HIGH);
  digitalWrite (motor1pin2, LOW);
} else {
  digitalWrite (motor1pin1, LOW);
  digitalWrite (motor1pin2, LOW);
}
}

it doesnt work.

That tells us nothing

What does it do or not do ?

int motor1pin1 = 0;
int motor1pin2 = 0;

Really ?
Two connections to the same wrong pin ?

I changed it. but it still doesnt work. this is my first time writing a code

const int buttonPin = 2;

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int motor1pin1 = 5;
int motor1pin2 = 4;

void setup() {
  // put your setup code here, to run once:
pinMode (buttonPin, INPUT);
pinMode (motor1pin1, OUTPUT);
pinMode (motor1pin2, OUTPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
  if (buttonState == HIGH) {
    buttonPushCounter++;
    Serial.println("on");
    Serial.print ("1:");
    Serial.println(buttonPushCounter);    
  } else {
    Serial.println("off");
  }
  delay (50);
}
lastButtonState = buttonState;

if (buttonPushCounter % 1 == 0) {
  digitalWrite (motor1pin1, HIGH);
  digitalWrite (motor1pin2, LOW);
} else {
  digitalWrite (motor1pin1, LOW);
  digitalWrite (motor1pin2, LOW);
}
}

The two motor pins must be DIFFERENT pins and it's never a good idea to use pin 0 or pin 1.

Try pins 8 and 9 and make sure you have those Arduino pins connect to the corresponding L298N pins.

Steve

Thanks for your reply,

i changed the inputs to 4,5 and connected them to the l298n but it doesnt work yet.

const int buttonPin = 2;

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int motor1pin1 = 5;
int motor1pin2 = 4;

void setup() {
  // put your setup code here, to run once:
pinMode (buttonPin, INPUT);
pinMode (motor1pin1, OUTPUT);
pinMode (motor1pin2, OUTPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
  if (buttonState == HIGH) {
    buttonPushCounter++;
    Serial.println("on");
    Serial.print ("1:");
    Serial.println(buttonPushCounter);    
  } else {
    Serial.println("off");
  }
  delay (50);
}
lastButtonState = buttonState;

if (buttonPushCounter % 1 == 0) {
  digitalWrite (motor1pin1, HIGH);
  digitalWrite (motor1pin2, LOW);
} else {
  digitalWrite (motor1pin1, LOW);
  digitalWrite (motor1pin2, LOW);
}
}

So what is happening? What are you seeing in Serial monitor and is that what you expect?

A schematic/circuit diagram would be useful. Also whether you are using a bare L298N chip or a module and if a module which one.

Steve

Post a schematic of your project

Forget the pushbuttons for now and write a small sketch that drives the motor in one direction for 5 seconds then in reverse for 5 seconds then repeats

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