Hi everyone,
Despite I am quite new to the Arduino world, I'm trying to build a first "drone".
Anyways, I'm having a lot of problems:
I control the arduino with bluetooth, with an HC-06 sensor. It connects properly and works fine with some commands, but when I press some command like "up", "down", "X", the command is not working and the BT immediatly disconnects. (I can immediatly reconnect).
So, I'm thinking the issues are in some part of my code related to those commands, but I can't find out any:
const float note_C0 = 16.35; //C0
// here i have a lot of others const float notes that I will not bother you with
int cont=0;
#define speakerPin = 3; // speaker connected to digital pin
#define motor1Pin = 7; // H-bridge leg 1 (pin 2 of H bridge)
#define motor2Pin = 8; // H-bridge leg 2 (pin 7of H bridge)
#define enablePin = 5; // H-bridge enable pin 1
#define enablePin2 = 6; // H-bridge enable pin 9
#define motor21Pin = 9; // H-bridge leg 1 (pin 15 of H bridge
#define motor22Pin = 10; // H-bridge leg 2 (pin 10 of bridge)
#define led=12;//red terminal of rgb led
#define led2=13;//blue terminal of rgb led
int incomingByte; // a variable to read incoming serial data into
#include <Servo.h>
Servo myservo;
int pos = 1500;
void setup() {
Serial.begin(9600); // open serial port to receive data
myservo.attach(11);
pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(motor21Pin, OUTPUT);
pinMode(motor22Pin, OUTPUT);
pinMode(enablePin2, OUTPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(enablePin, LOW);
digitalWrite(enablePin2, LOW);
r2D2(); //starting sound
delay(500);
}
void loop() {
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
//up
if (incomingByte == '1') {
digitalWrite(led, LOW);//change color of led
digitalWrite(led2, HIGH);
analogWrite(enablePin, 200); // turn on the motor 1
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
analogWrite(enablePin2, 200); // turn on second motor
digitalWrite(motor21Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor22Pin, HIGH); // set leg 2 of the H-bridge high
delay(10);
digitalWrite(enablePin, LOW); // turn off motor 1
digitalWrite(enablePin2, LOW); // turn off motor 2
cont=0;
}
//down
else if (incomingByte == '2') {
digitalWrite(led, LOW); //change color of led
digitalWrite(led2, HIGH);
analogWrite(enablePin, 200); // turn on the motor 1
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
analogWrite(enablePin2, 200); // turn on second motor
digitalWrite(motor21Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor22Pin, LOW); // set leg 2 of the H-bridge low
delay(10);
digitalWrite(enablePin, LOW); // turn off motor 1
digitalWrite(enablePin2, LOW); // turn off motor 2
cont=0;
}
//left
else if (incomingByte == '3') {
digitalWrite(led2, LOW);
digitalWrite(led, HIGH);
digitalWrite(enablePin2, HIGH); // turn on second motor
digitalWrite(motor21Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor22Pin, HIGH); // set leg 2 of the H-bridge high
delay(10);
digitalWrite(enablePin2, LOW); // turn off motor 2
cont=0;
}
//right
else if (incomingByte == '4') {
digitalWrite(led2, LOW);
digitalWrite(led, HIGH);
digitalWrite(enablePin, HIGH); // turn on motor 1
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
delay(10);
digitalWrite(enablePin, LOW); // turn off motor 1
cont=0;
}
//triangle
else if ((incomingByte == '9')&&(cont==0)) {
cont++;
ohhh();
delay(500);
}
//x
else if ((incomingByte == '7')&&(cont==0)) {
cont++;
catcall();
delay(500);
}
else if (incomingByte == '8') {
pos=pos+35;
if(pos<2000){myservo.writeMicroseconds(pos);}
else{pos=2000;}
delay(10);
cont=0;
}
else if (incomingByte == 'A') {
cont=0;
pos=pos-35;
if(pos>1000){myservo.writeMicroseconds(pos);}
else{pos=1000;}
delay(10);
}
}
}
void beep (int speakerPin, float noteFrequency, long noteDuration)
{
int x;
// Convert the frequency to microseconds
float microsecondsPerWave = 1000000/noteFrequency;
// Calculate how many HIGH/LOW cycles there are per millisecond
float millisecondsPerCycle = 1000/(microsecondsPerWave * 2);
// Multiply noteDuration * number or cycles per millisecond
float loopTime = noteDuration * millisecondsPerCycle;
// Play the note for the calculated loopTime.
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(microsecondsPerWave);
digitalWrite(speakerPin,LOW);
delayMicroseconds(microsecondsPerWave);
}
}
void r2D2(){
beep(speakerPin, note_A7,100); //A
beep(speakerPin, note_G7,100); //G
beep(speakerPin, note_E7,100); //E
beep(speakerPin, note_C7,100); //C
beep(speakerPin, note_D7,100); //D
beep(speakerPin, note_B7,100); //B
beep(speakerPin, note_F7,100); //F
beep(speakerPin, note_C8,100); //C
beep(speakerPin, note_A7,100); //A
beep(speakerPin, note_G7,100); //G
beep(speakerPin, note_E7,100); //E
beep(speakerPin, note_C7,100); //C
beep(speakerPin, note_D7,100); //D
beep(speakerPin, note_B7,100); //B
beep(speakerPin, note_F7,100); //F
beep(speakerPin, note_C8,100); //C
}
void catcall() {
for (int i=1000; i<5000; i=i*1.05) {
beep(speakerPin,i,10);
}
delay(300);
for (int i=1000; i<3000; i=i*1.03) {
beep(speakerPin,i,10);
}
for (int i=3000; i>1000; i=i*.97) {
beep(speakerPin,i,10);
}
}
void ohhh() {
for (int i=1000; i<2000; i=i*1.02) {
beep(speakerPin,i,10);
}
for (int i=2000; i>1000; i=i*.98) {
beep(speakerPin,i,10);
}
}
void uhoh() {
for (int i=1000; i<1244; i=i*1.01) {
beep(speakerPin,i,30);
}
delay(200);
for (int i=1244; i>1108; i=i*.99) {
beep(speakerPin,i,30);
}
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
Furthermore, about the DC motors commands, only the "left" command looks like it's working, but, if I hold down the "movement arrow" on the bluetooth controller (a random mobile app), the motors just move a little bit when i release it, while, obviously, I need it to keep going the whole time I'm pressing it.
Hope someone can help me out, thank you.