Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define NUMREADINGS 5 // Number of readings (increase for a slow curve)
- #define MINSOUND 25 // Min sound intensity
- #define MAXSOUND 125 // Max sound intensity
- const int firstLED = 2;
- const int lastLED = 9;
- int leds;
- int readings[NUMREADINGS];
- int index = 0;
- int total = 0;
- int average = 0;
- int sound = 0;
- int x = 0;
- int y = 0;
- void setup() {
- for (int r = 0; r < NUMREADINGS; r++) {
- readings[r] = 0;
- }
- for (int p = firstLED; p <= lastLED; p++) {
- pinMode(p, OUTPUT);
- }
- leds = (lastLED - firstLED) + 1;
- }
- void loop() {
- total -= readings[index];
- readings[index] = analogRead(0);
- total += readings[index];
- index++;
- if (index >= NUMREADINGS) {
- index = 0;
- }
- average = abs((total / NUMREADINGS) - 338); // ((1024 * 300) / 1000) = ~338
- sound = map(average, MINSOUND, MAXSOUND, 0, leds);
- for (int ledON = firstLED; ledON < (firstLED + sound); ledON++) {
- digitalWrite(ledON, HIGH);
- }
- for (int ledOFF = (firstLED + sound); ledOFF <= lastLED; ledOFF++) {
- digitalWrite(ledOFF, LOW);
- }
- digitalWrite(10, HIGH);
- digitalWrite(11, HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement