2

I'm using this HC-05 Bluetooth module to communicate between my Arduino mega 2560 and my laptop. I am using processing to send the data from my computer to the Arduino.

The module connects nicely and start blinking and when it is paired with my computer it blinks twice every seconds. The problem is that every time the module blinks twice, it ignores the data I am trying to send. Everything else seems to work fine.

Does anyone why this is happening and what I can do to avoid it?

Here is the arduino code:

/*
 * bluetooth_rbg_led.ino
 * Uses bluetooth communication to adjust the
 * values of an rgb LED.
 * 
 * Sahibjot Singh
 * July 2021
 */


// Servo library to control servos
#include <Servo.h>

// Global variables
Servo servo;
const byte servoPin = 2;
int servoAngle;

// Exeuctes once, at the start
void setup() {
  // Attaches the servo to it's pin
  servo.attach(servoPin);
  servo.write(0);

  // Begins the serial communication with the
  // bluetooth module at pins 18 and 19
  Serial1.begin(9600);
}

// Loops infinitely
void loop() {
  // Reads the values from the serial port
  if (Serial1.available()) {
    servoAngle = Serial1.read();
  }

  // Writes the value to the led pin
  servo.write(servoAngle);
}

Here's the processing code:

/*
 * bluetooth_rgb_led.pde
 * Simple program to take user input and
 * write it to the serial port.
 *
 * Sahibjot Singh
 * July 2021
 */

// The library needed to write to the serial port
import processing.serial.*;

// Serial variables
Serial port;
int servoAngle;
int userInput;

// Slider variables
int sliderX = 100;
int sliderY = 195;
int sliderWidth = 400;
int sliderHeight = 10;

// Slider rect variables
int sliderRectX = 100;
int sliderRectY = 175;
int sliderRectWidth = 50;
int sliderRectHeight = 50;

// Executes once, at the start
void setup() {
  // Makes the window 600 pixels by 600 pixels
  size(600, 600);
  
  // Starts serial communication
  port = new Serial(this, "COM7", 9600);
  port.bufferUntil('\n');
}

// Loops infinitely
void draw() { 
  // Gets the input from the slider
  if (mousePressed && mouseX > sliderX && mouseX < sliderX + sliderWidth &&
      mouseY > sliderY - 20 && mouseY < sliderY + sliderHeight + 20) {
      sliderRectX = mouseX - sliderRectWidth / 2;
      userInput = sliderRectX - (sliderX - sliderRectWidth / 2);
      servoAngle = (int)map(userInput, 0, 400, 0, 180);
  }
  
  // Writes the LED value to the serial port
  port.write(char(servoAngle));
  
  // Clears the window
  clear();
  background(200, 200, 200);
  
  // Draws the slider
  stroke(30);
  strokeWeight(2);
  fill(150, 150, 150);
  rect(sliderX, sliderY, sliderWidth, sliderHeight, 10);
  fill(255, 0, 0);
  rect(sliderRectX, sliderRectY, sliderRectWidth, sliderRectHeight, 10);
  
  // Draws the text on the side of the slider
  fill(255, 255, 255);
  textSize(32);
  text("0", 50, 210);
  text("180", 530, 210);
  
  // Draws the text about the led's brigthness
  fill(0, 0, 255);
  textSize(40);
  text("Servo Angle: " + Integer.toString(servoAngle), 100, 320);
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.