Lost, I created a BT car, using HC05 and L293 motor shield on the UNO R3
I used the analog pins A0 to A5 for lights, SR04, HC05 and a passive buzzer.
All works well, but the moment the buzzer has sounded once, motor 1 and 2 stop working.
Everything else keeps working.
I have to press the reset buton on the motor shield, after that, all is back to normal, but the moment I activate the buzzer, the problem is back.
I tried to swap the buzzer with an other analog port, but the problem remains.
I used the passive buzzer, so I could play a little melody, instead of a single beep,
'''
#include <AFMotor.h>
#include "pitches.h"
#include <SoftwareSerial.h>
#include <Servo.h>
#include "SR04.h"
#define TRIG_PIN A5
#define ECHO_PIN A4
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;//afstand tot sr04
int FrontLED=A3;
int i = 50;
SoftwareSerial HC05(A0,A1); // RX, TX
int horn=A2;
//initial motors pin
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
int ledState = LOW;
//unsigned long premillis = 0; om de led te laten blinken
//const long interval = 1000;
Servo myservo;
char command;
void setup()
{
myservo.attach(10);
myservo.write(115);
pinMode(FrontLED, OUTPUT);
//digitalWrite(FrontLED,LOW);
//pinMode(horn, OUTPUT);
//digitalWrite(horn, LOW);
HC05.begin(9600); //Set the baud rate to your Bluetooth module.
Serial.begin(9600);
}
void loop() {
a=sr04.Distance();
//Serial.println(a);
if (a<=20){
//digitalWrite(horn,HIGH);
tone(horn,NOTE_C5, 1000);
delay(1000);
//digitalWrite(horn,LOW);
//digitalWrite(FrontLED,HIGH);
// delay(1000);
// digitalWrite(FrontLED,LOW);
// delay(1000);
//unsigned long numillis = millis();
//if (numillis - premillis >= interval)
// {
// premillis = numillis;
// if (ledState == LOW) {
// ledState = HIGH;
// }
// else
// {ledState = LOW;}
// digitalWrite (FrontLED, ledState);
//}
}
if (HC05.available() > 0) {
command = HC05.read();
Stop(); //initialize with motors stoped
digitalWrite(FrontLED,LOW);
switch (command) {
case 'F':
forward();
break;
case 'B':
back();
break;
case 'L':
left();
break;
case 'R':
right();
break;
case 'V':
toeter();
break;
case '3':
toeter();
break;
case 'K':
links();
break;
case 'P':
rechts();
break;
case 'W':
lichtaan();
Serial.println (command);
break;
case 'w':
lichtuit();
break;
case 'N':
scan();
break;
}
}
}
int lookRight()
{
myservo.write(0);
delay(500);
// int distance = readPing();
delay(100);
myservo.write(115);
// return distance;
}
int lookLeft()
{
myservo.write(180);
delay(500);
// int distance = readPing();
delay(100);
myservo.write(115);
// return distance;
delay(100);
}
void forward()
{
myservo.write(115);
// return distance;
delay(100);
digitalWrite(FrontLED,HIGH);
motor1.setSpeed(255); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(255); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(255); //Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(255); //Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void back()
{
myservo.write(115);
//digitalWrite(FrontLED,HIGH);
motor1.setSpeed(255); //Define maximum velocity
motor1.run(BACKWARD); //rotate the motor anti-clockwise
motor2.setSpeed(255); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor anti-clockwise
motor3.setSpeed(255); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor anti-clockwise
motor4.setSpeed(255); //Define maximum velocity
motor4.run(BACKWARD); //rotate the motor anti-clockwise
}
void left()
{
digitalWrite(FrontLED, HIGH);
myservo.write(170);
motor1.setSpeed(0); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(0); //Define lower velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(255); //Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(255); // Define lower velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void right()
{
digitalWrite(FrontLED, HIGH);
myservo.write(50);
motor1.setSpeed(255); //Define lower velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(255); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(0); //Define lower velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(0); //Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void Stop()
{
myservo.write(115);
digitalWrite(FrontLED,LOW);
motor1.setSpeed(0); //Define minimum velocity
motor1.run(RELEASE); //stop the motor when release the button
motor2.setSpeed(0); //Define minimum velocity
motor2.run(RELEASE); //rotate the motor clockwise
motor3.setSpeed(0); //Define minimum velocity
motor3.run(RELEASE); //stop the motor when release the button
motor4.setSpeed(0); //Define minimum velocity
motor4.run(RELEASE); //stop the motor when release the button
}
void links()
{
myservo.write(70);
}
void rechts()
{
myservo.write(170);
}
void toeter()
{
tone(horn,NOTE_C5, 500);
delay(150);
tone(horn,NOTE_F5, 500);
delay(150);
tone(horn,NOTE_A5, 500);
delay(150);
tone(horn,NOTE_C6, 500);
delay(300);
tone(horn,NOTE_A5, 500);
delay(150);
tone(horn,NOTE_C6, 500);
delay(800);
//digitalWrite(horn,HIGH);
// delay(500);
// digitalWrite(horn,LOW);
}
void lichtaan()
{
//if (ledState == LOW) {
ledState = HIGH;
//}
//else
//{
// ledState = LOW;
//}
digitalWrite (FrontLED, HIGH);
}
void lichtuit()
{
digitalWrite (FrontLED, LOW);
}
void scan()
{
digitalWrite (FrontLED, HIGH);
for (int i = 50; i < 170; i++){
myservo.write(i);
delay(10);
Serial.println(i);}
for (int i = 170; i >49; i--){
myservo.write(i);
delay(10);
Serial.println(i);}
myservo.write(115);
digitalWrite (FrontLED, LOW);
}
'''

