usually the sensor reads the right value, but now it reads 0.
#include <NewPing.h>
#include <Bridge.h>
#include <BridgeClient.h>
#include <BridgeServer.h>
#include <BridgeSSLClient.h>
#include <BridgeUdp.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>
#include <Servo.h>
// Servo myservo; // create servo object to control a servo
// int pos = 0; // variable to store the servo position
#include <Keypad.h>
//NewPing sonar(trigger_pin, echo_pin ,[ max_cm_distance]);
NewPing sonar(9, 10 , 200);
long duration;
float distance;
float p = 3.14;
float r = 14.06;
float h = 13.14;
float volume;
const int buzzer = 8;
const byte ROWS = 4;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {11, 12, 7, 6};
byte colPins[COLS] = {5, 4, 3};
String inputString;
long inputInt;
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
pinMode(buzzer, OUTPUT); // declare buzzer as output
inputString.reserve(5); //Input string has 5 digits
//myservo.attach(13);
}
void loop() {
char customKey = customKeypad.waitForKey();
if(customKey)
{
Serial.println(customKey);
if (customKey >= '0' && customKey <= '9') //If we enter a number between 0 and 9
{
inputString = inputString + customKey; // Add new digit
}
else if (customKey == '#')
{
// for (pos = 0; pos <= 10; pos += 1) { // goes from 0 degrees to 10 degrees
// // in steps of 1 degree
// myservo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
// }
if (inputString.length() > 0)
{
inputInt = inputString.toInt(); // YOU GOT AN INTEGER NUMBER
Serial.println(inputInt);
inputString = ""; // clear input
// DO YOUR WORK HERE
while(volume<float(inputInt))
{
duration = sonar.ping();
distance = (duration / 2) * 0.0343;
volume = (h-distance)pr;
Serial.println(distance);
Serial.println(volume);
}
if(volume>float(inputInt))
{
// for (pos = 10; pos >= 0; pos -= 1) { // goes from degrees to 0 degrees
// myservo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
// }
Serial.println("h is ");
Serial.println(distance);
Serial.print("float input ");
Serial.println(float(inputInt));
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(1000); // ...for 1sec
}
}
}
else if (customKey == '*')
{
inputString = ""; // clear input
}
}
}