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??
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"
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.
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 but if that waht it takes i will say many thx i cant test ikke befor tomorrow, but thx for your time guys
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?
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...
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
// 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
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
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.