Problem with the Serial Commands

The following code in sketch should do the next steps:
first of all I’m using arduino pro mini.

check if serial available (if>0), read 1 byte only from the serial, if that byte equal to 1 - do the main code (which should work perfectly):

void setup()
{
Serial.begin(9600);
pinMode(2 , INPUT);
}

void loop()
{

byte wave=0;
byte data;
int jumper=1;

if(Serial.available()>0)
{
data=Serial.read();
if(data==1)
{

for(int x=0;x<8;x++)
{

if(digitalRead(2)==HIGH)
wave=wave+jumper;
jumper=jumper*2;
delay(1140);
}
Serial.print(wave);
if(Serial.availableForWrite()>0)
Serial.write(wave);
else
delay(1);

}

}
}

By using c# (if you gusy know or don’t know the codes I will explain anyway) I’m reading and writing to the serial port by those commands:

myport.Read(data,0,1) - data = my byte array and 0 means that I will start stuffing the array from place 0 in it, 1 means that I recive only 1 byte.

myport.Write(send,0,1) - send = byte array and 0 almost the same as the Read method, 1 means it will write only 1 byte.

now to my problem:
by writing with the sketch to the serial the same value(wave = 85; serial.write(wave):wink:
the whole program either in c# and sketch did the job.
but not exactly, it seems like when i write to the serial through c# only once - the arduino program identify it as much more data in a weird way which works that the longer it take me to send new data to the serial - the more data the arduino is sending to teh serial.

in other words it seems like the structure if(data==‘1’) (data=serial.read() when serial.available >0) doesnt fillter the data and it’s like he isn’t even exist even if I don’t send data to the serial - it keep readding from it and it get the same value of “1”.

Thank you for your patient and help :]

To post:

  1. Use CTRL-T in the Arduino IDE to autoformat your code.
  2. Paste the autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

Good Luck!

   data=Serial.read();
   if(data==1)

Do you really expect to receive this ASCII character? or did you mean ‘1’ (with single quotes around it)?

                 delay(1140);

That’s a lot of Arduino time (over a second) to waste. delay(…) is usually a bad thing to do.

              Serial.write(wave);

What is connected to the serial port? You have Serial.print(wave) in one place, and Serial.write(wave) in another. These are NOT the same!

vaj4088:
To post:

  1. Use CTRL-T in the Arduino IDE to autoformat your code.
  2. Paste the autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

Good Luck!

   data=Serial.read();

if(data==1)




Do you really expect to receive this ASCII character? or did you mean '1' (with single quotes around it)?



delay(1140);




That's a lot of Arduino time (over a second) to waste. delay(...) is usually a bad thing to do.



Serial.write(wave);




What is connected to the serial port? You have Serial.print(wave) in one place, and Serial.write(wave) in another. These are NOT the same!

Thanks for your fast reply,
I’m not on my working space atm so I can’t give you the IDE.
and I’m sending the byte [00000001] so it equals 1 not the ascii code of 1 which is 31H

I wrote the serial.print to check the parameters I get after I run the program in the sketch serial and I know it is different than writing to it , that’s why I wrote both: 1 (print) to check the parameter, 2 (write) to actually send the data to the serial so I can make use of my program in c#.

the long delay was meant to create a big time-line so I can make sure if my program works and thank to this option I figured out that there is some weird problem.

I have a wpf button that when I click on it then I write to the serial port through c# the data 1 (not the ascii as I mentioned before) and with this data (which equal to 1) I want the arduino to start sampling through the input pin 2 (which is connected to sync wave) 8 times the pin’s value between each sample of 100HZ/1KHZ and insert them to a byte called “wave” as you can see in the sketch program and then I will serial.write this wave and read through c#.

so again when I inseretd 85 in the wave = 01010101 in binary - the program in c# did well and I got that parameter “wave” to my data array in c# but when I don’t insert the data and I let the input pin 2 to sample , I always get weird and wrong data and it seems like the problem is with the serial.read because when I used delay of 1.14 seconds and I clicked the wpf button in c# , I Had to wait 8 seconds in the first click (which means I send 1 serial byte to tell the arduino to start sampling and then every 1.14 sec he sample the voltage of the sync wave) but on the second click I had to wait like 1 micro second or something that fast idk… not a long time anyway.
on the thrid click if I was waiting like 1 sec after I clicked the first time , I also had to wait like 1 micro second.

some weird thing is that:
first clcik on the wpf butotn I always had to wait 8 seconds but after that i.e if I wait 0.5 and than i spam the button like 4 times - so 3 ff them will happen in no time but the 4 click in that spam will make me wait 4 seconds or 8 seconds (long time but not always 8 , sometimes 8 sometimes 20,4 etc…).
If I would wait like 20 seconds and than I will spam that button , I would get to click it like 20 times and all of them will happen in 1 micro second instead 8 seconds.

