I made a post a few days ago about my Arduino Due not responding to code, I have fixed the problem and it works great now with a little brosing on the forums.
Basically all my code does is send '@' when the right button is held down and sends '#' when it is released which is working perfectly fine, it is sending and recieving very fast. The problem that I have however is my mouse, and some of my other mice, glitch out if you press them just a little bit too hard. I mean that if you were to put a certain amount of pressure on the button and really fast, it would cause the mouse to double click the button. I guess it depends on the button.
When this happens it glitches out and starts sending the keystrokes/clicks to the computer even though it isn't held down, because of this double click thing.
So my question is how could I implement something that once the button is released it doesn't receive data for say example 50ms later?
Here is my code, it's pretty complex but it's because I suck at coding:
void loop()
{
int go = 0;
char incomingByte;
char ch;
if(SerialUSB.available())
{
ch = SerialUSB.read();
while (ch=='@') {
if(SerialUSB.available())
{
ch = SerialUSB.read();
}
double number1 = random(1,100);
double number2;
if (number1 < 15) {
number2 = random(1,16);
} else {
number2 = random(17,95);
}
for (int j = 0; j < number2; j++) {
int i = 0;
int who;
int Sleep = 30;
int Delay;
double asd = 90;
double dsa = 30;
int CPS = random(1, 100);
who = random(1, 20);
if (CPS >= 1 && CPS <= 30)
{
asd = 50;
dsa = 20;
Sleep = 70;
}
if (CPS >= 31 && CPS <= 60)
{
Sleep = 80;
asd = 50;
dsa = 20;
}
if (CPS >= 61 && CPS <= 74)
{
Sleep = 110;
asd = 80;
}
if (CPS >=75 && CPS <= 85)
{
Sleep = 90;
asd = 60;
}
if (CPS >= 86 && CPS <= 93)
{
Sleep = 120;
asd = 90;
}
if (CPS >=94 && CPS <= 100)
{
Sleep = 100;
asd = 70;
}
double ads = (random(1,20));
double last;
double test;
int cont = 1;
do {
test = (random(dsa,asd));
if(abs(last-test) < 10) {
cont = 0;
}
if(abs(last-test) > 10) {
cont = 1;
}
} while (cont == 0);
if (ads >=1 && ads <= 10)
{
Sleep = Sleep - (random(1,5));
}
if (ads >=11 && ads <= 20)
{
Sleep = Sleep + (random(1,5));
}
double testv = (Sleep - test);
Mouse.press();
delay(test);
Mouse.release();
delay(testv);
last = test;
if(SerialUSB.available())
{
ch = SerialUSB.read();
if(ch=='#')
{
break;
} else {
SerialUSB.write('@');
}
}
}
delay(random(133,280));
}
}
}
Thanks in advance.
It sounds like you need to read up on debouncing.
David1937:
The problem that I have however is my mouse, and some of my other mice, glitch out if you press them just a little bit too hard. I mean that if you were to put a certain amount of pressure on the button and really fast, it would cause the mouse to double click the button. I guess it depends on the button.
The spurious doubleclicks are very often caused by dirt or fibers on the mouse switch.
You could try to (carefully) clean it/them, that worked a couple of times with my mice.
Whandall:
The spurious doubleclicks are very often caused by dirt or fibers on the mouse switch.
You could try to (carefully) clean it/them, that worked a couple of times with my mice.
I don't think that it is dirt or dust because even when I get new mouses it happens, also a problem if your finger has a tiny spasm and your finger is on the button, it causes it to press it a few times in less than 1/10th of a second.
You don't have to believe me. 
UKHeliBob:
It sounds like you need to read up on debouncing.
Yeah that sounds like it would work. I'm new to this and reading up on it I understand it if it were to be used with a button, but how would I use it with recieving Serial Data.
As in something like this: https://www.arduino.cc/en/Tutorial/Debounce
char ch;
if(SerialUSB.available())
{
ch = SerialUSB.read();
while (ch=='@') {
if(SerialUSB.available())
{
ch = SerialUSB.read();
}
I tried using debounce but I don't think that that is the problem. Whenever I spam send '@' it is recieving more
'@' then '#' causing it to get stuck turned on.
Your code above seems to be missing some parentheses. I take it that you meant:
char ch;
if(SerialUSB.available())
{
ch = SerialUSB.read();
while (ch=='@')
{
if(SerialUSB.available())
{
ch = SerialUSB.read();
}
}
}
To your question
So my question is how could I implement something that once the button is released it doesn't receive data for say example 50ms later?
you could use delay(50);
but the buffer would be filling up and not clearing.
Would this do what you want?
unsigned long StartIme = millis();
while ((millis() - startTime ) < 50)
{
if(SerialUSB.available())
{
ch = SerialUSB.read();
}
}
;
lemming:
Your code above seems to be missing some parentheses. I take it that you meant:
char ch;
if(SerialUSB.available())
{
ch = SerialUSB.read();
while (ch=='@')
{
if(SerialUSB.available())
{
ch = SerialUSB.read();
}
}
}
To your question
you could use `delay(50);`
but the buffer would be filling up and not clearing.
Would this do what you want?
unsigned long StartIme = millis();
while ((millis() - startTime ) < 50)
{
if(SerialUSB.available())
{
ch = SerialUSB.read();
}
}
;
Oh sorry there are missing brackets because there are commands that sent mouse clicks and keystrokes while the '@' is sent. I have a c# code that sends '@' when the right click button is clicked and sends '#' when released. The while loop isn't closed because it is what contains the mouse/keystroke sends.
Will that still work with what you gave me?
I mean that if you were to put a certain amount of pressure on the button and really fast, it would cause the mouse to double click the button
It still sounds to me as though you need to deal with the problem by not allowing mouse clicks to be responded to if they occur to often, but you need to do this in your C# sharp code, not on the Arduino.