62 lines
1.3 KiB
C++
62 lines
1.3 KiB
C++
/*
|
|
Keyboard Clicker
|
|
*/
|
|
|
|
int outPinsSize = 2;
|
|
int outPins[] = { 13, 12 };
|
|
|
|
int outShift = 0;
|
|
int lastCount = 0;
|
|
|
|
int outBell = 11;
|
|
|
|
#define RELAY_OFF HIGH
|
|
#define RELAY_ON LOW
|
|
|
|
void setup() {
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
for (int i = 0; i < outPinsSize; i++) {
|
|
pinMode(outPins[i], OUTPUT);
|
|
digitalWrite(outPins[i], RELAY_OFF);
|
|
}
|
|
|
|
pinMode(outBell, OUTPUT);
|
|
digitalWrite(outBell, RELAY_OFF);
|
|
|
|
// initialize serial:
|
|
Serial.begin(115200);
|
|
}
|
|
|
|
void loop() {
|
|
}
|
|
|
|
/*
|
|
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
|
|
routine is run between each time loop() runs, so using delay inside loop can
|
|
delay response.
|
|
*/
|
|
void serialEvent() {
|
|
while (Serial.available()) {
|
|
// get the new byte:
|
|
char inChar = (char)Serial.read();
|
|
//Serial.println(inChar);
|
|
if (inChar >= '0' && inChar <= '9') {
|
|
int count = inChar - '0';
|
|
if (count >= lastCount) {
|
|
outShift = (outShift + 1) % outPinsSize;
|
|
}
|
|
lastCount = count;
|
|
for (int i = 0; i < outPinsSize; i++) {
|
|
digitalWrite(outPins[(i + outShift) % outPinsSize], ((count > i) ? RELAY_ON : RELAY_OFF));
|
|
}
|
|
} else {
|
|
if (inChar == '(') {
|
|
digitalWrite(outBell, RELAY_ON);
|
|
delay(20);
|
|
digitalWrite(outBell, RELAY_OFF);
|
|
}
|
|
}
|
|
}
|
|
}
|