Arduino Code not responding to serial read

When I try to send the '@' via the serial monitor or via a c# application which has a button for it, it doesn't do anything. I am using an Arduino Due and have connected the Due to my computer via it's Native USB port and am trying to get it to exacute a while loop by using the following code. The character the program and what I am attempting to send through the serial monitor is '@'.

I have double checked that I am using the same baud and the right port and stuff also.

My code:
int go = 0;
char incomingByte;
char ch;
if(Serial.available())
{
ch = Serial.read();

if(ch=='@')
{
go = 1;
}
else
{
go = 0;
}
}
while (go == 1) {
// blah //blah //blah
}

Thanks for the help!

Disclaimer: no experience with Due.

Can you please post a complete program that exhibits the behaviour; for all I know your code snippet resides in setup() and hence chances are slim it will see a character on the serial port when your application starts.

Also please use code tags; edit your post and
Type
** **[code]** **
before your code
Type
** **[/code]** **
after your code

Next, serial monitor requires you to click the send button; this might be something you don't do. Without saying you're a beginner, it is often a beginners mistake thinking that typing something win serial monitor will result in the character being send.

Lastly, have you tried echoing received characters to the serial monitor?

sterretje:
Disclaimer: no experience with Due.

Can you please post a complete program that exhibits the behaviour; for all I know your code snippet resides in setup() and hence chances are slim it will see a character on the serial port when your application starts.

Also please use code tags; edit your post and
Type
** **[code]** **
before your code
Type
** **[/code]** **
after your code

Next, serial monitor requires you to click the send button; this might be something you don't do. Without saying you're a beginner, it is often a beginners mistake thinking that typing something win serial monitor will result in the character being send.

Lastly, have you tried echoing received characters to the serial monitor?

Yeah sorry I can do that, I will send it in a few hours, My computer has been shut down its 2 am.

Serial is the programming port on Due. The native USB port is SerialUSB.

sterretje:
Disclaimer: no experience with Due.

Can you please post a complete program that exhibits the behaviour; for all I know your code snippet resides in setup() and hence chances are slim it will see a character on the serial port when your application starts.

Also please use code tags; edit your post and
Type
** **[code]** **
before your code
Type
** **[/code]** **
after your code

Next, serial monitor requires you to click the send button; this might be something you don’t do. Without saying you’re a beginner, it is often a beginners mistake thinking that typing something win serial monitor will result in the character being send.

Lastly, have you tried echoing received characters to the serial monitor?

Okay sorry for that delay. I have sent the ‘@’ in the serial monitor right after restarting it and the code works for about 3 seconds then stops until I unplug my Arduino and restart Arduino IDE. My code is suspose to send mouse clicks when the character @ is send and stop while # is sent, I want it to be working more than a few seconds lol.

 #include <HID.h>

#include <Mouse.h>


void setup() {
unsigned long seed=0, count=31;
while (--count)
  seed = (seed<<1) | (analogRead(0)&1);
randomSeed(analogRead(0));
Mouse.begin();
SerialUSB.begin(9600);
while (!SerialUSB.available());
delay(2000);
}

