#how to not read data from serial, for real

1 messages · Page 1 of 1 (latest)

plucky basin
#

my issue happen in the main.cpp

stuck igloo
#

what the heck lol

#

this seems so complex

#

what's the problem?

plucky basin
#

can't understand the bit order from the serial output of the arduino

stuck igloo
#

???

#

describe more

plucky basin
#

alr

#

let me go take that piece the code and some illustrative video

#
        arduino.readSerialPort(output, MAX_DATA_LENGTH);

        //cout<<">> "<<output<<endl;
        /*
        for (int i = 0; i < MAX_DATA_LENGTH; i++)
        {
            printf("%c", output[i]);
        }*/

        /*
        printf("%c \n", output[0]); 
        printf("%c \n", output[1]);    //bit giusti
        printf("%c \n", output[2]);*/
        
        
        tripl[0] = (output[0] - 48) * 100;
        tripl[1] = (output[1] - 48)* 10;
        tripl[2] = output[2] - 48;
        x = tripl[0] + tripl[1] + tripl[2];

        printf("%d \n", x);

I'm reading some bits from the serial port through the random library I found on the internet
the char output has some limit size that is 255, As I understood, 255 should be bytes
now every half second I'm getting the first 3 bytes from the serial output of the arduino, but during the runtime the numbers keep changing and I have no idea how to solve that

#

https://www.youtube.com/watch?v=tpEo5AOSSkg
I found this video that also explain some of the single bits order, but I'm not sure how this gonna actually affect the char variables

Want to learn more? Check out our courses!
https://bit.ly/3B8YW3y

We designed this circuit board for beginners!
Kit-On-A-Shield: https://amzn.to/3lfWClU

Get your Free Trial of Altium PCB design Software
https://www.altium.com/yt/programmingelectronicsacademy

SHOP OUR FAVORITE STUFF! (affiliate links)
-----------------------------------...

â–¶ Play video
#

then

#

I'm also gonna show my arduino code and some of the console output

#

ok, on the start everything runs perfect, then it start getting some negative random values, then it go back to normal

#

I'm also trying to change it, but the problem remain

#
// SerialMouse sketch
//#define potYPin 5
int vale = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int x = analogRead(A0);
  vale = map(x, 0, 1023, 0, 255);
  //int y = analogRead(potYPin);
  Serial.print(vale,DEC);
  //Serial.print(",");
  delay(50);
  //Serial.print(y,DEC);*/
  Serial.println();  // send a cr/lf
  delay(50); // send position 20 times a second
}
stuck igloo
#

so explain what your PC side code is doing

#

@plucky basin

plucky basin
# stuck igloo so explain what your PC side code is doing

It check if connection is estabilished then it start read data from the serial.print from the arduino and saving it in pc side char array named output, I don't understand how to read function works since it comes from the library and the library doesn't have any docs, anyway, after storing the bytes in the char array, i'm converting char to int by subtracting 48 that it should convert from ascii to normal int, then I'm trying to get the full value of the potentiometer and saving it in a variable x, all this for just move the mouse cursor

stuck igloo
#

you are not reading bits btw, you are reading bytes

#

can you explain more how you convert the chars to int?

#

you're making an assumption in your code I think

plucky basin
#

HmmmmStare a number in ascii table have a certain id, kinda 0 from ascii table if directly assigned to an int should be 48, 1 should be 49, 2 = 50 and so on

#

By sub -48 from every char variable i'm getting the real num

plucky basin
stuck igloo
#

why do you think that?

plucky basin
stuck igloo
#

don't guess unless you have some evidence

plucky basin
stuck igloo
#

how about this: could your PC code fail for some numbers?

plucky basin
stuck igloo
#

Parse the input 10

plucky basin
stuck igloo
#

Imagine the Arduino sends the input 10 to the computer

plucky basin
#

Ok

stuck igloo
#

how does your code parse that input?

plucky basin
#

The code starts by reading byte by byte, start with 1 and multiply it by ten times, so it would be 10, then the 0 will remain the same,
In the end the code will just 10+0 = 10
x= 10

stuck igloo
#

how

#

nvm

#

parse 100

plucky basin
#

Ok, 🤨

Uhm...
Then, 100 will be converted in the ascii table when it goes out from the arduino
The code process every single byte starting by left
1 (converted from ascii would be 49)

The code store that in the array named output

1 = output[0] \first byte from left

(Some temporary variable for store the value) = (output[0]-48) * 100

0=output [1] \ the second byte took out from the array

(Some temporary variable for store the value)= (output[1]-48)*10

This one should remain 0(48-48*10 = 0)

First byte multiplied by 100+ second byte multiplied by 10 + third byte multiplied by 1 = 100+0+0 = 100

stuck igloo
#

what

#

so the first byte, you multiply by 100 right

plucky basin
#

Yes

stuck igloo
#

then why when parsing 10 did you multiply the first byte by 10

plucky basin
#

It didn't was in the 3 digits format, so I directly started by the second one, but the real first should be 0 or null

plucky basin
stuck igloo
#

dude

#

what

#

when you send 10, the first digit is 1

plucky basin
#

Yes

stuck igloo
#

when you send 100 the first digit is 1

plucky basin
#

Yes

stuck igloo
#

where would it ever multiply null*100

plucky basin
stuck igloo
plucky basin
stuck igloo
#

no like what line of code sends the numbers to the computer in 3 digit format

plucky basin
#

the line 65

#

Anyway I have to go to sleep, blobOk I will hope to keep talking about this later, (8 hours, just the time to sleep), FeelsPlasticBag I don't know how exactly explain the code, but to every question I will try answer you

stuck igloo
#

sure. maybe in the morning you'll see what the problem is

plucky basin
#

Yeah, I hope so

#

Bye, cya

stuck igloo
#

bye

plucky basin
#

Uhm,hi

plucky basin
stuck igloo
plucky basin
#

so now I remapped again the value

#

and now I just should have 3 bytes

#

and after I remove a line of code "println" that I used for separate the values, now -3000 doesn't appear anymore

#

the last problem if I remove the sleep function the code runs too fast and the byte order start to get unstable
and the order start swapping
kinda 251 get printed for few seconds, then by the unstability of the code it become 512 for few moments, then goes back to normal

#

now I'm printing another byte, and I'm adding a function in the main.cpp for recognize the right order of the bytes

stuck igloo
#

how you're doing it now is really inefficient and prone to bugs

plucky basin
stuck igloo
#

why not just send the raw binary value

#

don't convert the int to a string, send the string, then convert to int again

#

just send int

plucky basin
#

uhm, I have only a question, where do I store that binary value?
as I see here the library just accept char storage

#

should I change it?

#

I mean

#

modify the library

stuck igloo
#

yeah?

#

modify the library, find another one, or don't use the library at all

plucky basin
#

all right, I will try blobOk thanks for the tip

stuck igloo
#

no problem

plucky basin
#

fu... my wiring to the potentiometer just got damaged uuuuuuuuuuuuuu

plucky basin
#

ok today I got some free time for modify the library

#

so we said instead of getting char, we get directly binary

stuck igloo
#

sure

plucky basin
#

how to not read data from serial, for real