永发信息网

最近对Arduino很感兴趣,求Arduino电子琴程序讲解(最好详细一些,谢谢!)

答案:1  悬赏:50  手机版
解决时间 2021-02-25 11:32
int ledPin = 12;
int capval1;
int capval2;
int capval3;
int capval4;
int capval5;
int capval6;
int capval7;
int capval8;
void setup()
{
pinMode(ledPin, OUTPUT);

}

void loop ()
{
digitalWrite(ledPin,LOW);
capval1 = readCapacitivePin(3);
capval2 = readCapacitivePin(4);
capval3 = readCapacitivePin(5);
capval4 = readCapacitivePin(6);
capval5 = readCapacitivePin(7);
capval6 = readCapacitivePin(8);
capval7 = readCapacitivePin(9);
capval8 = readCapacitivePin(10);
if (capval1 > 2) {
tone(ledPin,262,10);
}
if (capval2 > 2) {
tone(ledPin,294,10);
}
if (capval3 > 2) {
tone(ledPin,330,10);
}
if (capval4 > 2) {
tone(ledPin,350,10);
}
if (capval5 > 2) {
tone(ledPin,393,10);
}
if (capval6 > 2) {
tone(ledPin,441,10);
}
if (capval7 > 2) {
tone(ledPin,495,10);
}
if (capval8 > 2) {
tone(ledPin,525,10);
}
}

uint8_t readCapacitivePin(int pinToMeasure) {
// Variables used to translate from Arduino to AVR pin naming
volatile uint8_t* port;
volatile uint8_t* ddr;
volatile uint8_t* pin;
// Here we translate the input pin number from
// Arduino pin number to the AVR PORT, PIN, DDR,
// and which bit of those registers we care about.
byte bitmask;
port = portOutputRegister(digitalPinToPort(pinToMeasure));
ddr = portModeRegister(digitalPinToPort(pinToMeasure));
bitmask = digitalPinToBitMask(pinToMeasure);
pin = portInputRegister(digitalPinToPort(pinToMeasure));
// Discharge the pin first by setting it low and output
*port &= ~(bitmask);
*ddr |= bitmask;
delay(1);
// Make the pin an input with the internal pull-up on
*ddr &= ~(bitmask);
*port |= bitmask;

// Now see how long the pin to get pulled up. This manual unrolling of the loop
// decreases the number of hardware cycles between each read of the pin,
// thus increasing sensitivity.
uint8_t cycles = 17;
if (*pin & bitmask) { cycles = 0;}
else if (*pin & bitmask) { cycles = 1;}
else if (*pin & bitmask) { cycles = 2;}
else if (*pin & bitmask) { cycles = 3;}
else if (*pin & bitmask) { cycles = 4;}
else if (*pin & bitmask) { cycles = 5;}
else if (*pin & bitmask) { cycles = 6;}
else if (*pin & bitmask) { cycles = 7;}
else if (*pin & bitmask) { cycles = 8;}
else if (*pin & bitmask) { cycles = 9;}
else if (*pin & bitmask) { cycles = 10;}
else if (*pin & bitmask) { cycles = 11;}
else if (*pin & bitmask) { cycles = 12;}
else if (*pin & bitmask) { cycles = 13;}
else if (*pin & bitmask) { cycles = 14;}
else if (*pin & bitmask) { cycles = 15;}
else if (*pin & bitmask) { cycles = 16;}

// Discharge the pin again by setting it low and output
// It's important to leave the pins low if you want to
// be able to touch more than 1 sensor at a time - if
// the sensor is left pulled high, when you touch
// two sensors, your body will transfer the charge between
// sensors.
*port &= ~(bitmask);
*ddr |= bitmask;

return cycles;
}
最佳答案
比如彩屏显示五颜六色的颜色 颜色参数就可以使用随机数字random(颜色值)。比如
setColor(random(255), random(255), random(255)); //红绿蓝的值都是在0~255范围随机调用
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
金川路在哪里啊,我有事要去这个地方
京润珍珠寇润的产品适合多大年龄,适合哪种嫉
【红灯笼】红灯笼的含义是什么
今天看到一个蓝魅note4G1399元,但我用的是3G
读了南辕北辙,明白了什么道理? 5分
永盛棉百怎么去啊,有知道地址的么
求求求求!!闯红灯,什么样摄像头拍照,圆球
和一个最要好的朋友总是玩游戏的时候吵架!只
惠辑打字复印部地址有知道的么?有点事想过去
绝对值是5的数有几个?
there is a big three over there同义
手游棋牌 怎么样才算违法
浙江义乌飞途旅游有限公司地址在哪,我要去那
小马虎在计算时,把被除数195错看成159,算出
有什么词语代表人看透一切,不在乎一切的
推荐资讯
农历1月是什么星座?
关于甲醛自测盒溶液的颜色
美家宾馆地址有知道的么?有点事想过去
惠普7610突然检测不到打印头
我用发源堂一个月都没见效
如何界定招标人是否具有编制招标文件和组织评
大学留级生的电影,是一个大学生留了很多年的
1820—1939年间广东沿海某乡居民离乡谋生情况
家里有3台电脑,WIN7系统的 怎样设置局域网?
妖夜寻狼系列电影的顺序
求 Delphi 代码意思?
豪威豆业地址在哪,我要去那里办事
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?