我的 Arduino 正在发送一个信号,但没有发送另一个信号。似乎与代码有关
My Arduino is sending one signal but not sending the other one.. seems to be something with the code
我的 android 工作室代码是:
bluetooth_connect_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
if(BTinit())
{
BTconnect();
beginListenForData();
// The code below sends the number 3 to the Arduino asking it to send the current state of the door lock so the lock state icon can be updated accordingly
command = "3";
try
{
outputStream.write(command.getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
});
lock_state_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
if(connected == false)
{
Toast.makeText(getApplicationContext(), "Please establish a connection with the bluetooth servo door lock first", Toast.LENGTH_SHORT).show();
}
else
{
command = "1";
try
{
outputStream.write(command.getBytes()); // Sends the number 1 to the Arduino. For a detailed look at how the resulting command is handled, please see the Arduino Source Code
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
});
}
void beginListenForData() // begins listening for any incoming data from the Arduino
{
final Handler handler = new Handler();
stopThread = false;
buffer = new byte[1024];
Thread thread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopThread)
{
try
{
int byteCount = inputStream.available();
if(byteCount > 0)
{
byte[] rawBytes = new byte[byteCount];
inputStream.read(rawBytes);
final String string = new String(rawBytes, "UTF-8");
handler.post(new Runnable()
{
public void run()
{
if(string.equals("3"))
{
lock_state_text.setText("Lock State: LOCKED"); // Changes the lock state text
lock_state_img.setImageResource(R.drawable.locked_icon); //Changes the lock state icon
}
else if(string.equals("4"))
{
lock_state_text.setText("Lock State: UNLOCKED");
lock_state_img.setImageResource(R.drawable.unlocked_icon);
}
}
});
}
}
catch (IOException ex)
{
stopThread = true;
}
}
}
});
thread.start();
}
我的 Arduino 代码是:
#include <Servo.h>
#include <EEPROM.h>
Servo servo;
char state;
void setup() {
EEPROM.write(0,2);
// put your setup code here, to run once:
servo.attach(7);
if(EEPROM.read(0) == 1) // Reads the EEPROM value stored to know what state the door lock was in before it was last turned off
{ // An EEPROM value of 1 means UNLOCKED and a value of 2 means LOCKED
servo.write(0); // Rotates the servo to the unlocked position
delay(200);
}
else if(EEPROM.read(0) == 2)
{
servo.write(75); // Rotates the servo to the locked position
delay(200);
}
Serial.begin(9600);
}
void loop() {
EEPROM.write(0,2);
// put your main code here, to run repeatedly:
if(Serial.available() > 0)
{
char data;
data = Serial.read(); // The variable data is used to store the value sent by the Android app
switch(data)
{
case '1':
if(EEPROM.read(0) == 1) //An EEPROM value of 1 means it is currently unlocked
{
EEPROM.write(0, 2); // Writes the number 2 to address 0 on the Arduino's EEPROM. This value will be used by the Arduino to remember the last state the door lock was in
Serial.print("3"); // Sends the number 3 to the Android app. To see what this does, please see the Android Studio Project file
servo.write(75);
delay(15);
}
else if(EEPROM.read(0) == 2) //An EEPROM value of 2 means it i currently locked
{
EEPROM.write(0, 1); // Writes the number 1 to address 0 on the Arduino's EEPROM. This value will be used by the Arduino to remember the last state the door lock was in
Serial.print("4"); // Sends the number 4 to the Android app. The number sent will be used by the app to update the locked/unlocked icon
servo.write(0);
delay(15);
}
break;
case '3':
if(EEPROM.read(0) == '1')
{
Serial.print("4");
}
else if(EEPROM.read(0) == '2')
{
Serial.print("3");
}
break;
}
}
}
我已经制作了 apk 文件并在 arduino uno 上上传了 arduino 代码(因此这两个代码都没有错误)。现在,当我点击连接时,它会建立连接,然后当我点击 lock_state_btn 时,arduino 会向伺服电机发送信号以改变其位置,同时还会发回信号来改变图标。但是当我点击lock_state_btn 再次,它应该再次改变它没有做的位置。有人可以帮我吗?
在 switch
中测试时,您的 EEPROM 变量始终包含 2
。
考虑一下我从您的 Arduino 程序中取出的这段代码:
void loop() {
// EEPROM (0) value could be 1 or 2
EEPROM.write(0,2); // <—
// EEPROM value is 2
switch(data)
{
case '1':
if(EEPROM.read(0) == 1) // newer executed
{
EEPROM.write(0, 2);
}
else if(EEPROM.read(0) == 2) // always executed, independently from actual servo position
{
EEPROM.write(0, 1);
// EEPROM value is 1, but will be overwritten at the beginning of he next loop execution
// (i.e. before any new call to this switch statement)
}
break;
}
您只需删除标有 // <—
的行即可使您的程序运行。
我的 android 工作室代码是:
bluetooth_connect_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
if(BTinit())
{
BTconnect();
beginListenForData();
// The code below sends the number 3 to the Arduino asking it to send the current state of the door lock so the lock state icon can be updated accordingly
command = "3";
try
{
outputStream.write(command.getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
});
lock_state_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
if(connected == false)
{
Toast.makeText(getApplicationContext(), "Please establish a connection with the bluetooth servo door lock first", Toast.LENGTH_SHORT).show();
}
else
{
command = "1";
try
{
outputStream.write(command.getBytes()); // Sends the number 1 to the Arduino. For a detailed look at how the resulting command is handled, please see the Arduino Source Code
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
});
}
void beginListenForData() // begins listening for any incoming data from the Arduino
{
final Handler handler = new Handler();
stopThread = false;
buffer = new byte[1024];
Thread thread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopThread)
{
try
{
int byteCount = inputStream.available();
if(byteCount > 0)
{
byte[] rawBytes = new byte[byteCount];
inputStream.read(rawBytes);
final String string = new String(rawBytes, "UTF-8");
handler.post(new Runnable()
{
public void run()
{
if(string.equals("3"))
{
lock_state_text.setText("Lock State: LOCKED"); // Changes the lock state text
lock_state_img.setImageResource(R.drawable.locked_icon); //Changes the lock state icon
}
else if(string.equals("4"))
{
lock_state_text.setText("Lock State: UNLOCKED");
lock_state_img.setImageResource(R.drawable.unlocked_icon);
}
}
});
}
}
catch (IOException ex)
{
stopThread = true;
}
}
}
});
thread.start();
}
我的 Arduino 代码是:
#include <Servo.h>
#include <EEPROM.h>
Servo servo;
char state;
void setup() {
EEPROM.write(0,2);
// put your setup code here, to run once:
servo.attach(7);
if(EEPROM.read(0) == 1) // Reads the EEPROM value stored to know what state the door lock was in before it was last turned off
{ // An EEPROM value of 1 means UNLOCKED and a value of 2 means LOCKED
servo.write(0); // Rotates the servo to the unlocked position
delay(200);
}
else if(EEPROM.read(0) == 2)
{
servo.write(75); // Rotates the servo to the locked position
delay(200);
}
Serial.begin(9600);
}
void loop() {
EEPROM.write(0,2);
// put your main code here, to run repeatedly:
if(Serial.available() > 0)
{
char data;
data = Serial.read(); // The variable data is used to store the value sent by the Android app
switch(data)
{
case '1':
if(EEPROM.read(0) == 1) //An EEPROM value of 1 means it is currently unlocked
{
EEPROM.write(0, 2); // Writes the number 2 to address 0 on the Arduino's EEPROM. This value will be used by the Arduino to remember the last state the door lock was in
Serial.print("3"); // Sends the number 3 to the Android app. To see what this does, please see the Android Studio Project file
servo.write(75);
delay(15);
}
else if(EEPROM.read(0) == 2) //An EEPROM value of 2 means it i currently locked
{
EEPROM.write(0, 1); // Writes the number 1 to address 0 on the Arduino's EEPROM. This value will be used by the Arduino to remember the last state the door lock was in
Serial.print("4"); // Sends the number 4 to the Android app. The number sent will be used by the app to update the locked/unlocked icon
servo.write(0);
delay(15);
}
break;
case '3':
if(EEPROM.read(0) == '1')
{
Serial.print("4");
}
else if(EEPROM.read(0) == '2')
{
Serial.print("3");
}
break;
}
}
}
我已经制作了 apk 文件并在 arduino uno 上上传了 arduino 代码(因此这两个代码都没有错误)。现在,当我点击连接时,它会建立连接,然后当我点击 lock_state_btn 时,arduino 会向伺服电机发送信号以改变其位置,同时还会发回信号来改变图标。但是当我点击lock_state_btn 再次,它应该再次改变它没有做的位置。有人可以帮我吗?
在 switch
中测试时,您的 EEPROM 变量始终包含 2
。
考虑一下我从您的 Arduino 程序中取出的这段代码:
void loop() {
// EEPROM (0) value could be 1 or 2
EEPROM.write(0,2); // <—
// EEPROM value is 2
switch(data)
{
case '1':
if(EEPROM.read(0) == 1) // newer executed
{
EEPROM.write(0, 2);
}
else if(EEPROM.read(0) == 2) // always executed, independently from actual servo position
{
EEPROM.write(0, 1);
// EEPROM value is 1, but will be overwritten at the beginning of he next loop execution
// (i.e. before any new call to this switch statement)
}
break;
}
您只需删除标有 // <—
的行即可使您的程序运行。