I'm currently working on a project
It uses a bluetooth model HC-05 to connect to the app
It uses an ultrasonic distance sensor
And will print different numbers at the serial monitor when the sensor detects an object at a certain distance using the serial.println ()
But the problem is the app doesn't function.
Here is the code.
#include <Servo.h> // include the servo library to control servo easier
Servo servo1; // naming the servo so the arduino recognize our servo
int LED1 = 10; // pin 2 for first led
int LED2 = 11; // pin 3 for second led
int LED3 = 12; // pin 4 for third led
int LED4 = 13; // pin 5 for forth led
int trigPin = 6; // trig pin for ultrasound emiter goes to pin 6
int echoPin = 7; // echo pin for ultrasound receiver goes to pin 7
int distance; // creating variables for distance
long duration; // creating variables for large number using long function
int motionSensor = 8; // signal pin of motion sensor goes to pin 8
//int secondState;
void setup() {
// put your setup code here, to run once:
servo1.attach(9); // servo is attach in pin 9
pinMode(motionSensor,INPUT);// setting motion sensor for input to receive data
pinMode(LED1,OUTPUT); // setting led for output to generate voltage
pinMode(LED2,OUTPUT); // setting led for output to generate voltage
pinMode(LED3,OUTPUT); // setting led for output to generate voltage
pinMode(LED4,OUTPUT); // setting led for output to generate voltage
pinMode(trigPin,OUTPUT); // setting trig pin for output to emit ultrasound wave
pinMode(echoPin,INPUT); // setting echo pin for input to receive ultrasound wave
Serial.begin(9600);
//Serial.println("Starting Program");
}
int old_distance;
int new_distance;
void loop() {
///////////ULTRASOUND DISTANCE DETECTOR////////////////
digitalWrite(trigPin,LOW); // giving low voltage to trig pin for 2 microseconds
delay(20);
digitalWrite(trigPin,HIGH); // giving voltage to trig pin to emit ultrasound wave
delay(100);
digitalWrite(trigPin,LOW); // turning off trig pin again
duration = pulseIn(echoPin,HIGH); // read the time taken for echo pin to receive ultrasound wave
distance = duration *0.034/2; // velocity of sound is 0.034cm/µs,hence the distance = velocity*time /2(time needed for signal to bounce back)
//////////LED SHOWING CAPACITY OF DUSTBIN/////////////
new_distance = getDistanceValue ();
if (new_distance != old_distance) {
Serial.println(new_distance);
old_distance = new_distance;
}
if (distance > 20) {
digitalWrite(LED4,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED1,LOW);
//Serial.println(0);
new_distance = 0;
}
else if (distance > 15 && distance <= 20) {
digitalWrite(LED4,HIGH);
digitalWrite(LED3,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED1,LOW);
//Serial.println(25);
new_distance = 25;
}
else if (distance > 10 && distance <= 15) {
digitalWrite(LED4,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED2,LOW);
digitalWrite(LED1,LOW);
//Serial.println(50);
new_distance = 50;
}
else if (distance > 5 && distance <= 10) {
digitalWrite(LED4,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED1,LOW);
//Serial.println(75);
new_distance = 75;
}
else if (distance < 5) {
digitalWrite(LED4,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED1,HIGH);
//Serial.println(100);
new_distance = 100;
}
//////////////////MOTION SENSOR////////////////////
int sensorValue = digitalRead(motionSensor);
if(sensorValue == 1){
servo1.write(180);
if(distance<10){
servo1.write(180);
}
}
else {
servo1.write(0);
}
}
int getDistanceValue () {
if (new_distance == 0 && distance > 20) {
//Serial.println(0);
new_distance = 0;
}
else if (new_distance == 25 && distance > 15 && distance <= 20) {
//Serial.println(25);
new_distance = 25;
}
else if (new_distance == 50 && distance > 10 && distance <= 15) {
//Serial.println(50);
new_distance = 50;
}
else if (new_distance == 75 && distance > 5 && distance <= 10) {
//Serial.println(75);
new_distance = 75;
}
else if (new_distance == 100 && distance <= 5) {
//Serial.println(100);
new_distance = 100;
}
return new_distance;
}
The attached the designer blocks for the MIT App Inventor
In the app
there are a few checkboxes
For example
The checkbox with 25% will be checked if the serial monitor prints 25
But currently it doesn't work
The code should be fine.
The problem is should be with the designer blocks in the MIT App Inventor
Anyone can take a look at it, I can't seem to identify the problem.