#include <CapSense.h>
#define SND_PIN 12 // ,
CapSense key1 = CapSense(7, 5); // 1 7- 5-
CapSense key2 = CapSense(7, 4); // 2 7- 4-
CapSense key3 = CapSense(7, 3); // 3 7- 3-
int range = 200; // , ,
byte button; //
int freq; //
void setup()
{
// 1- .
// ,
key1.set_CS_AutocaL_Millis(0xFFFFFFFF);
}
void loop()
{
long total1 = key1.capSense(30);
long total2 = key2.capSense(30);
long total3 = key3.capSense(30);
button = 0;
if (total1 > range) button |= 1;
if (total2 > range) button |= 2;
if (total3 > range) button |= 4;
switch (button) {
case 1: freq = 200; break ;
case 2: freq = 300; break ;
case 3: freq = 400; break ;
case 4: freq = 500; break ;
case 5: freq = 600; break ;
case 6: freq = 700; break ;
case 7: freq = 800; break ;
default : freq = 0;
}
freq? tone(SND_PIN, freq) : noTone(SND_PIN); //
}
* This source code was highlighted with Source Code Highlighter .
#include <CapSense.h>
#define SND_PIN 12 // ,
CapSense key1 = CapSense(7, 5); // 1 7- 5-
CapSense key2 = CapSense(7, 4); // 2 7- 4-
CapSense key3 = CapSense(7, 3); // 3 7- 3-
int range = 200; // , ,
byte button; //
int freq; //
void setup()
{
// 1- .
// ,
key1.set_CS_AutocaL_Millis(0xFFFFFFFF);
}
void loop()
{
long total1 = key1.capSense(30);
long total2 = key2.capSense(30);
long total3 = key3.capSense(30);
button = 0;
if (total1 > range) button |= 1;
if (total2 > range) button |= 2;
if (total3 > range) button |= 4;
switch (button) {
case 1: freq = 200; break ;
case 2: freq = 300; break ;
case 3: freq = 400; break ;
case 4: freq = 500; break ;
case 5: freq = 600; break ;
case 6: freq = 700; break ;
case 7: freq = 800; break ;
default : freq = 0;
}
freq? tone(SND_PIN, freq) : noTone(SND_PIN); //
}
* This source code was highlighted with Source Code Highlighter .
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using WindowsInput; using System.IO; using System.IO.Ports; namespace WindowsFormsApplication1 { public partial class Form1 : Form { System.IO.Ports.SerialPort newPort; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { newPort = new SerialPort("COM11", 57600, Parity.None, 8, StopBits.One); newPort.Open(); newPort.DataReceived += new SerialDataReceivedEventHandler(GetArduinoBytes); } public void GetArduinoBytes(object sender, SerialDataReceivedEventArgs e) { string data = newPort.ReadExisting(); string [] arr; int key = 0; arr = data.Split('\r'); try { key = Convert.ToInt32(arr[0]); } catch (System.FormatException) { // } int key1 = key & 1; int key2 = (key >> 1) & 1; int key3 = (key >> 2) & 1; int key4 = (key >> 3) & 1; int key5 = (key >> 4) & 1; int key6 = (key >> 5) & 1; int key7 = (key >> 6) & 1; int key8 = (key >> 7) & 1; if (key1 == 1) WindowsInput.InputSimulator.SimulateKeyDown(VirtualKeyCode.DOWN); else WindowsInput.InputSimulator.SimulateKeyUp(VirtualKeyCode.DOWN); if (key2 == 1) WindowsInput.InputSimulator.SimulateKeyDown(VirtualKeyCode.UP); else WindowsInput.InputSimulator.SimulateKeyUp(VirtualKeyCode.UP); if (key3 == 1) WindowsInput.InputSimulator.SimulateKeyDown(VirtualKeyCode.LEFT); else WindowsInput.InputSimulator.SimulateKeyUp(VirtualKeyCode.LEFT); if (key4 == 1) WindowsInput.InputSimulator.SimulateKeyDown(VirtualKeyCode.RIGHT); else WindowsInput.InputSimulator.SimulateKeyUp(VirtualKeyCode.RIGHT); if (key5 == 1) WindowsInput.InputSimulator.SimulateKeyDown(VirtualKeyCode.SPACE); else WindowsInput.InputSimulator.SimulateKeyUp(VirtualKeyCode.SPACE); if (key6 == 1) WindowsInput.InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_Z); else WindowsInput.InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_Z); } } }
#include <CapSense.h> #define SND_PIN 12 // , CapSense key1 = CapSense(5, 2); // 1 5- 5- CapSense key2 = CapSense(5, 3); // 2 5- 4- CapSense key3 = CapSense(5, 4); // 3 5- 3- CapSense key4 = CapSense(5, 6); // 4 5- 3- CapSense key5 = CapSense(5, 7); // 5 5- 3- CapSense key6 = CapSense(5, 8); // 6 5- 3- CapSense key7 = CapSense(5, 9); // 7 5- 3- CapSense key8 = CapSense(5, 11); // 8 5- 3- int range = 100; // , unsigned int button = 0, oldButton = 0; // int freq; // byte sense = 30; long total1, total2, total3, total4, total5, total6, total7, total8; void setup() { //key1.set_CS_AutocaL_Millis(0xFFFFFFFF); Serial.begin(57600); // } void loop() { total1 = key1.capSense(sense); total2 = key2.capSense(sense); total3 = key3.capSense(sense); total4 = key4.capSense(sense); total5 = key5.capSense(sense); total6 = key6.capSense(sense); total7 = key7.capSense(sense); total8 = key8.capSense(sense); button = 0; if (total1 > range) button |= 1; if (total2 > range) button |= 2; if (total3 > range) button |= 4; if (total4 > range) button |= 8; if (total5 > range) button |= 16; if (total6 > range) button |= 32; if (total7 > range) button |= 64; if (total8 > range) button |= 128; if (button != oldButton) { Serial.println(button); oldButton = button; } /* if (button) freq = button * 10 + 200; else freq = 0; if (freq) { Serial.println(freq); tone(SND_PIN, freq); } else noTone(SND_PIN); // */ }
Source: https://habr.com/ru/post/143928/