78Views4Replies

Author Options:

Hc-05 and Arduino connection Answered

Hello to everyone.I have make a bleutooth robot which i can control it via android(tablet) and i have a problem with the code.I want from the robot to stop when connection lost or bluetooth disconnected.Now when the connection lost the robot go ahead and falls on the objects.How to add this line on the code?Can you help me please?The STATE pin of HC-05 when is HIGH is connected and when is LOW is disconnected.I put this on my code but it doesn't stop.Thank you for your time

The bluetooth that i use is HC-05

My code:

#include <Servo.h>

Servo SERVO_1; // Initialize Servo1

// Motor Control Variables
int PWM1 = 9;
int ENABLE1 = 8;
int PWM2 = 5;
int ENABLE2 = 7;
int PWM3 = 3;
int ENABLE3 = 4;
int PWM4 = 6;
int ENABLE4 = 12;
int STATE=2;


void setup() {
  SERVO_1.attach(10);
  Serial.begin(9600);
  pinMode(ENABLE1, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(ENABLE2, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(ENABLE3, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(ENABLE4, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(STATE, INPUT);
}

void loop() {

   if(digitalRead(STATE)== HIGH)

  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    int incomingByte = Serial.read();
    // action depending on the instruction
    // as well as sending a confirmation back to the app
    switch (incomingByte) {
      case 'F':
        moveForward();
        Serial.println("Going forward");
        break;
      case 'L' : // Case 'L' is received,
        SERVO_1.write (180); // Στρίψε Αριστερά.
        SERVO_1.attach(10);
        break;
      case 'N':
        turnright();
        Serial.println("Turning right");
        break;
      case 'M':
        turnleft();
        Serial.println("Turning left");
        break;
      case 'O' : // Case 'L' is received,
        SERVO_1.write (0); // Στρίψε Αριστερά.
        SERVO_1.attach(10);
        break;
      case 'B':
        moveBackward();
        Serial.println("Going forward");
        break;
      case 'P':
        SERVO_1.write(90); // Στρίψε Αριστερά.
        SERVO_1.attach(10);
        break;
      case 'S':
        moveNone();
        Serial.println("Stopping");
        break;
      default:
        // if nothing matches, do nothing
        break;
    }
  }
}

void moveForward() {
  // turn the driving motor on to go forwards at set speed
  digitalWrite(ENABLE1, HIGH);
  digitalWrite(ENABLE2, HIGH);
  digitalWrite(ENABLE3, HIGH);
  digitalWrite(ENABLE4, HIGH);
  analogWrite(PWM1, 255);
  analogWrite(PWM2, 255);
  analogWrite(PWM3, 255);
  analogWrite(PWM4, 255);

}

void moveBackward() {
  // turn the driving motor on to go backwards at set speed
  digitalWrite(ENABLE1, LOW);
  digitalWrite(ENABLE2, LOW);
  digitalWrite(ENABLE3, LOW);
  digitalWrite(ENABLE4, LOW);
  analogWrite(PWM1, 255);
  analogWrite(PWM2, 255);
  analogWrite(PWM3, 255);
  analogWrite(PWM4, 255);
}
void turnright() {
  digitalWrite(ENABLE1, HIGH);
  digitalWrite(ENABLE2, HIGH);
  digitalWrite(ENABLE3, LOW);
  digitalWrite(ENABLE4, LOW);
  analogWrite(PWM1, 255);
  analogWrite(PWM2, 255);
  analogWrite(PWM3, 255);
  analogWrite(PWM4, 255);
}

void turnleft() {
  digitalWrite(ENABLE1, LOW);
  digitalWrite(ENABLE2, LOW);
  digitalWrite(ENABLE3, HIGH);
  digitalWrite(ENABLE4, HIGH);
  analogWrite(PWM1, 255);
  analogWrite(PWM2, 255);
  analogWrite(PWM3, 255);
  analogWrite(PWM4, 255);
}
void moveNone() {
  // turn the driving motor off
  digitalWrite(ENABLE1, 0);
  digitalWrite(ENABLE2, 0);
  digitalWrite(ENABLE3, 0);
  digitalWrite(ENABLE4, 0);
  analogWrite(PWM1, 0);
  analogWrite(PWM2, 0);
  analogWrite(PWM3, 0);
  analogWrite(PWM4, 0);
  SERVO_1.detach();
}

Tags:arduino

Discussions

0
None
steveastrouk

2 years ago

You have nothing to force the action OFF when the STATE pin is low, so it carries on doing what it did last.

there should be an ELSE statement saying MOVENONE !

0
None
dimitrakis1992steveastrouk

Answer 2 years ago

thank you sir for your answer.How to put the else statement?can you help me please?i know little about coding.thanks again

0
None
dimitrakis1992dimitrakis1992

Answer 2 years ago

UPDATE

I tried to modify the code with my little experience but with no result.If you can help me i would appreciate it.

0
None
steveastroukdimitrakis1992

Answer 2 years ago

if(digitalRead(STATE)== HIGH)

{ do stuff }

else movenone()

// I would remove the servo.detach inside movenone, because when comms are available again, you don't attach the servo. There is no need.