Created
September 10, 2014 11:04
-
-
Save melwil/62ef02f24c9d9c53a457 to your computer and use it in GitHub Desktop.
Arduinoeksperiment hos Iterate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#include <Esplora.h> | |
// Kontrollvariabler | |
int counter = 0; | |
boolean firstNoise = false; | |
boolean hasPause = false; | |
int iterationsSinceSound = 0; | |
// Mikrofon | |
int maxVal = 0; | |
int micVal; | |
// Lyssensor | |
int sensorValue; | |
int sensorLow = 1023; | |
int sensorHigh = 0; | |
const int ledPin = 13; | |
boolean turnedOn = false; | |
void setup() | |
{ | |
pinMode(ledPin, OUTPUT); | |
digitalWrite(ledPin, HIGH); | |
while(millis()<5000){ | |
sensorValue = analogRead(A0); | |
micVal = analogRead(A1); | |
Serial.println(micVal); | |
if(sensorValue > sensorHigh) | |
{ | |
sensorHigh = sensorValue; | |
} | |
if(sensorValue < sensorLow) | |
{ | |
sensorLow = sensorValue; | |
} | |
} | |
digitalWrite(ledPin, LOW);; | |
Serial.begin(9600); | |
Serial.println("ready"); | |
} | |
void loop() | |
{ | |
counter++; | |
sensorValue = analogRead(A0); | |
micVal = analogRead(A1); | |
if (micVal > maxVal) | |
{ | |
maxVal = micVal; | |
} | |
if(counter == 100) | |
{ | |
if (firstNoise) { | |
iterationsSinceSound++; | |
if (iterationsSinceSound > 40) { | |
iterationsSinceSound = 0; | |
firstNoise = false; | |
hasPause = false; | |
} | |
} | |
if(maxVal>60) | |
{ | |
Serial.println(maxVal); | |
if (!firstNoise) { | |
Serial.println("first sound"); | |
firstNoise = true; | |
} | |
else { | |
if (firstNoise && hasPause) { | |
Serial.println("second sound"); | |
toggleState(); | |
firstNoise = false; | |
hasPause = false; | |
} | |
} | |
} | |
else { | |
if (firstNoise && !hasPause) { | |
Serial.println("has pause"); | |
hasPause = true; | |
} | |
} | |
maxVal =0; | |
counter=0; | |
} | |
//Serial.println(micVal); | |
if (turnedOn) | |
{ | |
int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 20000); | |
tone(8, pitch, 20); | |
} | |
//delay(1); | |
} | |
void toggleState() | |
{ | |
turnedOn = !turnedOn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment