Hi, I have two codes that work perfectly fine, but I would like to combine them into one. One of the codes is to send data to my phone through Bluetooth, and the other to control a little car based on the distance detected by some ultrasonic sensors. The problem is that the loop of this code contains some delays which would cause the data to stop being transferred to my phone. Any ideas? Use code tags to format code for the forum. Here are the two codes:
Code one:
#include <Servo.h>
Servo rf;
Servo rb;
Servo lf;
Servo lb;
Servo headx;
Servo headz;
//motor drivers connections
byte m1A1 = 37;
byte m1A2 = 39;
byte m2A1 = 41;
byte m2A2 = 43;
byte m3A1 = 26;
byte m3A2 = 28;
byte m4A1 = 29;
byte m4A2 = 31;
byte m5A1 = 33;
byte m5A2 = 35;
byte m6A1 = 34;
byte m6A2 = 36;
//wheel angle manuver
byte servostep = 4;
byte distancechange = 20;
byte timetocheck = 3000;
//wheels initial
byte inrf = 88;
byte inlf = 92;
byte inrb = 94;
byte inlb = 92;
//wheels right
byte rightrf = 101;
byte rightlf = 105;
byte rightrb = 107;
byte rightlb = 105;
///wheels left
byte leftrf = 75;
byte leftlf = 79;
byte leftrb = 81;
byte leftlb = 79;
//front wheels set value
int leftRF = leftrf;
int leftLF = leftlf;
int rightRF = rightrf;
int rightLF = rightlf;
//back wheels set value
int leftRB = leftrb;
int leftLB = leftlb;
int rightRB = rightrb;
int rightLB = rightlb;
//servo pins
byte rfpin = 4;
byte rbpin = 5;
byte lfpin = 2;
byte lbpin = 3;
byte headxpin= 6;
byte headzpin = 9;
// Define ultrasonic sensor pins
byte echoL = 10;
byte triggerL = 11;
byte echoM = 12;
byte triggerM = 13;
byte echoR = 22;
byte triggerR = 24;
// Define servo objects
int headxcurrent = 93;
byte headxin = 93;
byte headzin = 90;
int headzcurrent = 90;
int headxsteps = 10;
int headzsteps = 10;
// Define distance thresholds
byte minDistance = 30; // Minimum distance in cm
byte maxDistance = 120; // Maximum distance in cm
// Function to calculate distance
int getDistance(int triggerPin, int echoPin) {
// Send trigger pulse
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// Read echo pulse duration
long duration = pulseIn(echoPin, HIGH);
// Calculate distance in cm
int distance = duration * 0.034 / 2;
return distance;
}
void setup() {
//head code start
headx.attach(headxpin);
headz.attach(headzpin);
headx.write(93);
headz.write(90);
//head code end
// Initialize servo objects
rf.attach(rfpin);
rb.attach(rbpin);
lf.attach(lfpin);
lb.attach(lbpin);
// Initialize ultrasonic sensor pins
pinMode(triggerL, OUTPUT);
pinMode(echoL, INPUT);
pinMode(triggerM, OUTPUT);
pinMode(echoM, INPUT);
pinMode(triggerR, OUTPUT);
pinMode(echoR, INPUT);
// Set initial servo positions
lf.write(inlf);
lb.write(inlb);
rf.write(inrf);
rb.write(inrb);
// Wait for servo initialization
delay(500);
// Set motor control pins as outputs
pinMode(m1A1, OUTPUT);
pinMode(m1A2, OUTPUT);
pinMode(m2A1, OUTPUT);
pinMode(m2A2, OUTPUT);
pinMode(m3A1, OUTPUT);
pinMode(m3A2, OUTPUT);
pinMode(m4A1, OUTPUT);
pinMode(m4A2, OUTPUT);
pinMode(m5A1, OUTPUT);
pinMode(m5A2, OUTPUT);
pinMode(m6A1, OUTPUT);
pinMode(m6A2, OUTPUT);
}
void loop() {
// Read distances from ultrasonic sensors
int distanceL = getDistance(triggerL, echoL);
int distanceM = getDistance(triggerM, echoM);
int distanceR = getDistance(triggerR, echoR);
digitalWrite(m1A1, HIGH);
digitalWrite(m1A2, LOW);
digitalWrite(m2A1, LOW);
digitalWrite(m2A2, HIGH);
digitalWrite(m3A1, LOW);
digitalWrite(m3A2, HIGH);
digitalWrite(m4A1, HIGH);
digitalWrite(m4A2, LOW);
digitalWrite(m5A1, HIGH);
digitalWrite(m5A2, LOW);
digitalWrite(m6A1, HIGH);
digitalWrite(m6A2, LOW);
//move right; hit left
if (distanceL >= minDistance && distanceL <= maxDistance) {
rf.write(inrf);
lf.write(inlf);
rb.write(inrb);
lb.write(inlb);
digitalWrite(m1A1, LOW);
digitalWrite(m1A2, LOW);
digitalWrite(m2A1, LOW);
digitalWrite(m2A2, LOW);
digitalWrite(m3A1, LOW);
digitalWrite(m3A2, LOW);
digitalWrite(m4A1, LOW);
digitalWrite(m4A2, LOW);
digitalWrite(m5A1, LOW);
digitalWrite(m5A2, LOW);
digitalWrite(m6A1, LOW);
digitalWrite(m6A2, LOW);
delay(500);
rf.write(rightrf);
lf.write(rightlf);
delay(700);
digitalWrite(m1A1, HIGH);
digitalWrite(m1A2, LOW);
digitalWrite(m2A1, LOW);
digitalWrite(m2A2, HIGH);
digitalWrite(m3A1, LOW);
digitalWrite(m3A2, HIGH);
digitalWrite(m4A1, HIGH);
digitalWrite(m4A2, LOW);
digitalWrite(m5A1, HIGH);
digitalWrite(m5A2, LOW);
digitalWrite(m6A1, HIGH);
digitalWrite(m6A2, LOW);
delay(3000);
rf.write(inrf);
lf.write(inlf);
}
// Move right or left; hit middle
if (distanceM >= minDistance && distanceM <= maxDistance) {
rf.write(inrf);
lf.write(inlf);
rb.write(inrb);
lb.write(inlb);
digitalWrite(m1A1, LOW);
digitalWrite(m1A2, LOW);
digitalWrite(m2A1, LOW);
digitalWrite(m2A2, LOW);
digitalWrite(m3A1, LOW);
digitalWrite(m3A2, LOW);
digitalWrite(m4A1, LOW);
digitalWrite(m4A2, LOW);
digitalWrite(m5A1, LOW);
digitalWrite(m5A2, LOW);
digitalWrite(m6A1, LOW);
digitalWrite(m6A2, LOW);
delay(500);
if(distanceR > distanceL){
rb.write(leftrb);
lb.write(leftlb);
}
else if(distanceR < distanceL){
rb.write(leftrb);
lb.write(leftlb);
}
delay(700);
//backwards
digitalWrite(m1A1, LOW);
digitalWrite(m1A2, HIGH);
digitalWrite(m2A1, HIGH);
digitalWrite(m2A2, LOW);
digitalWrite(m3A1, HIGH);
digitalWrite(m3A2, LOW);
digitalWrite(m4A1, LOW);
digitalWrite(m4A2, HIGH);
digitalWrite(m5A1, LOW);
digitalWrite(m5A2, HIGH);
digitalWrite(m6A1, LOW);
digitalWrite(m6A2, HIGH);
delay(2200);
//stop
rb.write(inrb);
lb.write(inlb);
delay(700);
digitalWrite(m1A1, LOW);
digitalWrite(m1A2, LOW);
digitalWrite(m2A1, LOW);
digitalWrite(m2A2, LOW);
digitalWrite(m3A1, LOW);
digitalWrite(m3A2, LOW);
digitalWrite(m4A1, LOW);
digitalWrite(m4A2, LOW);
digitalWrite(m5A1, LOW);
digitalWrite(m5A2, LOW);
digitalWrite(m6A1, LOW);
digitalWrite(m6A2, LOW);
delay(500);
//check for all distances again
if(distanceR > distanceL){
rb.write(leftrf);
lb.write(leftlf);
}
else if(distanceR < distanceL){
rb.write(rightrf);
lb.write(rightlf);
}
else if (distanceM > distanceL && distanceR){
rf.write(inrf);
lf.write(inlf);
rb.write(inrb);
lb.write(inlb);
}
delay(700);
digitalWrite(m1A1, HIGH);
digitalWrite(m1A2, LOW);
digitalWrite(m2A1, LOW);
digitalWrite(m2A2, HIGH);
digitalWrite(m3A1, LOW);
digitalWrite(m3A2, HIGH);
digitalWrite(m4A1, HIGH);
digitalWrite(m4A2, LOW);
digitalWrite(m5A1, HIGH);
digitalWrite(m5A2, LOW);
digitalWrite(m6A1, HIGH);
digitalWrite(m6A2, LOW);
delay(3000);
rf.write(inrf);
lf.write(inlf);
rb.write(inrb);
lb.write(inlb);
}
// move left; hit rigth
if (distanceR >= minDistance && distanceR <= maxDistance) {
rf.write(inrf);
lf.write(inlf);
rb.write(inrb);
lb.write(inlb);
digitalWrite(m1A1, LOW);
digitalWrite(m1A2, LOW);
digitalWrite(m2A1, LOW);
digitalWrite(m2A2, LOW);
digitalWrite(m3A1, LOW);
digitalWrite(m3A2, LOW);
digitalWrite(m4A1, LOW);
digitalWrite(m4A2, LOW);
digitalWrite(m5A1, LOW);
digitalWrite(m5A2, LOW);
digitalWrite(m6A1, LOW);
digitalWrite(m6A2, LOW);
delay(700);
rf.write(leftrf);
lf.write(leftlf);
delay(2000);
digitalWrite(m1A1, HIGH);
digitalWrite(m1A2, LOW);
digitalWrite(m2A1, LOW);
digitalWrite(m2A2, HIGH);
digitalWrite(m3A1, LOW);
digitalWrite(m3A2, HIGH);
digitalWrite(m4A1, HIGH);
digitalWrite(m4A2, LOW);
digitalWrite(m5A1, HIGH);
digitalWrite(m5A2, LOW);
digitalWrite(m6A1, HIGH);
digitalWrite(m6A2, LOW);
delay(3000);
rf.write(inrf);
lf.write(inlf);
}
// Delay before next iteration
delay(100);
}
Code two:
#include <DHT.h>
#define DHT_PIN_BELLOW 23
#define DHT_PIN_INSIDE 25
#define DHT_PIN_ABOVE 27
#define DHT_TYPE DHT11
DHT dht_bellow(DHT_PIN_BELLOW, DHT_TYPE);
DHT dht_inside(DHT_PIN_INSIDE, DHT_TYPE);
DHT dht_above(DHT_PIN_ABOVE, DHT_TYPE);
const int trigPinLeft = 11;
const int echoPinLeft = 10;
const int trigPinMiddle = 13;
const int echoPinMiddle = 12;
const int trigPinRight = 22;
const int echoPinRight = 24;
// Define the analog input pins for the gas sensors
const int gas1Pin = A0;
const int gas2Pin = A1;
int measureDistance(int trigPin, int echoPin);
void setup() {
// Initialize serial communication with the computer
Serial.begin(9600);
// Initialize Bluetooth serial communication on Serial3 (RX3: pin 15, TX3: pin 14)
Serial3.begin(9600);
dht_bellow.begin();
dht_inside.begin();
dht_above.begin();
// Set the ultrasonic sensor pins as inputs and outputs
pinMode(trigPinLeft, OUTPUT);
pinMode(echoPinLeft, INPUT);
pinMode(trigPinMiddle, OUTPUT);
pinMode(echoPinMiddle, INPUT);
pinMode(trigPinRight, OUTPUT);
pinMode(echoPinRight, INPUT);
}
void loop() {
// Measure distances from ultrasonic sensors
int distanceLeft = measureDistance(trigPinLeft, echoPinLeft);
int distanceMiddle = measureDistance(trigPinMiddle, echoPinMiddle);
int distanceRight = measureDistance(trigPinRight, echoPinRight);
// Read gas sensor values
int gas1Value = analogRead(gas1Pin);
int gas2Value = analogRead(gas2Pin);
// Temperature sensor values
float temperature_bellow = dht_bellow.readTemperature();
float humidity_bellow = dht_bellow.readHumidity();
float temperature_inside = dht_inside.readTemperature();
float humidity_inside = dht_inside.readHumidity();
float temperature_above = dht_above.readTemperature();
float humidity_above = dht_above.readHumidity();
// Send sensor data in CSV format through Bluetooth (Serial3)
Serial3.print(distanceLeft);
Serial3.print(",");
Serial3.print(distanceMiddle);
Serial3.print(",");
Serial3.print(distanceRight);
Serial3.print(",");
Serial3.print(gas1Value);
Serial3.print(",");
Serial3.print(gas2Value);
Serial3.print(",");
Serial3.print(temperature_bellow);
Serial3.print(",");
Serial3.print(humidity_bellow);
Serial3.print(",");
Serial3.print(temperature_inside);
Serial3.print(",");
Serial3.print(humidity_inside);
Serial3.print(",");
Serial3.print(temperature_above);
Serial3.print(",");
Serial3.println(humidity_above);
// Send sensor data to the computer through the USB serial as well (optional)
Serial.print(distanceLeft);
Serial.print(",");
Serial.print(distanceMiddle);
Serial.print(",");
Serial.print(distanceRight);
Serial.print(",");
Serial.print(gas1Value);
Serial.print(",");
Serial.print(gas2Value);
Serial.print(",");
Serial.print(temperature_bellow);
Serial.print(",");
Serial.print(humidity_bellow);
Serial.print(",");
Serial.print(temperature_inside);
Serial.print(",");
Serial.print(humidity_inside);
Serial.print(",");
Serial.print(temperature_above);
Serial.print(",");
Serial.println(humidity_above);
delay(1000); // Delay for 1 second
}
// Function to measure distance using ultrasonic sensor
int measureDistance(int trigPin, int echoPin) {
// Send ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the echo
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance
int distance = duration * 0.034 / 2;
return distance;
}