Reciveing Serial data i never ask for. is it broken??

Hey guys :slight_smile: first of thx for reading this :slight_smile:

I have an arduino mega but when I try and use serial.print and Serial.Read it first gives me the number I ask for but then al also gives me a val of 10 just after... Can anyone tell me if I’m a newb or is my arduino broken?

the only command i send was a 1, so why did i get 2 results back??

http://img33.imageshack.us/img33/8273/tempto.png

clipcomet :slight_smile:

The serial monitor is probably sending the newline character.
Try selected the equivalent to "no new line" in your language in the dropdown box where it says "Ny linje"

Many thx m8 that helped :slight_smile: so the other Val that turned up have no effect in my scripts?

edit.

now i tryed reading 2 bit and i get this.. why :s where dose the 207 come from.. sry for the troble

clipcomet:
Many thx m8 that helped :slight_smile: so the other Val that turned up have no effect in my scripts?

edit.

now i tryed reading 2 bit and i get this.. why :s where dose the 207 come from.. sry for the troble

ImageShack - Best place for all of your image hosting and image sharing needs

You are reading from the serial port when no data is available and Serial.read() is returning -1. Which is equal to 255 when converted to unsigned. 255 - 48 = 207;
Check if Serial.available() again before reading or atleast put a small delay between the statements.

i don't understand the:

while(Serial.available()==0);

doesn't Serial.available(); return a true value, if there is data?
like this it looks like your checking if there is no data..

I don't think i am wrong, but just in case do tell me :slight_smile:

as i understand it, it will only give me a val if its aktiv aka more then 0, so it do not keep sending me 00000000 all the time...

sry i did not know that it needed a delay from serial.read1 to serial.read2. i have not seen it in any guides :slight_smile: but if that waht it takes i will say many thx :slight_smile: i cant test ikke befor tomorrow, but thx for your time guys :slight_smile:

I don't know if you plan on adding much code, but the while(Serial.available()); will stop the code dead in its tracks, till there is data. like a delay(); only this one is active as long as there is no data received. what guide did you follow?

or the example code from:

hope it helps though :).

Well what I was going for here was a way to type in fx. 1234 in the serial monitor and get the data down into an INT. I only started play arduino 6 weeks ago and have made the 8x8x8 led cube and some hex bots. But what I want for my new one is typing in an 8 digit number (as a command) and have the arduino read and divide it so I can use it as I please. fx. ( if I use 99887766 and I want the first 2 digit to tell that motor to turn on.. the next 2 to how fast the motor will turn. and the last 4 for what pos. I want the motor to turn.) This was what I came up with. But I’m sure you guys that have been at this longer then me have a better way... :slight_smile:

I don’t like the while command, because it makes me stuck there. But if not it will just read a lot off 00000000 and whatever I type will not make any sense. Or is Theres another way
Again i´ll love any help

Ps this I becoming a topic that dose not fit in here sry

Here I the code I have working so fare ( the // is in danich. sry :slight_smile:


// forsøg for med at fange mere ind 1 byte fra serial til martin fra jacob
// vi snakkede om det og nu fandt jeg en måde mens jeg lavede min robot
// stave fejl er tilladt :wink:

byte input[4]; // værdig til at fange 4 bit
byte code[4]; // for at lave den til dicimal tal så den ser rigtig ud på serial monitoren når den sender det tilbage igen

int datablock; // her er de 4 bit samlet i en INT så den kan bruges til og sende en 4 bit kode fx. 9878

void setup() {

Serial.begin(9600); // starter Serial kom

}

void loop() {

while (Serial.available() == 0); // kikker om der er nogle data på vej

input[0] = Serial.read(); //læser 1 byte
delay(5);
input[1] = Serial.read(); //læser 2 byte
delay(5);
input[2] = Serial.read(); //læser 3 byte
delay(5);
input[3] = Serial.read(); //læser 4 byte

for (int i=0; i<4; i++) //conveter til DEC da 1 dec = 49 i binær
{
code = input -48;
}
for (int i=0; i<4; i++) // printer de 4 byte 1 af gangen for at se
{
_ Serial.println (code*);
}
datablock = code[3] * 1 + code[2] * 10 + code[1] * 100 + code[0] * 1000; // her samler den de 4 byte i en INT ved at multi dem med det antal 0 der skal til for at få deres plads
Serial.println(datablock); // printer de 4 byte.. kan lave sriptet til 255 bit og det kan deles op i lige så mange dele som man vil.. du skal bare søger for der er et lille delay mellen værd gang du indlæser en Serial.read
}*_

while (Serial.available() == 0);    // kikker om der er nogle data på vej
input[0] = Serial.read();          //læser 1 byte
delay(5);
input[1] = Serial.read();          //læser 2 byte
delay(5);
input[2] = Serial.read();          //læser 3 byte
delay(5);
input[3] = Serial.read();          //læser 4 byte

If you are expecting 4 digits to be transmitted, then wait until Serial.available() is 4.

so the ==0 is not what Val to look for, but the number of byte to grab??