Arduino not switching to automated mode when disconnected from bluetooth

Hi All,

I have a code that works perfectly as long as I am connected to the HC06 Bluetooth module via he Serial Bluetooth Terminal app on my phone. As soon as I go out of range the programmed sequence stops working.

The program is set to do a rotation sequence on the hour every hour. I use the Bluetooth commands to set the time and move the 'clock' into the correct position.

I have very limited knowledge of programming. My questions are:

A. does the current code attached specify to continue previous inputs via bluetooth when bluetooth is disconnected (i.e meaning I have connected the wrong wires somewhere)

B. if the current code does include this condition can I add the linked code?

C. where do I add this code - I don't know how to combine the current code with the new condition.

Any assistance would be greatly appreciated.

CURRENT CODE:

#include <SoftwareSerial.h>
#include "ds1307.h"

#define SEQ_FLAG_ADDR 0x03
#define PREV_MOVE_HOUR_ADDR 0x04

//Stepper Outputs step2,dir3
const uint8_t step_pin = 3;
const uint8_t dir_pin = 6;
const uint8_t enable_pin = 8;
uint8_t seq_flag = 0;

//Timing parameters
// const uint32_t time_between_rotations_ms = 3600*1000;
// uint32_t prev_ms = 0;
uint8_t prev_hr = 0;

//Stepper parameters
const uint16_t steps_per_rev = 1000;
const uint16_t rev_ratio = 10;
const float steps_per_deg = rev_ratio*steps_per_rev/360.0;
const uint16_t hourly_steps = (uint16_t) 390*steps_per_deg;

//Stepper Acceleration Profile
const uint16_t accel_steps = 600;
const uint16_t decel_steps = accel_steps;
const uint16_t min_speed_step_period_us = 10000;
const uint16_t max_speed_step_period_us = 2500;
const uint16_t step_period_delta_us = (uint16_t)((min_speed_step_period_us-max_speed_step_period_us)/accel_steps);

//Bluetooth Serial Setup
const uint8_t HC06_TXD_PIN = 11; //Connect to TX pin on HC06
const uint8_t HC06_RXD_PIN = 5; //Connect to RX pin on HC06
const uint8_t RX_BUFFER_SIZE = 16;

//Bluetooth Commands
const char START_CMD[] = {'s','t','a','r','t'};
const char STOP_CMD[] = {'s','t','o','p'};
const char MOVF_CMD[] = {'m','o','v','f'};
const char MOVB_CMD[] = {'m','o','v','b'};
const char SKIP_CMD[] = {'s','k','i','p'};
const char SET_MIN_CMD[] = {'s','e','t','m'};
const char SET_HR_CMD[] = {'s','e','t','h'};

//Instantiate SoftwareSerial.
SoftwareSerial hc06(HC06_TXD_PIN,HC06_RXD_PIN);
char rx_buffer[RX_BUFFER_SIZE];
uint8_t cmd_len = 0;

const char end_marker = '\n';
uint8_t rx_handle()
{
  static uint8_t ndx = 0;
  char rc;
  while (hc06.available() > 0)
  {
    rc = hc06.read();
    //If no newline yet
    if (rc != end_marker)
    {
      rx_buffer[ndx] = rc;
      ndx++;
      if (ndx >= RX_BUFFER_SIZE)
      {
        ndx = RX_BUFFER_SIZE - 1;
      }
    }
    else
    {
      rx_buffer[ndx] = '\0'; // terminate the string
      cmd_len = ndx;
      ndx = 0;
      return cmd_len;
    }
  }
  return 0;
}

void move(uint8_t dir, uint32_t steps)
{
  //Enable the driver
  digitalWrite(enable_pin,1);
  delay(10);

  //Set direction
  digitalWrite(dir_pin,dir);

  //If number of steps is too tiny, just move at the constant mininum rate.
  if (steps < (accel_steps + decel_steps))
  {
    for (;steps>0;steps--)
    {
      digitalWrite(step_pin,1);
      delayMicroseconds(min_speed_step_period_us);
      digitalWrite(step_pin,0);
      delayMicroseconds(min_speed_step_period_us);
    }
  }
  else
  {
    uint16_t step_period_us = min_speed_step_period_us;
    steps-=(accel_steps+decel_steps);
    //Accelerate   
    for (int i = accel_steps; i>0; i--)
    {
      digitalWrite(step_pin,1);
      delayMicroseconds(step_period_us);
      digitalWrite(step_pin,0);
      delayMicroseconds(step_period_us);
      step_period_us-=step_period_delta_us;
    }
    //Constant Velocity
    for (int i = steps; i>0; i--)
    {
      digitalWrite(step_pin,1);
      delayMicroseconds(step_period_us);
      digitalWrite(step_pin,0);
      delayMicroseconds(step_period_us);
    }
    //Decelerate
    for (int i = decel_steps; i>0; i--)
    {
      digitalWrite(step_pin,1);
      delayMicroseconds(step_period_us);
      digitalWrite(step_pin,0);
      delayMicroseconds(step_period_us);
      step_period_us+=step_period_delta_us;
    }
  }
  delay(10);
  
  //Disable the driver
  digitalWrite(enable_pin,0);

}

