Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int sampleWindow = 100; // Sample window width in mS (50 mS = 20Hz)
- unsigned int sample;
- void setup()
- {
- Serial.begin(9600);
- }
- void loop()
- {
- unsigned long startMillis= millis(); // Start of sample window
- unsigned int peakToPeak = 0; // peak-to-peak level
- unsigned int signalMax = 0;
- unsigned int signalMin = 1024;
- // collect data for 50 mS
- while (millis() - startMillis < sampleWindow)
- {
- sample = analogRead(14);
- if (sample < 1024) // toss out spurious readings
- {
- if (sample > signalMax)
- {
- signalMax = sample; // save just the max levels
- }
- else if (sample < signalMin)
- {
- signalMin = sample; // save just the min levels
- }
- }
- }
- peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
- double volts = (peakToPeak * 5.0) / 1024; // convert to volts
- if( volts < 0.02 ) {
- Serial.println("row: 0");
- } else if (volts >= 0.02 && volts < 0.30) {
- Serial.println("row: 1");
- } else if (volts >= 0.30 && volts < 0.60) {
- Serial.println("row: 2");
- } else if (volts >= 0.60 && volts < 0.90) {
- Serial.println("row: 3");
- } else if (volts >= 0.90 && volts < 1.20) {
- Serial.println("row: 4");
- } else if (volts >= 1.20 && volts < 1.50) {
- Serial.println("row: 5");
- } else if (volts >= 1.50 && volts < 1.80) {
- Serial.println("row: 6");
- } else if (volts >= 1.80 && volts < 2.10) {
- Serial.println("row: 7");
- } else if (volts >= 2.10 && volts < 2.40) {
- Serial.println("row: 8");
- } else if (volts >= 2.40 && volts < 2.70) {
- Serial.println("row: 9");
- } else if (volts >= 2.70 && volts < 3.00) {
- Serial.println("row: 10");
- } else if (volts >= 3.00 && volts < 3.30) {
- Serial.println("row: 11");
- } else if (volts >= 3.30 && volts < 3.60) {
- Serial.println("row: 12");
- } else if (volts >= 3.60) {
- Serial.println("row: 13");
- }
- Serial.println(volts);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement