#include "UsbJoystick.h"
#define PIEZOTHRESHOLD 70
#define PADNUM 4
unsigned long time[PADNUM] = {0,0,0,0};
int tempval[PADNUM] = {0,0,0,0};
unsigned long buttontime[PADNUM] = {0,0,0,0};
void setup() {
UsbJoystick.setup();
}
void loop() {
UsbJoystick.update();
for(int i = 0; i < PADNUM; i++) {
buttondelay(i);
}
UsbJoystick.sendMsg();
}
void buttondelay(int pad){
if ((millis() - buttontime[pad]) > 10) {
int val = analogRead(pad);
if( val >= PIEZOTHRESHOLD ) {
if(tempval[pad]==0){
sendmsg(pad,tempval[pad]);
}
if (val>tempval[pad]){
tempval[pad]=val;
}
}else{
if (tempval[pad]>PIEZOTHRESHOLD){
tempval[pad]=0;
}
}
}else{
UsbJoystick.setButton(pad);
}
}
void sendmsg(int pad,int val){
if ((millis() - time[pad]) > 10) {
UsbJoystick.setButton(pad);
time[pad] = millis();
buttontime[pad] = millis();
}
}
#define PIEZOTHRESHOLD 70
#define PADNUM 4
unsigned long time[PADNUM] = {0,0,0,0};
int tempval[PADNUM] = {0,0,0,0};
unsigned long buttontime[PADNUM] = {0,0,0,0};
void setup() {
UsbJoystick.setup();
}
void loop() {
UsbJoystick.update();
for(int i = 0; i < PADNUM; i++) {
buttondelay(i);
}
UsbJoystick.sendMsg();
}
void buttondelay(int pad){
if ((millis() - buttontime[pad]) > 10) {
int val = analogRead(pad);
if( val >= PIEZOTHRESHOLD ) {
if(tempval[pad]==0){
sendmsg(pad,tempval[pad]);
}
if (val>tempval[pad]){
tempval[pad]=val;
}
}else{
if (tempval[pad]>PIEZOTHRESHOLD){
tempval[pad]=0;
}
}
}else{
UsbJoystick.setButton(pad);
}
}
void sendmsg(int pad,int val){
if ((millis() - time[pad]) > 10) {
UsbJoystick.setButton(pad);
time[pad] = millis();
buttontime[pad] = millis();
}
}
留言
張貼留言