void setup()
{
  //Setup outputs
  pinMode(enable_pin, OUTPUT);
  pinMode(step_pin, OUTPUT);
  pinMode(dir_pin, OUTPUT);

  //Connect I2C
  Wire.begin();
  
  //Start serial comms with the hc06
  hc06.begin(9600);
  Serial.begin(9600);
  //Enable the clock.
  enable_clk();
  //Check DS1307 RAM if the microcontroller is in moving sequence. 
  seq_flag = get_ram(SEQ_FLAG_ADDR);
  //Prev hour  
  prev_hr = get_ram(PREV_MOVE_HOUR_ADDR);
}

//setup:
//loop:
//if seq_flag:
  //Check RAM for previous hour of move
  //If current hour is new, move.
  //Send 
//check command from HC06

void loop()
{
  //Check for data
  cmd_len = rx_handle();

  //If the length of the command is >0, process the command.
  if (cmd_len)
  {
    //Stop sequence
    if (!strncmp(STOP_CMD,rx_buffer,4))
    {
      Serial.println(F("Stopping sequence."));
      set_ram(SEQ_FLAG_ADDR,0x00);      
      seq_flag = 0;
    }
    //Start the sequence
    if (!strncmp(START_CMD,rx_buffer,5))
    {
      Serial.println(F("Starting sequence."));
      set_ram(SEQ_FLAG_ADDR,0x01);
      seq_flag = 1;
    }
    //Move CCW by n degrees
    if (!strncmp(MOVB_CMD,rx_buffer,4))
    {
      float angle_to_move = atof(&rx_buffer[4]);
      uint32_t steps = (uint32_t) angle_to_move * steps_per_deg;
      Serial.println(F("Moving CCW"));
      move(0,steps);
    }
    //Move CW by n degrees
    if (!strncmp(MOVF_CMD,rx_buffer,4))
    {
      float angle_to_move = atof(&rx_buffer[4]);
      uint32_t steps = (uint32_t) angle_to_move * steps_per_deg;
      Serial.println(F("Moving CW"));
      move(1,steps);
    }
    //Set new minute command
    if (!strncmp(SET_MIN_CMD,rx_buffer,4))
    {
      uint8_t new_min = atoi(&rx_buffer[4]);
      set_min(new_min);
      Serial.print(F("New time: "));
      Serial.print(get_hr());
      Serial.print(":");
      Serial.println(get_min());
    }
    //Set new hour command
    if (!strncmp(SET_HR_CMD,rx_buffer,4))
    {
      uint8_t new_hr = atoi(&rx_buffer[4]);
      set_hr(new_hr);
      Serial.print(F("New time: "));
      Serial.print(get_hr());
      Serial.print(":");
      Serial.println(get_min());
    }
    //Skip to next position
    if (!strncmp(SKIP_CMD,rx_buffer,4))
    {
      Serial.print(F("Skipping..."));
      move(1,hourly_steps);
    }
    Serial.println(F("Done with parsing."));
  }

  //If the seq flag is high and its almost moving time,
  if (seq_flag && get_min() > 57)
  {
    
    //Check the hours
    uint8_t curr_hr = get_hr();
    
    //If the current hour is not the same as the previous hour, do the move and record a new hour.
    if (curr_hr != prev_hr)
    {
      Serial.println(F("Running the hourly move."));
      prev_hr = curr_hr;
      set_ram(PREV_MOVE_HOUR_ADDR,prev_hr);
      move(1,hourly_steps);
      Serial.println(F("Done with hourly move."));
    }
  }
  //Sleep for a little bit 
  delay(100);
}

CODING to switch from bluetooth to automated sourced from this thread: [Bluetooth Module Action when disconnected] (Bluetooth Module Action when Disconnected)

Your RTC is working?

From I can see here the motor will move based in RTC time.

  // If the seq flag is high and its almost moving time,
  if (seq_flag && (get_min() > 57))
  {

    // Check the hours
    uint8_t curr_hr = get_hr();

    // If the current hour is not the same as the previous hour, do the move and record a new hour.
    if (curr_hr != prev_hr)
    {
      Serial.println(F("Running the hourly move."));
      prev_hr = curr_hr;
      set_ram(PREV_MOVE_HOUR_ADDR,prev_hr);
      move(1,hourly_steps);
      Serial.println(F("Done with hourly move."));
    }
  }

Just for test you can try replace the part above by:

 move(1,hourly_steps);
 Serial.println(F("Done with hourly move."));
 delay(10000UL);

Hi FernandoGarcia,

Thank you so much I will try that and revert.

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