top of page

Arduino 3rd Practice

Work1: LCD Hello World

Work2: Play "Little Stars" with capacitive sensor

Code:

#define DO 262 #define RE 294 #define MI 330 #define FA 349 #define SO 392 #define LA 440 #define XI 494

#include <CapacitiveSensor.h>

CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); CapacitiveSensor cs_4_5 = CapacitiveSensor(4,5); CapacitiveSensor cs_4_10 = CapacitiveSensor(4,10);

void setup() { cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); Serial.begin(9600); }

void loop() { long start = millis(); long total1 = cs_4_2.capacitiveSensor(30); long total2 = cs_4_5.capacitiveSensor(30); long total3 = cs_4_10.capacitiveSensor(30);

Serial.print(millis() - start); Serial.print("\t");

Serial.print(total1); // print sensor output 1 Serial.print("\t"); Serial.print(total2); // print sensor output 2 Serial.print("\t"); Serial.println(total3); // print sensor output 3

delay(10); int a=(total1>500); int b=(total2>500); int c=(total3>500); int d=100*a+10*b+c; if ( d==100){ tone(8, DO); } else if ( d==10){ tone(8, MI); } else if (d==1){ tone(8, SO); } else if (d==110) tone(8, RE); else if (d==11) tone(8, FA); else if (d==101) tone(8, LA); else if (d==111) tone(8, XI); else { noTone(8); } }

References

  • https://playground.arduino.cc/Main/CapacitiveSensor?from=Main.CapSense

  • https://github.com/KonstantinDimitrov/Arduino-Capacitive-Sensor/blob/master/Arduino-Capacitive-Sensor/Arduino-Capacitive-Sensor.ino

bottom of page