MIT app inventor porject

Hi,
I'm working on a project to control 5 servo motors at once using the HC-06 module. I seen some code online that has helped me but I'm a little confused on this certain part.

unsigned int servopos = BTSerial.read();
unsigned int servopos1 = BTSerial.read();
unsigned int realservo = (servopos1 *256) + servopos; 
Serial.println(realservo);

Why do we need to have two unsigned int servopos when controlling the motors. I understand multiplying it by 256 to change from one byte to two but why have two servopos and add them?

Two successive received bytes are combined to make up one 16 bit integer quantity.

So servopos is already given as binary so multiplying servopos1 by 256 converts from decimal to binary why do we do this then for servopos1?

To convert a binary high byte and a binary low byte to a binary 16 bit integer, the formula is

integer = 256*(high byte) + (low byte)

alternatively in C/C++

unsigned int value = highbyte<<8 + lowbyte;

probably a dumb question but why are we receiving a high and low byte values if im trying to set a slider to move the servo at any angle between 0 and 180 degrees and to then have it mapped out like this int servo1 = realservo; servo1 = map(servo1, 1000, 1180, 0, 179);

i understand on the MIT website ive set the range for each slider between 1000 and 1180 and 2000 2180 etc but to then have 0, 179 after it?

HC-06 data transmission consists of bytes.

To send a 16 bit value via HC-06, you send the high and low byte individually and reconstruct the integer at the receiver.

to then have 0, 179 after it?

Look up how the map() function is used in the Arduino reference pages.

really appreciate the help im self teaching myself coding tysm!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.