in

Arduino Multiple Inputs One Pin

define c 3830 // 261 Hz

define d 3400 // 294 Hz

define e 3038 // 329 Hz

define f 2864 // 349 Hz

define g 2550 // 392 Hz

define a 2272 // 440 Hz

define b 2028 // 493 Hz

define C 1912 // 523 Hz

int note[] = { c, d, e, f, g, a, b, C};

void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
}

void loop() {

int val = analogRead(A0);

int pin = getPin(val);

if (pin > -1) {
Serial.println(pin);
tone(3, note[pin], 10); // pin , tone , time
}

delay(10);
}

int getPin(int val) {
// 1023, 1003 , 985, // 17… 180ohms

int pf = 17;

for (int i = 0; i < 8; i++) { if (val > (1003 – (pf * i) – 5)) {
return i;
}
}

return -1;

}

Written by Admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

GIPHY App Key not set. Please check settings

Arduino – Reading Analog Voltage

How to use Arduino Pro Mini