Controlling quadcopter over serial

Hello!

My question is in my arduino site, because it was too long to this forum. Question about quadcopter serial code | Arduino projektini

Please don't take care of   Wordpress made them for some reason.

Thank you for help!

If I download that code to arduino it works fine, but if I download the whole quadcopter code with gyroscope and accellometer I can only send one time pwm value over serial to arduino.

It sounds to me that it is doing a lot of processing, (not surprised though), that is a lot of code.

Can you make it a little more friendly on the eyes? Better yet, put it in the proper format and make it an attachment. Do it for both the good code and the bad one.

if(pin == 03)

Why octal?

Is this better now?

gyro_quadcopter_2.ino (10.8 KB)

if(pin == 03)

Why octal?

What do you mean with octal? And code which contains

if(pin == 03)

works fine.

yes. Im able to compare your two codes and I noticed that the "bad one" is using an array to collect the data and then convert it, while the working on just gets a '1', '2' ... directly.

Also the bad one has a lot more Serial.prints, which will slow down the arduino's serial communication. Using an array is fine, in fact it is perferred, but you need to know how to send, receive and distribute the data correctly. Try something small scale and see if you can send data from the serial monitor to the arduino and have it split the data and send it back.

eliasojala1234:

if(pin == 03)

Why octal?

What do you mean with octal? And code which contains

if(pin == 03)

works fine.

He is asking (yet not explaining why he is asking for the uninitiated) why you are using an octal (base 8 ) value. This is because standard C syntax states that any numeric values that start with a zero which is not followed by an 'x' or a 'b' is interpreted as octal. In your case it really doesn't matter that much (but can lead to a bad habit) because the actual value is less than 8, but if you had 011 then that would equate to 9 in decimal, so 011 + 03 would equal 12 (in decimal), not 15. It's not quite defined (meaning different compilers may handle it differently) what would happen if you used 08 or 09. Some compilers may choke on this seemingly inexplicably unless one is familiar with octal notation, and others may assume that the coder really meant decimal and not mention the possible bad interactions if the bad habit isn't corrected.

(Too bad octal notation hasn't been depreciated into oblivion. It's use is such a special case now that most people group binary into sets of 4 bits instead of sets of 3 bits.)

yet not explaining why he is asking for the uninitiated

It's a useful technique for calibrating the level of "initiation".
There may have been a reason for the use of octal (DEC background?) - I wanted to find that reason.

AWOL:

yet not explaining why he is asking for the uninitiated

It's a useful technique for calibrating the level of "initiation".
There may have been a reason for the use of octal (DEC background?) - I wanted to find that reason.

I've never used it, but it was demonstrated to me in classes (in the '90s) as used with 6-bit processors (so, yeah, old PDPs). With a 6bit processor, two octal digits is used just like we use 2 hex digits with 8bit processors. But that is all pretty much ancient history as far as computers and technology anymore. Even our language is sets-of-4 centric: nibble == 4 bits, byte == 24 bits, etc. I have no idea what 3 bits or 23 bits are or were called.

As interesting as it is, this history lesson is probably drifting way off target for this thread on quadcopters. Oooo.. I just thought of a modern use for octal... motor on/off state flags for a tri-copter. (Yay! That's a little closer to the thread topic...) Maybe not... :stuck_out_tongue:

Thank you for your answers! No everything works fine. I changed 02 to '2' and 03 to '3' etc. Ready code is in attachment.

gyro_quadcopter_2.ino (10.7 KB)

int pin = Serial.read();
      char buffer1[] = {' ',' ',' '}; // Receive up to 3 bytes
      while(!Serial.available());
      Serial.readBytesUntil('\n', buffer1, 3);
      int pwm = atoi(buffer1);

This is worrying.
I don't think "readBytesUntil" terminates the array and you aren't terminating it explicitly, so it isn't valid to call "atoi".

I think a clean-up of code, concentrating on better (and correct) use of arrays would result in much shorter code - can you see how code like

 if(pin == '2') {
        pwm2 = pwm;
        Serial.print("2: ");
        Serial.println(pwm2);

repeats with only tiny differences? Some arrays would sort that out, and make it easier to concentrate on the real issues.