Why doesn't my code work?

Hi, I'm new to electronics and everything Arduino related, but I have made a device that detects movement with an accelerometer and an Arduino Micro which then should send the data(what it reads from the accelerometer) via Bluetooth to my phone. For some reason it doesn't do the last part (sending the data to my phone). I'm using the Serial Bluetooth Monitor by Kai Morich, and the module connection works fine with the phone. The serial monitor part of the code also works fine(in that it prints the data). Any ideas, what the problem is?

#include <SoftwareSerial.h>           // this is for BT
SoftwareSerial Bluetooth(2,3);        // this is for BT PINS: 2,3

String device_name = "ShockCheck";
char c = ' ';                         // this is for BT stores response of BT
const char *pin = "1234";
const int groundpin = A1;             // analog input pin 4 -- ground
const int powerpin = A0;              // analog input pin 5 -- voltage
const int zpin = A2;                  // z-axis (only on 3-axis models)
const int ypin = A3;                  // y-axis
const int xpin = A4;                  // x-axis of the accelerometer
const int led1Pin = 4 ;
const int led2Pin = 5;  
const int MeasurableDifference = 100;
int previousValue = 0; 
int currentValue = 0; 

 
void setup() {
  
  Serial.begin(9600);
  Serial.begin(9600);
  // Serial.begin(device_name);         THIS IS FOR LATER TO NAME THE DEVICE
  Serial.println("ready");            // this is for BT
  Bluetooth.begin(38400);             // this is for BT
  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(groundpin, LOW);
  digitalWrite(powerpin, HIGH);
  digitalWrite(led1Pin, LOW);  
  digitalWrite(led2Pin, LOW);
  #ifdef USE_PIN
    SerialBT.setPin(pin);
    Serial.println("Using PIN");
  #endif

  previousValue = analogRead(zpin);

}

void loop() {
  if (Bluetooth.available())
  {
    c=Bluetooth.read();
    Serial.write(c);
  }
  
  if(Serial.available()) 
  {
    c= Serial.read();
    Bluetooth.write(c);
  }
  
  currentValue = analogRead(zpin); 
  int difference = abs(currentValue - previousValue); 
  Serial.print(difference);
  Serial.print("\t");
  
  if (difference > MeasurableDifference) {
    digitalWrite(led1Pin, HIGH);
  
     if (difference > MeasurableDifference + 50) {
      digitalWrite(led2Pin, HIGH);
    }
  }
  else {
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
  }

  previousValue = currentValue; 

  Bluetooth.print(analogRead(xpin)); //THIS IS THE PART I AM INTERESTED IN
  Serial.print(analogRead(xpin));
  Bluetooth.print("\t");
  Serial.print("\t");
  Bluetooth.print(analogRead(ypin));
  Serial.print(analogRead(ypin));
  Bluetooth.print("\t");
  Serial.print("\t");
  Bluetooth.print(analogRead(zpin));
  Serial.print(analogRead(zpin));
  Bluetooth.println();
  Serial.println();

  delay(2000);
}
#include <SoftwareSerial.h>           // this is for BT
SoftwareSerial Bluetooth(2, 3);       // this is for BT PINS: 2,3

String device_name = "ShockCheck";
char c = ' ';                         // this is for BT stores response of BT
const char *pin = "1234";
const int groundpin = A1;             // analog input pin 4 -- ground
const int powerpin = A0;              // analog input pin 5 -- voltage
const int zpin = A2;                  // z-axis (only on 3-axis models)
const int ypin = A3;                  // y-axis
const int xpin = A4;                  // x-axis of the accelerometer
const int led1Pin = 4 ;
const int led2Pin = 5;
const int MeasurableDifference = 100;
int previousValue = 0;
int currentValue = 0;


void setup() {
  Serial.begin(9600);
  // Serial.begin(device_name);         THIS IS FOR LATER TO NAME THE DEVICE
  Serial.println("ready");           
  Bluetooth.begin(38400);            
  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(groundpin, LOW);
  digitalWrite(powerpin, HIGH);
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, LOW);
#ifdef USE_PIN
  SerialBT.setPin(pin);
  Serial.println("Using PIN");
#endif

  previousValue = analogRead(zpin);

}

void loop() {
  if (Bluetooth.available())
  {
    c = Bluetooth.read();
    Serial.write(c);
  }

  if (Serial.available())
  {
    c = Serial.read();
    Bluetooth.write(c);
  }

  currentValue = analogRead(zpin);
  int difference = abs(currentValue - previousValue);
  Serial.print(difference);
  Serial.print("\n");

  if (difference > MeasurableDifference) {
    digitalWrite(led1Pin, HIGH);

    if (difference > MeasurableDifference + 50) {
      digitalWrite(led2Pin, HIGH);
    }
  }
  else {
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
  }

  previousValue = currentValue;

  int a = analogRead(xpin); //THIS IS THE PART I AM INTERESTED IN
  int b = analogRead(ypin);
  int c = analogRead(zpin);
  char bufer[30];
  sprintf(bufer, "%d\t%d\t%d", a, b, c);
  Bluetooth.println(bufer);
  Serial.println(bufer);

  delay(2000);
}

Why use SoftwareSerial if you have the TX and RX pins on the Arduino Micro? They are independent of the USB communication; access via Serial1.

SoftwareSerial might not be reliable on 38400, 19200 might be a safer choice. Did you configure the BT module to use 38400?

I don't have further knowledge of Bluetooth so can't be of further help.

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