need help on code explanation please

Hi
I'm working on a drone based on:
https://gist.githubusercontent.com/mamdouhmostafa/b1cb5392d0dc58ddc0980f0b58f313df/raw/1c4fb92216b84daf5627947b337eb25ecf2c119e/Quadcopter%2520Setup

I need help to explanation the following code :

EEPROM.write(0, center_channel_1 & 0b11111111);
EEPROM.write(1, center_channel_1 >> 8);
....
 if(center_channel_1 != ((EEPROM.read(1) << 8) | EEPROM.read(0)))error = 1;

seems the value of center_channel_1 was '&' and write to '0' , and then used in if().
what exactly the value changed and why do so?
Thanks
Adam

post_20201130.ino (38.5 KB)

...and that's why you should use code tags

Oh dear. The smileys have invaded your code

Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

Sorry the code file is too large to post into page.

If you put code tags around the bit of code you posted - as suggested - then it'll make sense to the rest of us, rather than us trying to decode smileys ....

As i'm feeling nice ...

    Serial.println(F("Done!"));
    EEPROM.write(0, center_channel_1 & 0b11111111);
    EEPROM.write(1, center_channel_1 >> 8);
    EEPROM.write(2, center_channel_2 & 0b11111111);
    EEPROM.write(3, center_channel_2 >> 8);

And

    //To make sure evrything is ok, verify the EEPROM data.
    Serial.println(F("Verify EEPROM data"));
    delay(1000);
    if(center_channel_1 != ((EEPROM.read(1) << 8) | EEPROM.read(0)))error = 1;
    if(center_channel_2 != ((EEPROM.read(3) << 8) | EEPROM.read(2)))error = 1;

It also help to know that centre_channel_1 is defined as an int.

The first bit of code is writing a 16-bit int to the EEPROM by splitting it into 2 8-bit values. The second bit of code is re-assembling the 2 8-bit values back into the 16-bit int and checking that they match.

markd833:
If you put code tags around the bit of code you posted - as suggested - then it'll make sense to the rest of us, rather than us trying to decode smileys ....

As i'm feeling nice ...

    Serial.println(F("Done!"));

EEPROM.write(0, center_channel_1 & 0b11111111);
   EEPROM.write(1, center_channel_1 >> 8);
   EEPROM.write(2, center_channel_2 & 0b11111111);
   EEPROM.write(3, center_channel_2 >> 8);



And


//To make sure evrything is ok, verify the EEPROM data.
   Serial.println(F("Verify EEPROM data"));
   delay(1000);
   if(center_channel_1 != ((EEPROM.read(1) << 8) | EEPROM.read(0)))error = 1;
   if(center_channel_2 != ((EEPROM.read(3) << 8) | EEPROM.read(2)))error = 1;



It also help to know that centre_channel_1 is defined as an int.

The first bit of code is writing a 16-bit int to the EEPROM by splitting it into 2 8-bit values. The second bit of code is re-assembling the 2 8-bit values back into the 16-bit int and checking that they match.

Thank you so much for help. and also I learn how to put section of code.

laoadam:
Thank you so much for help. and also I learn how to put section of code.

Hey! You've only been here for four years.

TheMemberFormerlyKnownAsAWOL:
Hey! You've only been here for four years.

:wink:

again, the following code write center_channel, low_channel, high_channel values into EEPROM, the thing is I didn't find any where the high_channel defined, I mean not assigned like:
low_channel_1 = receiver_input_channel_1;
how can write it?

