I am using Arduino Nano, Bluetooth module HC-05 and one push button. I want to rotate the motor when it receives signal from the Bluetooth and stop the rotation when the push button is pressed but here the Bluetooth signal should not be disconnected the motor should stop its rotation when Bluetooth is connected and the button is pressed. The problem is when signal is passed through Bluetooth or serial , the motor rotates but when we push the button to stop the rotation the motor doesn't stops. Below is what I tried.
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth(10,6);
const int buttonPin = 5; // the pin that the pushbutton is attached to
const int motorPin = 9;
const int ledPin = LED_BUILTIN; // the pin that the LED is attached to
int buttonState = 0; // current state of the button
int Data;
void setup() {
Serial.begin(9600);
Bluetooth.begin(9600);
Bluetooth.println("Send 1 to open LOCK. Send 0 to close LOCK");
Serial.println("Send 1 to open LOCK. Send 0 to close LOCK");
delay(1000);
Bluetooth.println("Waiting for command...");
Serial.println("Waiting for command...");
pinMode(buttonPin, INPUT);
pinMode(motorPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
if (Bluetooth.available())
{
Data=Bluetooth.read();
if(Data=='1'){
Serial.println("Motor rotating");
Serial.println(buttonState);
digitalWrite(motorPin, HIGH);
}
if (Data=='1' and buttonState == 1){
Serial.println("Motor stop");
Serial.println(buttonState);
digitalWrite(motorPin, LOW);
digitalWrite(ledPin, HIGH);
}
else{;}
}
}
I tried only to control with the push button only which works well. When the button is pushed it motor stops and when unpushed it rotates.