在收到来自蓝牙的信号时旋转电动机,并在按下按钮时停止电动机

时间:2019-09-13 07:37:56

标签: arduino bluetooth arduino-ide

我正在使用蓝牙HC-O5,按钮,Arduino Nano和40rpm的直流电动机。我先解释我的问题。 (情况1)我想在收到来自蓝牙的信号时开始旋转电动机,并在按下按钮时停止电动机旋转。 (第2种情况)我想在按下按钮时开始旋转电动机,并从蓝牙接收信号,并在按下按钮时停止旋转。

现在,我使用Arduino IDE进行了编码,其中案例1在案例2中运行良好,电动机在不按下按钮的情况下旋转和停止。 我的代码在下面。

#include <SoftwareSerial.h>

SoftwareSerial Bluetooth(10,6);
const int  buttonPin = 3;    // 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 currentState = 0;
int lastState = 0;
int Data;
int c_lastState = 0;


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);
  Bluetooth.available();  
  Data=Bluetooth.read(); 
    if(Data=='1'){
      Serial.println("Motor rotating");
      digitalWrite(motorPin, HIGH);
      currentState = buttonState;
      lastState = 0; 
      digitalWrite(ledPin, LOW);
      Serial.print("Button State - ");
      Serial.println(buttonState);
      Serial.print("Current Button State - ");
      Serial.println(currentState);
      Serial.print("Last Button State - ");
      Serial.println(lastState);
     // lastState == 0;
      //Serial.print("Changed Last Button State - ");
      //Serial.println(lastState);
    }

    else if (buttonState == 1 and lastState == 0){
      Serial.println("Motor Stop");
      digitalWrite(motorPin, LOW);
      currentState = buttonState;
      lastState = currentState; 
      digitalWrite(ledPin, HIGH);
      Serial.println(buttonState);
      Serial.print("Button State - ");
      Serial.println(buttonState);
      Serial.print("Current Button State - ");
      Serial.println(currentState);
      Serial.print("Last Button State - ");
      Serial.println(lastState);
    }


    if(Data=='1' and buttonState == 1){
      Serial.println("Motor rotating..............................");
      digitalWrite(motorPin, HIGH);
      currentState = buttonState;
      lastState = currentState; 
      digitalWrite(ledPin, LOW);
      Serial.print("Button State - ");
      Serial.println(buttonState);
      Serial.print("Current Button State - ");
      Serial.println(currentState);
      Serial.print("Last Button State - ");
      Serial.println(lastState);
      c_lastState = 1;
      Serial.print("Changed Last Button State - ");
      Serial.println(c_lastState);
    }


    else if (buttonState == 1 and c_lastState == 0){
      Serial.println("Motor Stop");
      digitalWrite(motorPin, LOW);
      currentState = buttonState;
      lastState = currentState; 
      c_lastState = lastState;
      digitalWrite(ledPin, HIGH);
      Serial.println(buttonState);
      Serial.print("Button State - ");
      Serial.println(buttonState);
      Serial.print("Current Button State - ");
      Serial.println(currentState);
      Serial.print("Last Button State - ");
      Serial.println(lastState);
      Serial.print("Changed Last Button State - ");
      Serial.println(c_lastState);
    }
}

在这里,由于所使用的变量造成了问题,但是如果不使用,我将无法解决情况1,因此我使用了它。如果还有其他解决方法,请告诉我。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码通过蓝牙和按钮来启动和停止电动机(仅在发生数据更改或按钮变为高电平时才启动或停止)。

#include <SoftwareSerial.h>

SoftwareSerial Bluetooth(10,6);
const int  buttonPin = 3;    // 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 running=0;
int lastState = 0;
int data=0;
int lastData=0;


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() {

    // start and stop with bluetooth when data changed to 1
  Bluetooth.available();  
// read the data:
  data = Bluetooth.read();

  // compare the data to its previous state
  if (data != lastData) {
    // the state has changed
    if (data == 1) {
      // if the current state is 1 
      if(running)
      {
          //stop if running already
          Serial.println("Motor Stop");
          digitalWrite(motorPin, LOW);
          running = 0;
      }
        else
        {
            //start if not running
            Serial.println("Motor start");
          digitalWrite(motorPin, HIGH);
            running = 1;
        }
    } 

    // Delay to avoid bouncing
    delay(50);
  }
  // save the current state as the last state
 lastData = data;

// start and stop with pushbutton when it changed to HIGH

// read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // the state has changed
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      if(running)
      {
          Serial.println("Motor Stop");
          digitalWrite(motorPin, LOW);
          running = 0;
      }
        else
        {
            Serial.println("Motor start");
          digitalWrite(motorPin, HIGH);
            running = 1;
        }
    } 

    // Delay to avoid bouncing
    delay(50);
  }
  // save the current state as the last state
 lastButtonState = buttonState;

}