if(error == 0){
    //If all is good, store the information in the EEPROM
    Serial.println(F(""));
    Serial.println(F("==================================================="));
    Serial.println(F("Storing EEPROM information"));
    Serial.println(F("==================================================="));
    Serial.println(F("Writing EEPROM"));
    delay(1000);
    Serial.println(F("Done!"));
    EEPROM.write(0, center_channel_1 & 0b11111111); 
    EEPROM.write(1, center_channel_1 >> 8);         
    EEPROM.write(2, center_channel_2 & 0b11111111);
    EEPROM.write(3, center_channel_2 >> 8);
    EEPROM.write(4, center_channel_3 & 0b11111111);
    EEPROM.write(5, center_channel_3 >> 8);
    EEPROM.write(6, center_channel_4 & 0b11111111);
    EEPROM.write(7, center_channel_4 >> 8);
    EEPROM.write(10, high_channel_2 & 0b11111111);
    EEPROM.write(11, high_channel_2 >> 8);
    EEPROM.write(12, high_channel_3 & 0b11111111);
    EEPROM.write(13, high_channel_3 >> 8);
    EEPROM.write(14, high_channel_4 & 0b11111111);
    EEPROM.write(15, high_channel_4 >> 8);
    EEPROM.write(16, low_channel_1 & 0b11111111);
    EEPROM.write(17, low_channel_1 >> 8);
    EEPROM.write(18, low_channel_2 & 0b11111111);
    EEPROM.write(19, low_channel_2 >> 8);
    EEPROM.write(20, low_channel_3 & 0b11111111);

Well, it's obviously not in that snippet, is it?

TheMemberFormerlyKnownAsAWOL:
Well, it's obviously not in that snippet, is it?

Thanks.
Sure, sorry I did't attached the code here, it was attached at the topic post. where the low_channel and center_channel was assigned, but the high_channel.
Best

Perhaps this is what you are looking for ....

    if(receiver_input_channel_1 < low_channel_1)low_channel_1 = receiver_input_channel_1;
    if(receiver_input_channel_2 < low_channel_2)low_channel_2 = receiver_input_channel_2;
    if(receiver_input_channel_3 < low_channel_3)low_channel_3 = receiver_input_channel_3;
    if(receiver_input_channel_4 < low_channel_4)low_channel_4 = receiver_input_channel_4;
    if(receiver_input_channel_1 > high_channel_1)high_channel_1 = receiver_input_channel_1;
    if(receiver_input_channel_2 > high_channel_2)high_channel_2 = receiver_input_channel_2;
    if(receiver_input_channel_3 > high_channel_3)high_channel_3 = receiver_input_channel_3;
    if(receiver_input_channel_4 > high_channel_4)high_channel_4 = receiver_input_channel_4;

markd833:
Perhaps this is what you are looking for ....

    if(receiver_input_channel_1 < low_channel_1)low_channel_1 = receiver_input_channel_1;

if(receiver_input_channel_2 < low_channel_2)low_channel_2 = receiver_input_channel_2;
   if(receiver_input_channel_3 < low_channel_3)low_channel_3 = receiver_input_channel_3;
   if(receiver_input_channel_4 < low_channel_4)low_channel_4 = receiver_input_channel_4;
   if(receiver_input_channel_1 > high_channel_1)high_channel_1 = receiver_input_channel_1;
   if(receiver_input_channel_2 > high_channel_2)high_channel_2 = receiver_input_channel_2;
   if(receiver_input_channel_3 > high_channel_3)high_channel_3 = receiver_input_channel_3;
   if(receiver_input_channel_4 > high_channel_4)high_channel_4 = receiver_input_channel_4;

Thank you.
I checked this, it confused me cause of it used 'high_channel' as a condition to do compare before assigned 'high_channel'.
Is it OK to do that way?
Best

The code is simply updating the max and min values seen on each channel.

markd833:
The code is simply updating the max and min values seen on each channel.

Thanks.
I was tangled how the first time 'if' compare to execute if the high_channel wasn't assigned a value.
Best

laoadam:
Thanks.
I was tangled how the first time 'if' compare to execute if the high_channel wasn't assigned a value.
Best

A variable will always have a value. What that value is depends on how and where it is declared

This is the declaration

int high_channel_1, high_channel_2, high_channel_3, high_channel_4;
int low_channel_1, low_channel_2, low_channel_3, low_channel_4;

As this is done at the global level the variables will automatically be set to zero

Incidentally, complicated code like this

    EEPROM.write(8, high_channel_1 & 0b11111111);
    EEPROM.write(9, high_channel_1 >> 8);
//later
    if(high_channel_1 != ((EEPROM.read(9) << 8) | EEPROM.read(8)))error = 1;

would be unnecessary if you used EEPROM.put() and EEPROM.get() to save and load the values which could be done in a single statement for each

Out of interest, what is the highest value that any of the channel variables could be ?

UKHeliBob:
A variable will always have a value. What that value is depends on how and where it is declared

This is the declaration

int high_channel_1, high_channel_2, high_channel_3, high_channel_4;

int low_channel_1, low_channel_2, low_channel_3, low_channel_4;



As this is done at the global level the variables will automatically be set to zero

Incidentally, complicated code like this


EEPROM.write(8, high_channel_1 & 0b11111111);
   EEPROM.write(9, high_channel_1 >> 8);
//later
   if(high_channel_1 != ((EEPROM.read(9) << 8) | EEPROM.read(8)))error = 1;



would be unnecessary if you used EEPROM.put() and EEPROM.get() to save and load the values which could be done in a single statement for each

Out of interest, what is the highest value that any of the channel variables could be ?

Thank you.
My model is not ready to do the measurement yet, according to the controller code, the the highest value that any of the channel was set to 2000, and the min. set as 1000. The code we are talking now is for setup.
It is: Brokking.net - Project YMFC-AL - The Arduino auto-level quadcopter - Home.

Best

I measured the ESC pulse during drone motor running, the front L/R = 1200, and rear L/R 740 ? why such big gap?

The measurements above was done when push the pitch rod only, I mean I just let it go straight up, and the drone is assembled quite balanced and stand on the floor.

I was expect the 4 motors should run same RPM? or it will flip off, in fact it did flip back to front. don't know why?

the following code is for the drone straight up take off (if i'm not wrong), I don't know why?

if (start == 2){ //The motors are started.[color=#181818][/color]
if (throttle > 1800) throttle = 1800; //We need some room to keep full control at full throttle.[color=#181818][/color]
esc_1 = throttle - pid_output_pitch + pid_output_roll - pid_output_yaw; //Calculate the pulse for esc 1 (front-right - CCW)[color=#181818][/color]
esc_2 = throttle + pid_output_pitch + pid_output_roll + pid_output_yaw; //Calculate the pulse for esc 2 (rear-right - CW)[color=#181818][/color]
esc_3 = throttle + pid_output_pitch - pid_output_roll - pid_output_yaw; //Calculate the pulse for esc 3 (rear-left - CCW)[color=#181818][/color]
esc_4 = throttle - pid_output_pitch - pid_output_roll + pid_output_yaw; //Calculate the pulse for esc 4 (front-left - CW)

Best