that’s why I figured out that the problem is with the condition structure if(serial.xxx) or somewhere in the sketch program for sure

Thanks for the explanations.

Unfortunately, I don't know where to go from here. Normally I would suggest inserting Serial.println("..."); statements throughout the code to debug, but this usually doesn't work if the serial port is being used for something else. Perhaps you can use the LED on pin 13 to provide some insight.

Is the byte coming in on the stream a numeric 1, or is it an ASCII '1'? ASCII '1' is a numeric 49.

I think that Nitayp1 answered that question when Nitayp1 wrote

and I'm sending the byte [00000001] so it equals 1 not the ascii code of 1 which is 31H

Note that 31H == 49.

PaulMurrayCbr:
Is the byte coming in on the stream a numeric 1, or is it an ASCII '1'? ASCII '1' is a numeric 49.

It's 1.
anyway I prefert to make it ascii 1 but I'm not sure if the code in c# which is:
string num="1";
myport.Write(num);

I'm not sure if that string will send 49 or not so I'm using a byte array [1] and I send the byte 00000001

I'll try the ascii code again but I't still doesn't explain to me why that weird problem happens

but I't still doesn't explain to me why that weird problem happens

And you STILL haven't posted all of your Arduino code or any of your C# code. So the only thing weird is is you expecting us to guess what is wrong with your code.

Have a look at Serial Input Basics

...R

I think you misunderstand the purpose of availableForWrite(). It tells you that there is space in the TX buffer.

      Serial.print(wave);
      if (Serial.availableForWrite() > 0)
      {
        Serial.write(wave);
      }
      else
        delay(1);

The 'print' places a few characters in the buffer (1, 2 or 3 digits, the value of wave). Next you check if there is still space in the buffer. Yes there is as the buffer is 63 bytes big. And therefore you will always add one byte to it.

PS: It's also possible that I don't understand what you want to achieve.

sterretje:
I think you misunderstand the purpose of availableForWrite(). It tells you that there is space in the TX buffer.

      Serial.print(wave);

if (Serial.availableForWrite() > 0)
      {
        Serial.write(wave);
      }
      else
        delay(1);



The 'print' places a few characters in the buffer (1, 2 or 3 digits, the value of wave). Next you check if there is still space in the buffer. Yes there is as the buffer is 63 bytes big. And therefore you will always add one byte to it.

PS: It's also possible that I don't understand what you want to achieve.

What I want to achieve is that when I'm sending the String "1" (from=>String X="1" in c#) then I want to enter a loop that checks the value of pin 2 every 1 sec, doing it 8 times (I connect it to a sync voltage source).
Now it's importnat that you understand that I used the byte wave as an array of bits - every bit in the byte represent HIGH or LOW as 1 for HIGH, 0 for LOW.
When pin No.2 on HIGH then one of the bits will change to 1, when on LOW none of the bits changes cause I set up all the bits in the byte "wave" to 0 when I boot.
The "jumper" used to tell us which bit will be changed in the byte "wave" for expample:
when x=0 then the jumper=1 - this represent bit No.1 in the byte "wave"
when x=2 then jumper=4 - this represent bit No.3 in the byte "wave"
when x=7 then jumper=128 - thsi represent bit No.8 in the byte "wave"

With "jumper" I set up the byte "wave" that should be equal to a sync wave of 1HZ which doesn't seems to work very well when I connecting it to a sync wave.

Nitayp1:
What I want to achieve is that when I'm sending the String "1" (from=>String X="1" in c#) then I want to enter a loop that checks the value of pin 2 every 1 sec, doing it 8 times

This seems all mixed up to me. Perhaps you meant to say "when I receive the character '1' from a C# program on my PC I want to enter a loop ..."?

But if that is true I can't see why you are concerned with availableForWrite() ?

If you really are trying to send the character '1' {A} I don't understand why you mention C# and {B} I don't understand how you expect anything to happen when I'm sending the String "1" because that happens almost instantly and is over and done with.

Please have another attempt at a better description of what you want to happen.

...R

I'm curious to know what we have to do to get people to read the forum "sticky" and put their code into code tags?

Robin2:
This seems all mixed up to me. Perhaps you meant to say "when I receive the character '1' from a C# program on my PC I want to enter a loop ..."?

But if that is true I can't see why you are concerned with availableForWrite() ?

If you really are trying to send the character '1' {A} I don't understand why you mention C# and {B} I don't understand how you expect anything to happen when I'm sending the String "1" because that happens almost instantly and is over and done with.

Please have another attempt at a better description of what you want to happen.

...R

Well it's very simple,
I will describe again what I want from the sketch program:

