Hi everyone,
I have been struggling for a while now with my little project and I am simply lost on why it is not working as I expect it to work. I am fairly new to Arduino and generally doing hardware stuff, so forgive me for any mistakes or oversights.
The project in general is pretty simple: I have an ultrasonic sensor to measure distance, two 28byj-48 step motors and a HC-05 Bluetooth module. I want to control via Bluetooth from another device. On command the distance sensor should measure the distance and if its greater than I want it to be the step motors should drive until the desired distance is reached. Obviously I am having some gears etc. ready for this.
For whatever reason though sending and receiving data does not work, with the ultrasonic module and the Bluetooth module plugged in at the same time. Because then using the serial monitor with the correct port I can't even turn on the LED (which I do by sending 1), nor can I start the measurement process at all. If I do it via the USB connection it works as intended. I can also send and receive data with the Bluetooth module if I don't use the ultrasonic sensor. As it works via Bluetooth if I omit the measurement and leave the ultrasonic sensor unplugged.
Any help or points would be appreciated.
I have a schematic attached, though for demonstration purposes the step motors are not plugged in correctly. The lower line with the motors and the ultrasonic device are powered with 5v the upper line with the HC-05 is powered with 3.3v I am using an HW-131 for this purpose.
The code reads as follows:
int delaytime = 2;
int trigPin = A1; // Trigger
int echoPin = A2; // Echo
long duration, cm;
char blueToothVal;
char lastValue;
// ports used to control the stepper motor
// if your motor rotate to the opposite direction,
// change the order as {4, 5, 6, 7};
int port[4] = {4, 5, 6, 7};
int portTwo[4] = {8,9,10,11};
// sequence of stepper motor control
int seq[8][4] = {
{ LOW, HIGH, HIGH, LOW},
{ LOW, LOW, HIGH, LOW},
{ LOW, LOW, HIGH, HIGH},
{ LOW, LOW, LOW, HIGH},
{ HIGH, LOW, LOW, HIGH},
{ HIGH, LOW, LOW, LOW},
{ HIGH, HIGH, LOW, LOW},
{ LOW, HIGH, LOW, LOW}
};
void rotate(int step) {
static int phase = 0;
int i, j;
int delta = (step > 0) ? 1 : 7;
step = (step > 0) ? step : -step;
for(j = 0; j < step; j++) {
phase = (phase + delta) % 8;
for(i = 0; i < 4; i++) {
digitalWrite(port[i], seq[phase][i]);
digitalWrite(portTwo[i], seq[phase][i]);
}
delay(delaytime);
}
// power cut
for(i = 0; i < 4; i++) {
digitalWrite(port[i], LOW);
digitalWrite(portTwo[i], LOW);
}
}
void MoveDeviceToDistanceSmaller(float d){
do{
Serial.println("MovingSmaller");
delay(250);
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
Serial.println(duration);
// Convert the time into a distance
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
Serial.println(cm);
if(cm>d){
rotate(100);
}
delay(250);
}while(cm > d);
}
void MoveDeviceToDistanceLarger(float d){
do{
Serial.println("MovingLarger");
delay(250);
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
if(cm<d){
rotate(100);
}
delay(250);
}while(cm < d);
}
void setup() {
pinMode(port[0], OUTPUT);
pinMode(port[1], OUTPUT);
pinMode(port[2], OUTPUT);
pinMode(port[3], OUTPUT);
pinMode(portTwo[0], OUTPUT);
pinMode(portTwo[1], OUTPUT);
pinMode(portTwo[2], OUTPUT);
pinMode(portTwo[3], OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop() {
if(Serial.available()) //if data received
{
blueToothVal=Serial.read();//read data
}
if (blueToothVal=='1') //receive 1
{
digitalWrite(13,HIGH); //turn on led
if (lastValue!='1') //if last value was not 1
Serial.println("On"); //send back that led is turned on now
lastValue=blueToothVal;
}
else if (blueToothVal=='0') //receive 0
{
digitalWrite(13,LOW); //turn off led
if (lastValue!='0') //if last value was not 0
Serial.println("Off"); //send back that led is turned off now
lastValue=blueToothVal;
}else if(blueToothVal=='3'){ //receive 3
MoveDeviceToDistanceSmaller(15.0); //move device until distance is 15cm
}
}