Hi,
I'm working on a personal project just to learn my way around Arduino, but I've recently hit a roadblock trying to connect the distance measuring HC-SR04 with my bluetooth connector (JY-MCU).
I've successfully created projects using them individually (using the HC-SR04 to measure distance, and using the JY-MCU bluetooth connector to control an LED), and now am trying to combine the projects.
The goal is to be able to turn the device on and off via bluetooth, and have the device display the distance of an object. In addition, the closer the object an RGB LED will change colours.
However, currently my code and setup simply results in the HC-SR04 returning 0, and I'm not entirely sure why. I have a little bit of schooling background in C, but no experience in regards to electronics and circuitry, so I believe my wiring is the reason why I'm having issues.
Thanks for any help or insight!
Here's the code I'm using:
const int redPin = 7; //red led
const int greenPin = 6; // green led
const int bluePin = 5; // blue led
const int numReadings = 10; // # to average
const int trigPin = 11; // Trigger
const int echoPin = 12; // Echo
long duration, cm;
int readings[numReadings]; // array to store numbers from analog input
int readIndex = 0; // index of current reading
int total = 0; // running total of values (to calc average)
int average = 0; // result
char blueToothVal;
char lastValue;
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
for (int i = 0; i < numReadings; i++){ //initialize array
readings[i] = 0;
}
}
void loop() {
if(Serial.available()){
blueToothVal = Serial.read();
}
if(blueToothVal == 'n'){
//turn on ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2) / 29.1; // convert duration to centimeters
Serial.println(duration); //DEBUG
//*************calc average box******************
total -= readings[readIndex]; // subtracting the last reading
readings[readIndex] = cm; // reading input from potentiometer
total += readings[readIndex]; // add new reading
readIndex++; // increment counter
if(readIndex >= numReadings){ // when counter exceeds array, reset
readIndex = 0;
}
average = total / numReadings;
//**********************************************
if(average <= 10){
//red led (255 0 0)
setColour(255,0, 0);
}else if(average <= 20){
//yellow led (255 255 0)
setColour(255, 255, 0);
}else if(average <= 30){
//green led (0 255 0)
setColour(0, 255, 0);
}else{
Serial.println(average);
}
lastValue = blueToothVal;
} else if(blueToothVal == 'f'){
//turn off
digitalWrite(trigPin, LOW);
setColour(0,0,0);
lastValue = blueToothVal;
}
delay(300);
}
void setColour(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
And here's my wiring setup.