#NodeMCU theremin

Dernière modification : 2016/12/23 16:20

HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8
This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
on 10 Nov 2012
Further modified by Peter Takacs on 04/02/04 https://patakacs.wordpress.com/2014/02/04/ultrasonic-sensor-hc-sr04-and-piezo-speakers-measuring-distance-and-playing-a-tone-with-piezo/

int echoPin =  D5;
int trigPin =  D6;
//int LEDPinYellow = 2; // Yellow LED
//int LEDPinGreen = 4; // Green LED
int soundPin = D7; // Piezo

int maximumRange = 200;
int minimumRange = 5;
long duration, distance;

void setup() {
Serial.begin (9600); // Init communications to serial monitor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//pinMode(LEDPinYellow, OUTPUT);
//pinMode(LEDPinGreen, OUTPUT);

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);

digitalWrite(trigPin, HIGH);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){
// Yellow led indicated out of maximumRange. Prints “Out Of Range” to serial if target is outside maximumRange.
Serial.println("Out Of Range");
//digitalWrite(LEDPinYellow, HIGH);
//digitalWrite(LEDPinGreen, LOW);
else {
// When ultasonic sensor picks up a signal _within_ maximumRange, print distance in cm to serial monitor, turn off YellowLed and turn on GreenLED.
Serial.print("Distance = " );
Serial.println(" cm" );
//digitalWrite(LEDPinYellow, LOW);
//digitalWrite(LEDPinGreen, HIGH);
tone(soundPin, 800, 300);
delay(distance); // Distance is the delay in ms between tones, ie Near maxRange -> Long tones, Near minimumRange -> Rapid tones.


// Mandatory delay