With c# I'm sending the string "1" (which probably equals to 49 in decimal), teh c# program si fine and working well, the data I'm sending to teh serial port arrive safely.
{A} : I mentioned c# because the program in there gotta reaad from the serial port the "wave" from sketch, but before she read it there gotta be some value in "wave" because I don't want to read random values.
So what I did was to send with c# the string "1" and by that the sketch understand to start a loop.
that loop, what is it doing?
As I said: It sample 8 times pin No.2 on teh arduino which is an INPUT pinMode, and I sample the pin every 1 second.
After 8 seconds The pin will be sampled 8 times, when he was on HIGH or LOW - that data will be stored in the byte "wave" and after that the sketch will send the data through teh serial and then the c# will read it and do what I want to do with this data.

{B}: I don't want anything to happen when I'm sending the string "1", I want something to happen after I will send that string, then the nsketch will read it, then something happens..

what so difficult to understand?
I read your topic with the arduin beginners like 5 time sand it didn't helped me at all about my problems btw so please don't recomend it again in my topic.

So again - there is a problem in my sketch code because what actually happens?
on the first time that I smaple the arduino's pin 2 , I have to wait 8 seconds before he will send me the data.
Thta is fine, but when I tell him to do it again, it seems like he doesn't entering the loop, it looks like he already send another data to the serial port with the first sending.
I have to mention that I got rid form teh Serial.print(wave) code in my program so It's not the problem in here.

anotehr example:
I have a button in WPF (I guess syou don't know what is WPF - it is a window that open in my PC by using visual studio)
That button - I click on it, he send string "1", the sketch read that string and start a loop, after 8 seconds the sketch will send to teh serial port some byte that equal to the sampling from pin 2.
Then the pc will read the serial port using c# and do stuff, teh writing code, the reading code, and teh stuff my c# si doing - all of them happen after 1 buton click.
so again - I click the button and send the string "1" (so hard to understand?) the c# waiting 8 seconds until there is a data in the serial port (why is it waiting 8 seconds? not because i told it that for sure).

myport.Write(SendAnalyze);
i = 0;
logi = 128;
level = 0;
XP1 = 0;
XP2 = 40;
YP1 = 190 + newWave * counterNew;
YP2 = 190 + newWave * counterNew;
myport.Read(data, 0, 1);
after 8 seconds I can click teh button again, during the 1st 8 seconds I can't click it because the program is activate.
on 2nd click for some reasohn I don't have to wait 8 seconds!!!!!!!
the last sentence tells you what is teh problem, I don't know why that thing happening but duo to that I know that if I haven't waited 8 seconds it means that I didn't smapled the pin 2 well.
and again I'm sampling it with sketch.

When are you going to get around to posting your Arduino code correctly?

When are you going to get around to posting the C# code?

what so difficult to understand?

When you get all defensive, and fail to explain exactly what the code on the Arduino is doing, and exactly what the code on the PC is doing, and how those two things differ from what you want, AND you fail to address any of the issues identified in the code you improperly posted, everything.

Nitayp1:
With c# I’m sending the string “1” (which probably equals to 49 in decimal), teh c# program si fine and working well, the data I’m sending to teh serial port arrive safely.
{A} : I mentioned c# because the program in there gotta reaad from the serial port the “wave” from sketch, but before she read it there gotta be some value in “wave” because I don’t want to read random values.
So what I did was to send with c# the string “1” and by that the sketch understand to start a loop.
that loop, what is it doing?
As I said: It sample 8 times pin No.2 on teh arduino which is an INPUT pinMode, and I sample the pin every 1 second.
After 8 seconds The pin will be sampled 8 times, when he was on HIGH or LOW - that data will be stored in the byte “wave” and after that the sketch will send the data through teh serial and then the c# will read it and do what I want to do with this data.

{B}: I don’t want anything to happen when I’m sending the string “1”, I want something to happen after I will send that string, then the nsketch will read it, then something happens…

what so difficult to understand?

I think I understand it now.

You want your Arduino to listen for a ‘1’ (and please use single quotes for characters) and when it detects that it should call a function that uses digitalRead() to detect the value of Pin2 once per second 8 times storing the readings as successive bits in a single byte.

That seems to account for this part of the program from your Original Post

if(Serial.available()>0)
{
   data=Serial.read();
   if(data==1)
   {
     
           for(int x=0;x<8;x++)
             {
 
                 if(digitalRead(2)==HIGH)
                         wave=wave+jumper;
                 jumper=jumper*2;
                 delay(1140);
             }
           Serial.print(wave);

but it should probably be if data == '1'

However I don’t understand the reason for the next piece

  if(Serial.availableForWrite()>0)
               Serial.write(wave);
              else
                 delay(1);

Also, if you want a 1 second interval why are you using delay(1140)?

In any case you would be better to use millis() to manage timing as illustrated in Several Things at a Time. That way your timing will automatically allow for the time taken by the rest of the code.

…R

teh c# program si fine and working well, the data I'm sending to teh serial port arrive safely.

Slow down a bit. Post in English.