I am making an rc car controlled with Bluetronics app (using HC-06 bluetooth module). After i wrote the code for the sensor to read the values the bluetooth commands wont work. I tried different codes for the sensor but nothing. After i remove everything about the sensor from the code it will work fine. Here is the code:
#include "pitches.h"
#define NOTE_G5 784 //notele folosite pentru buzzer
#define NOTE_GS5 831
#define NOTE_E6 1319
#define NOTE_G6 1568
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_D7 2349
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_G7 3136
#define NOTE_A7 3520
int melody[] = {
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, REST,
NOTE_A4, NOTE_G4, NOTE_A4, REST,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, REST,
NOTE_A4, NOTE_G4, NOTE_A4, REST,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST,
NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, REST,
NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, REST,
NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, REST,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST,
NOTE_D5, NOTE_E5, NOTE_A4, REST,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, REST,
NOTE_C5, NOTE_A4, NOTE_B4, REST,
NOTE_A4, NOTE_A4,
//Repeat of first part
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, REST,
NOTE_A4, NOTE_G4, NOTE_A4, REST,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, REST,
NOTE_A4, NOTE_G4, NOTE_A4, REST,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST,
NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, REST,
NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, REST,
NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, REST,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST,
NOTE_D5, NOTE_E5, NOTE_A4, REST,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, REST,
NOTE_C5, NOTE_A4, NOTE_B4, REST,
//End of Repeat
NOTE_E5, REST, REST, NOTE_F5, REST, REST,
NOTE_E5, NOTE_E5, REST, NOTE_G5, REST, NOTE_E5, NOTE_D5, REST, REST,
NOTE_D5, REST, REST, NOTE_C5, REST, REST,
NOTE_B4, NOTE_C5, REST, NOTE_B4, REST, NOTE_A4,
NOTE_E5, REST, REST, NOTE_F5, REST, REST,
NOTE_E5, NOTE_E5, REST, NOTE_G5, REST, NOTE_E5, NOTE_D5, REST, REST,
NOTE_D5, REST, REST, NOTE_C5, REST, REST,
NOTE_B4, NOTE_C5, REST, NOTE_B4, REST, NOTE_A4
};
int durations[] = {
8, 8, 4, 8, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8, 8,
8, 8, 8, 4, 8,
8, 8, 4, 8, 8,
4, 8, 4, 8,
8, 8, 4, 8, 8,
8, 8, 4, 4,
4, 8,
//Repeat of First Part
8, 8, 4, 8, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8, 8,
8, 8, 4, 8, 8,
8, 8, 8, 4, 8,
8, 8, 4, 8, 8,
4, 8, 4, 8,
8, 8, 4, 8, 8,
8, 8, 4, 4,
//End of Repeat
4, 8, 4, 4, 8, 4,
8, 8, 8, 8, 8, 8, 8, 8, 4,
4, 8, 4, 4, 8, 4,
8, 8, 8, 8, 8, 2,
4, 8, 4, 4, 8, 4,
8, 8, 8, 8, 8, 8, 8, 8, 4,
4, 8, 4, 4, 8, 4,
8, 8, 8, 8, 8, 2
};
//#define melodyPin A0 //Pin Buzzer
const int melodyPin = 12;
//motoare spate
#define ENA1 A5
#define ENB1 A4
int motor1pin1 = 0;
int motor1pin2 = 1;
int motor2pin1 = 2;
int motor2pin2 = 3;
//motoare fata
#define ENA2 A2
#define ENB2 A3
int motor3pin1 = 7;
int motor3pin2 = 6;
int motor4pin1 = 5;
int motor4pin2 = 4;
char val;
#include <HCSR04.h>
HCSR04 hc(A0, A1);
void setup() {
pinMode(melodyPin, OUTPUT);
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(ENA1, OUTPUT);
pinMode(ENB1, OUTPUT);
pinMode(motor3pin1, OUTPUT);
pinMode(motor3pin2, OUTPUT);
pinMode(motor4pin1, OUTPUT);
pinMode(motor4pin2, OUTPUT);
pinMode(ENA2, OUTPUT);
pinMode(ENB2, OUTPUT);
Serial.begin(9600);
delay(50);
}
void loop() {
while (Serial.available() > 0)
val = Serial.read();
switch (val) {
case 'B':
inainte(150);
break;
case 'T':
inapoi(150);
break;
case 'L':
stanga(150);
break;
case 'R':
dreapta(150);
break;
case 'S':
stop();
break;
case 'G':
int size = sizeof(durations) / sizeof(int);
for (int note = 0; note < size; note++) {
//to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int duration = 1000 / durations[note];
tone(melodyPin, melody[note], duration);
//to distinguish the notes, set a minimum time between them.
//the note's duration + 30% seems to work well:
int pauseBetweenNotes = duration * 1.30;
delay(pauseBetweenNotes);
//stop the tone playing:
noTone(melodyPin);
}
break;
default:
break;
}
Serial.println(hc.dist()); // return curent distance in serial
delay(60);
}
void inapoi(unsigned char speed_val) // speed_val:0~255
{
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
analogWrite(ENA1, speed_val);
analogWrite(ENB1, speed_val);
digitalWrite(motor3pin1, LOW);
digitalWrite(motor3pin2, HIGH);
digitalWrite(motor4pin1, LOW);
digitalWrite(motor4pin2, HIGH);
analogWrite(ENA2, speed_val);
analogWrite(ENB2, speed_val);
}
void inainte(unsigned char speed_val) // speed_val:0~255
{
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
analogWrite(ENA1, speed_val);
analogWrite(ENB1, speed_val);
digitalWrite(motor3pin1, HIGH);
digitalWrite(motor3pin2, LOW);
digitalWrite(motor4pin1, HIGH);
digitalWrite(motor4pin2, LOW);
analogWrite(ENA2, speed_val);
analogWrite(ENB2, speed_val);
}
void dreapta(unsigned char speed_val) {
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
analogWrite(ENA1, speed_val);
analogWrite(ENB1, speed_val);
digitalWrite(motor3pin1, HIGH);
digitalWrite(motor3pin2, LOW);
digitalWrite(motor4pin1, LOW);
digitalWrite(motor4pin2, HIGH);
analogWrite(ENA2, speed_val);
analogWrite(ENB2, speed_val);
}
void stanga(unsigned char speed_val) {
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
analogWrite(ENA1, speed_val);
analogWrite(ENB1, speed_val);
digitalWrite(motor3pin1, LOW);
digitalWrite(motor3pin2, HIGH);
digitalWrite(motor4pin1, HIGH);
digitalWrite(motor4pin2, LOW);
analogWrite(ENA2, speed_val);
analogWrite(ENB2, speed_val);
}
void stop() {
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
analogWrite(ENA1, 0);
analogWrite(ENB1, 0);
digitalWrite(motor3pin1, LOW);
digitalWrite(motor3pin2, LOW);
digitalWrite(motor4pin1, LOW);
digitalWrite(motor4pin2, LOW);
analogWrite(ENA2, 0);
analogWrite(ENB2, 0);
}