inoProjects/Theremin/Theremin.ino

46 lines
1.0 KiB
Arduino
Raw Normal View History

2019-04-29 03:14:05 -04:00
/*
*@author James McKenzie
*@lang C89 Based
*@contact lunarised@outlook.com
*/
int mode;
void setup() {
pinMode(0, INPUT_PULLUP);
pinMode (13, OUTPUT);
pinMode(8, OUTPUT);
2019-04-29 04:05:49 -04:00
Serial.begin (9600); //set up a serial connection with the computer
pinMode(11, OUTPUT); //the trigger pin
pinMode(12, INPUT); //echo pin
2019-04-29 03:14:05 -04:00
if (digitalRead(0) ==HIGH){
mode = 0;
mode = 1;
}
}
void loop() {
if (mode == 0){
digitalWrite(13, HIGH);
2019-04-29 04:05:49 -04:00
tone(8, getNote());
}
}
float getNote()
{
float echoTime;
float calculatedDistance;
digitalWrite(11, HIGH);
delayMicroseconds(20);
digitalWrite(11, LOW);
echoTime = pulseIn(12, HIGH);
calculatedDistance = echoTime / 8;
calculatedDistance += 110;
2019-04-29 04:07:41 -04:00
if (calculatedDistance > 1760){ //
2019-04-29 04:05:49 -04:00
2019-04-29 04:07:41 -04:00
calculatedDistance = 1760;
2019-04-29 03:14:05 -04:00
}
2019-04-29 04:05:49 -04:00
Serial.print(calculatedDistance); //print units after the distance
Serial.println(" hz"); //print units after the distance
return calculatedDistance;
2019-04-29 03:14:05 -04:00
}