void loop()
{
  int go = 0;
char incomingByte;
char ch;
if(SerialUSB.available())
{
   ch = SerialUSB.read();

  if(ch=='@')
  {
go = 1;
}
  else
  {
   go = 0;
  }
}
  while (go == 1) {
  // send data only when you receive data:
       if(SerialUSB.available())
{
   ch = SerialUSB.read();

  if(ch=='@')
  {
 go = 1;
 }
  else
  {
 go = 0;  
 }
           
Mouse.press();
delay(75);
Mouse.release();
}

oqibidipo:
Serial is the programming port on Due. The native USB port is SerialUSB.

Yeah I already tried switching it, no changes.

unsigned long seed=0, count=31;
while (--count)
  seed = (seed<<1) | (analogRead(0)&1);
randomSeed(analogRead(0));

What is the point to computing seed?

My code is suspose to send mouse clicks when the character @ is send and stop while # is sent

So, why do both blocks of code deal with ‘@’, and set go the same way?

Why did you post code that won’t even compile? Your indenting is atrocious. I tried to fix it, using Tools + Auto Format, but there are not the same number of { and } in your code.

PaulS:

unsigned long seed=0, count=31;

while (–count)
  seed = (seed<<1) | (analogRead(0)&1);
randomSeed(analogRead(0));



What is the point to computing seed?
So, why do both blocks of code deal with '@', and set go the same way?

Why did you post code that won't even compile? Your indenting is atrocious. I tried to fix it, using Tools + Auto Format, but there are not the same number of { and } in your code.

Yeah I think you are right that my code is the problem. It’s weird that it compiles though. So basically it works now but when I send ‘@’ it starts clicking and doesn’t stop, not even when I send the #

David1937:
Yeah I think you are right that my code is the problem. It's weird that it compiles though.

It would be weird if it compiled! As you posted it, it doesn't.

evanmars:
It would be weird if it compiled! As you posted it, it doesn’t.

Yeah it is compiling, well the full version of it. I didn’t want put it in because it’s quite confusing.
Here is the full code, sorry for it being confusing it’s generating random intervals.

#include <HID.h>

#include <Mouse.h>


void setup() {
unsigned long seed=0, count=31;
while (--count)
  seed = (seed<<1) | (analogRead(0)&1);
randomSeed(analogRead(0));
Mouse.begin();
SerialUSB.begin(9600);
while (!SerialUSB.available());
delay(2000);
}

void loop()
{
  int go = 0;
char incomingByte;
char ch;
if(SerialUSB.available())
{
   ch = SerialUSB.read();

  if(ch=='@')
  {
go = 1;
}
  else
  {
   go = 0;
  }
}
  while (go == 1) {
  // send data only when you receive data:
       if(SerialUSB.available())
{
   ch = SerialUSB.read();

  if(ch=='@')
  {
 go = 1;
 }
  else if (ch== '#')
  {
 go = 0;  
 }
           
        
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;

int CPS = random(1, 100);
who = random(1, 20);
if (CPS >= 1 && CPS <= 30)  
{
  Sleep = 70;
 
} 
if (CPS >= 31 && CPS <= 60)  
{
  Sleep = 80;
  
} 
if (CPS >= 61 && CPS <= 74)  
{
  Sleep = 110;

}
if (CPS >=75 && CPS <= 85)
{
  Sleep = 90;
  
}
if (CPS >= 86 && CPS <= 93)  
{
  Sleep = 120;
 
}
if (CPS >=94 && CPS <= 100)

{
  Sleep = 100;
 
}
double ads = (random(1,20));
double last;
double test;
int cont = 1;
if (ads >=1 && ads <= 10)
{
  Sleep = Sleep - (random(1,5));
}
if (ads >=11 && ads <= 20)
{
  Sleep = Sleep + (random(1,5));
}


do {
  test = (random(30,70));
if(abs(last-test) < 10) {
   cont = 0;
}
if(abs(last-test) > 10) {
   cont = 1;
}
} while (cont == 0);
 double testv = (Sleep - test);  
 
  Mouse.press();
  delay(test);
  Mouse.release();

  delay(testv);
 last = test;
  }
delay(random(133,280));
}



}
}
  while (go == 1) {
  // send data only when you receive data:
       if(SerialUSB.available())
{
   ch = SerialUSB.read();

  if(ch=='@')
  {
 go = 1;

Once go is set to 1, what is the purpose of dealing with another ‘@’ and setting go to 1 again?

Why are you using an int, when a boolean, and true or false, is a lot more intuitive?

if (number1 < 15) {
  number2 = random(1,16);
 
} else {
  number2 = random(17,95);
}

So, you want number2 to contain 1 to 15 or 17 to 94. What a strange set of values…

double ads = (random(1,20));

(What’s) (up) (with) (the) (useless) (parentheses) (?)

random() doesn’t return a double…

Using Tools + Auto Format before posting code would certainly be a good idea. So would adding comments to explain the bizarre code.

Some SerialUSB.print() statements might just go a long ways towards explaining the mystery.

What is sending the serial data?