Hello!
I have built an Arduino tank, that can turn left and right and move backwards and forwards, using a Bluetooth android app called "Bluecontrol".
I am trying to add to the tank code and to its hardware, the capability of playing a sound sample ("Left" or "Right") at a rising or decreasing frequencies, according to the distance of the tank from an obstacle on the left or on the right.
There are 2 problems that i am having at this moment, and which i am trying to solve with no success:
1.When implementing into the code, that is working very well when there are no ultra-sound or sound sample playback functionality in it, the parts that connect the sound sample with the ultra-sound sensors, there seems to be some kind of conflict, between either the sound library or the ultrasound sensor and the servo library or maybe some other problem.
When the ultra-sound sensor has the required distance of an obstacle in front of it and the code kicks in the sound sample, the servos either go crazy or do not respond and go crazy every time when the sound sample is playing or neither the servos nor the ultra-sound and sound playback will not respond at all (please look at remarks in the code below).
I got some error messages at a certain point and was searching the issue accordingly and had an impression that maybe it has to do with 2 libraries trying to use the pin11 timer? (It is an Arduino Uno) but i am not sure at all that this is the problem, since i did what solutions advised and replaced the servo.h library with ServoTimer2.h library - it did not help, but i think i stopped getting the pin11error.
2.The original sections of code, that i took from another project that i found on instructables.com, that i used for one ultra-sound sensor, i tried to double, so i will have 2 ultra-sound sensors, one for the left side obstacles and one for the right side obstacles, but the code still only allows one ultra-sound sensor to respond to obstacles.
Can anyone take a look at the code i have and tell me if might there be any idea, how to solve these problems i am dealing with?
Here is the code:
#include <ServoTimer2.h>
#include <PCM.h> // This is the sound library
#include <Time.h>
#include <TimeAlarms.h>
const unsigned char sample[] PROGMEM = {
126, 127, 128, 128, 128, 128, //Here comes a very long array of many numbers, that describes the sound sample, which i took off, for the sake of
//the readbility of the code.
};
#define trigPin 13 // This first ultra-sound sensor will respond to obstacles and play the sound sample, but then the servos go crazy and only when the sound
// sample plays.
#define echoPin 12// This first ultra-sound sensor will respond to obstacles and play the sound sample, but then the servos go crazy and only when the
// sound sample plays.
#define trigPin1 7 // This second ultra-sound sensor will not even respond to obstacles.
#define echoPin1 4 // This second ultra-sound sensor will not even respond to obstacles.
char val; // variable to receive data from the serial port
ServoTimer2 wheel1;
ServoTimer2 wheel2;
ServoTimer2 camera_servo1;
ServoTimer2 camera_servo2;
char mem=' '; //remember the last menu choice on the BlueControl app
int camera_servo1_mem_servo1=1500; // remember the camera servo1 last angle so they will not exceed their limits
int camera_servo2_mem_servo2=1500; // remember the camera servo1 last angle so they will not exceed their limits
int led = 10; // LED connected to digital pin 13
int led_count = 225; // LED minimum dim value brfore it starts lighting
int light_saver_timer=0; // Count 3.5 minutes with no action to shut light and save battery
void setup() {
Serial.begin(9600); // start serial communication at 9600bps
pinMode(trigPin, OUTPUT); //initiate ultrasound sensor trig pin
pinMode(echoPin, INPUT); //initiate ultrasound sensor echi pin
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(led, OUTPUT); //initiate led pin
pinMode(11,OUTPUT); //initiate speaker pin
digitalWrite(11,LOW); //initiate speaker pin
}
void loop() {
long duration, distance_left, duration1, distance_right;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(4); // Added this line
digitalWrite(trigPin1, LOW); // Added this line
delayMicroseconds(4); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(20); // Added this line
digitalWrite(trigPin1, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(20); // Added this line
digitalWrite(trigPin, LOW);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin, HIGH);
duration1 = pulseIn(echoPin1, HIGH);
distance_left = (duration/2) / 29.1;
distance_right = (duration1/2) / 29.1;
if (millis()-light_saver_timer > 210000){ // Count 3.5 minutes with no action to shut light and save battery
led_count=225;
light_saver_timer=millis();
analogWrite(led,led_count);
}
if( Serial.available() ){ // if data is available to read
val = Serial.read(); // read it and store it in 'val'
}
//Next lines save the last value that was chosen in the BlueControl app menu
if( val == 'a')
{
mem='a';
val=' ';
}
else if( val == 'c')
{
mem='c';
val=' ';
}
else if( val == 'd')
{
mem='d';
val=' ';
}
else if( val == 'e')
{
mem='e';
val=' ';
}
else if( val == 'U' && mem == 'a' )
{
light_saver();
drive_forward_long();
val=' ';
}
else if(val == 'U' && mem == 'c' )
{
light_saver();
drive_forward_short();
val=' ';
}
else if( val == 'D' && mem == 'a' )
{
light_saver();
drive_backward_long();
val=' ';
}
else if( val == 'D' && mem == 'c' )
{
light_saver();
drive_backward_short();
val=' ';
}
else if( val == 'R' )
{
light_saver();
turn_right();
val=' ';
}
else if( val == 'L' )
{
light_saver();
turn_left();
val=' ';
}
else if( val == 'C' )
{
light_saver();
motor_stop();
val=' ';
}
else if( val == 'U' && mem == 'd' )
{
light_saver();
camera_servo_up();
val=' ';
}
else if( val == 'D' && mem == 'd' )
{
light_saver();
camera_servo_down();
val=' ';
}
else if(val == 'U' && mem == 'e' )
{
light_saver();
if ( led_count < 255)
{
led_count=led_count+2;
analogWrite(led, led_count);
}
val=' ';
}
else if(val == 'D' && mem == 'e' )
{
light_saver();
if ( led_count > 225)
{
led_count=led_count-2;
analogWrite(led, led_count);
}
val=' ';
}
else if (distance_left <= 41){ //Next lines play the sound sample in different frequencies, according to disdtance measured by left side ultra-sound sensor
//Actually the tank will not respond at all (nor servos nor sound nor ultra-sound sensors when this below section is in the code:
if (distance_left <=41 && distance_left >=27) Alarm.timerRepeat(8.5,play_back_sample);
else if (distance_left <=27 && distance_left >=20) Alarm.timerRepeat(5.25,play_back_sample);
else if (distance_left <=20 && distance_left >=17) Alarm.timerRepeat(2.44,play_back_sample);
else if (distance_left <=17 && distance_left >=12) Alarm.timerRepeat(1.5,play_back_sample);
else if (distance_left <=12 && distance_left >=8) Alarm.timerRepeat(1.08,play_back_sample);
else if (distance_left <=8 && distance_left >=5) Alarm.timerRepeat(0.74,play_back_sample);
else if (distance_left <=5 && distance_left >=3) Alarm.timerRepeat(0.530,play_back_sample);
else if (distance_left <=4 && distance_left >=2) Alarm.timerRepeat(0.34,play_back_sample);
else if (distance_left <=2 && distance_left >=0) Alarm.timerRepeat(0.18,play_back_sample);
else if (distance_left >41) stopPlayback();
//The sound sapmle will work in relation to distance (and then the servos wikk go crazy, only if i put in this section, but then using delay is for sure a //problem, isn't it? :
play_back_sample();
if (distance_left <=41 && distance_left >=27) delay(8500);
else if (distance_left <=27 && distance_left >=20) delay(5250);
else if (distance_left <=20 && distance_left >=17) delay(2440);
else if (distance_left <=17 && distance_left >=12) delay(1500);
else if (distance_left <=12 && distance_left >=8) delay(1080);
else if (distance_left <=8 && distance_left >=5) delay(740);
else if (distance_left <=5 && distance_left >=3) delay(530);
else if (distance_left <=4 && distance_left >=2) delay(340);
else if (distance_left <=2 && distance_left >=0) delay(180);
}
else if (distance_right <= 41){ //Next lines play the sound sample in different frequencies, according to disdtance measured by right side ultra-sound //sensor
if (distance_right <=41 && distance_right >=27) Alarm.timerRepeat(8.5,play_back_sample);
else if (distance_right <=27 && distance_right >=20) Alarm.timerRepeat(5.25,play_back_sample);
else if (distance_right <=20 && distance_right >=17) Alarm.timerRepeat(2.44,play_back_sample);
else if (distance_right <=17 && distance_right >=12) Alarm.timerRepeat(1.5,play_back_sample);
else if (distance_right <=12 && distance_right >=8) Alarm.timerRepeat(1.08,play_back_sample);
else if (distance_right <=8 && distance_right >=5) Alarm.timerRepeat(0.74,play_back_sample);
else if (distance_right <=5 && distance_right >=3) Alarm.timerRepeat(0.530,play_back_sample);
else if (distance_right <=4 && distance_right >=2) Alarm.timerRepeat(0.34,play_back_sample);
else if (distance_right <=2 && distance_right >=0) Alarm.timerRepeat(0.18,play_back_sample);
else if (distance_right >41) stopPlayback();
}
}
void motor_stop(){
wheel1.attach(6);
wheel2.attach(5);
wheel1.attach(6);
wheel2.attach(5);
wheel1.write(1458); // set servo to mid-point
wheel2.write(1418);
}
void drive_forward_long(){
wheel1.attach(6);
wheel2.attach(5);
wheel1.write(2000);
wheel2.write(1000);
delay(4000);
wheel1.write(1458); // set servo to mid-point
wheel2.write(1418);
}
void drive_forward_short(){
wheel1.attach(6);
wheel2.attach(5);
wheel1.write(2000);
wheel2.write(1000);
delay(800);
wheel1.write(1458); // set servo to mid-point
wheel2.write(1418);
}
void drive_backward_long(){
wheel1.attach(6);
wheel2.attach(5);
wheel1.write(1000);
wheel2.write(2000);
delay(4000);
wheel1.write(1458); // set servo to mid-point
wheel2.write(1418);
}
void drive_backward_short(){
wheel1.attach(6);
wheel2.attach(5);
wheel1.write(1000);
wheel2.write(2000);
delay(800);
wheel1.write(1458); // set servo to mid-point
wheel2.write(1418);
}
void turn_left(){
wheel1.attach(6);
wheel2.attach(5);
wheel1.write(1000);
wheel2.write(1000);
delay(150);
wheel1.write(1458); // set servo to mid-point
wheel2.write(1418);
}
void turn_right(){
wheel1.attach(6);
wheel2.attach(5);
wheel1.write(2000);
wheel2.write(2000);
delay(150);
wheel1.write(1458); // set servo to mid-point
wheel2.write(1418);
}
void camera_servo_up(){
camera_servo1.attach(8);
camera_servo2.attach(9);
if (camera_servo1_mem_servo1>800 && camera_servo2_mem_servo2<2200)
{
camera_servo1_mem_servo1=camera_servo1_mem_servo1+30;
camera_servo2_mem_servo2=camera_servo2_mem_servo2-30;
camera_servo1.write(camera_servo1_mem_servo1);
camera_servo2.write(camera_servo2_mem_servo2);
}
}
void camera_servo_down(){
camera_servo1.attach(8);
camera_servo2.attach(9);
if (camera_servo1_mem_servo1>800 && camera_servo2_mem_servo2<2200)
{
camera_servo1_mem_servo1=camera_servo1_mem_servo1-30;
camera_servo2_mem_servo2=camera_servo2_mem_servo2+30;
camera_servo1.write(camera_servo1_mem_servo1);
camera_servo2.write(camera_servo2_mem_servo2);
}
}
void light_saver() {
light_saver_timer=millis();
}
void play_back_sample(){
startPlayback(sample, sizeof(sample));
}