سؤال

I have just started Arduino programming a few days ago and now I want to transfer potentiometer reading to a mobile device. I am using Arduino Leonardo and Bluetooth Mate Silver and the following code to transfer data:

    #include <SoftwareSerial.h>  

int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3;  // RX-I pin of bluetooth mate, Arduino D3
int val;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  Serial.begin(9600);  // Begin the serial monitor at 9600bps
  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$");  // Print three times individually
  bluetooth.print("$");
  bluetooth.print("$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  bluetooth.begin(9600);  // Start bluetooth serial at 9600
}

void loop()
{
  int val = analogRead(A0);

  if(bluetooth.available())  // If the bluetooth sent any characters
  {
    Serial.print(val);  
  }
  if(Serial.available())  // If stuff was typed in the serial monitor
  {
      bluetooth.print(val);
  }
  delay(1000);
  // and loop forever and ever!
}

I found that bluetooth connection estublished without any error, however I am not getting any data on mobile device. I changed the code (e.g., without the if statement/ printing potentiometer data on serial monitor and read the data from serial monitor to the transfer into mobile device), only I get the data in mobile device if I enter any values in serial monitor. I would highly appreciate if anyone could suggest me possible solutions to solve it.

لا يوجد حل صحيح

نصائح أخرى

 if(bluetooth.available())  // If the bluetooth sent any characters
  {
    Serial.print(val);  
  }
  if(Serial.available())  // If stuff was typed in the serial monitor
  {
      bluetooth.print(val);
  }

This is apparently broken in two ways:

  1. You make generating output contingent on unrelated input data being available. There's no apparent reason why receiving bluetooth should trigger local printing of the potentiometer value on the serial monitor, or why receiving from the serial monitor should trigger bluetooth printing of the potentiometer value.

  2. You are checking the serial and bluetooth available() methods, but you are never reading the characters. Thus the input buffers will probably overflow.

Likely your best solution would be to simply remove the if statements and always echo the value to both outputs in a loop, ignoring the inputs - unless there's some reason those are important.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top