1.连接电容引脚
12.留出数据线
23.蜂鸣器接到A4
34.用2B铅笔画线
45.接线
56.导入库文件
导入7.程序
编程2
3
4
5
6
7
程序:
#include <CapacitiveSensor.h>
#define speaker 11
CapacitiveSensor s1 = CapacitiveSensor(2,3);
CapacitiveSensor s2 = CapacitiveSensor(2,4);
CapacitiveSensor s3 = CapacitiveSensor(2,5);
CapacitiveSensor s4 = CapacitiveSensor(2,6);
CapacitiveSensor s5 = CapacitiveSensor(2,7);
CapacitiveSensor s6 = CapacitiveSensor(2,8);
CapacitiveSensor s7 = CapacitiveSensor(2,9);
CapacitiveSensor s8 = CapacitiveSensor(2,10);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
long sen1 = s1.capacitiveSensorRaw(300);
long sen2 = s2.capacitiveSensorRaw(300);
long sen3 = s3.capacitiveSensorRaw(300);
long sen4 = s4.capacitiveSensorRaw(300);
long sen5 = s5.capacitiveSensorRaw(300);
long sen6 = s6.capacitiveSensorRaw(300);
long sen7 = s7.capacitiveSensorRaw(300);
long sen8 = s8.capacitiveSensorRaw(300);
Serial.print(sen1);
Serial.print("\t");
Serial.print(sen2);
Serial.print("\t");
Serial.print(sen3);
Serial.print("\t");
Serial.print(sen4);
Serial.print("\t");
Serial.print(sen5);
Serial.print("\t");
Serial.print(sen6);
Serial.print("\t");
Serial.print(sen7);
Serial.print("\t");
Serial.print(sen8);
Serial.println("\t");
if (sen1> 1000) tone(speaker,131,100); // frequency
if (sen2 > 1000) tone(speaker,147,100); // you can see https://www.arduino.cc/en/Tutorial/toneMelody if you want to change frequency
if (sen3 > 1000) tone(speaker,165,100);
if (sen4 > 1000) tone(speaker,175,100);
if (sen5 > 1000) tone(speaker,196,100);
if (sen6 > 1000) tone(speaker,220,100);
if (sen7 > 1000) tone(speaker,247,100);
if (sen8 > 1000) tone(speaker,262,100);
if (sen1<=1000 & sen2<=1000 & sen3<=1000 & sen4<=1000 & sen5<=1000 & sen6<=1000 & sen7<=1000 & sen8<=1000)
noTone(speaker);
delay(10);
}
参考图二:
2
#include <CapacitiveSensor.h>
#define speaker A0
CapacitiveSensor s1 = CapacitiveSensor(2,3);
CapacitiveSensor s2 = CapacitiveSensor(4,5);
CapacitiveSensor s3 = CapacitiveSensor(6,7);
CapacitiveSensor s4 = CapacitiveSensor(8,9);
CapacitiveSensor s5 = CapacitiveSensor(10,11);
CapacitiveSensor s6 = CapacitiveSensor(12,13);
CapacitiveSensor s7 = CapacitiveSensor(A1,A2);
CapacitiveSensor s8 = CapacitiveSensor(A3,A4);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
long sen1 = s1.capacitiveSensorRaw(300);
long sen2 = s2.capacitiveSensorRaw(300);
long sen3 = s3.capacitiveSensorRaw(300);
long sen4 = s4.capacitiveSensorRaw(300);
long sen5 = s5.capacitiveSensorRaw(300);
long sen6 = s6.capacitiveSensorRaw(300);
long sen7 = s7.capacitiveSensorRaw(300);
long sen8 = s8.capacitiveSensorRaw(300);
Serial.print(sen1);
Serial.print("\t");
Serial.print(sen2);
Serial.print("\t");
Serial.print(sen3);
Serial.print("\t");
Serial.print(sen4);
Serial.print("\t");
Serial.print(sen5);
Serial.print("\t");
Serial.print(sen6);
Serial.println("\t");
if (sen1> 1000) tone(speaker,131,100); // frequency
if (sen2 > 1000) tone(speaker,147,100); // you can see https://www.arduino.cc/en/Tutorial/toneMelody if you want to change frequency
if (sen3 > 1000) tone(speaker,165,100);
if (sen4 > 1000) tone(speaker,175,100);
if (sen5 > 1000) tone(speaker,196,100);
if (sen6 > 1000) tone(speaker,220,100);
if (sen7 > 1000) tone(speaker,247,100);
if (sen8 > 1000) tone(speaker,262,100);
// if (sen1<=1000 & sen2<=1000 & sen3<=1000 & sen4<=1000 & sen5<=1000 & sen6<=1000 & sen7<=1000 )
// noTone(speaker);
// delay(10);
}
自制
网友评论