code compiles, but gives strange numbers on serial

have a look. this thing so far is supposed to print the pulse duration over serial, but i get gibberish. ignore the comments. I am not very good at this, so the code looks messy

drone.ino (7.64 KB)

ok, got a bit farther. now only the aile works, or the throttle, depending on where I put the curly braces, but is there a way to have them both work? should I use a for , or or other way?

Please feel free.to share your code. In code tags.

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

whoa whoa. thnx for the tricks in the ide. the code works, no error messages it uploads, and works, but not the way I want it to.here only the aile works, the thro does not. they will work if i change the curly braces, but i need help with finding a method on how to make the both work at the same time,without me changing the code. here is what I have

#include <Servo.h>

Servo servo1r;
Servo servo2l;
Servo servo3r;
Servo servo4l;

const int rServo1r = 6;
const int lServo2l = 7;
const int rServo3r = 8;
const int lServo4l = 9;


const int xaxis = A0;
const int yaxis = A1;


int Thro = 0;
const int Thropin = 3;
volatile boolean Thro_NewSignal = false;
volatile int Thro_Value = 0;// volatile, we set this in the Interrupt and read it in loop so it must be declared volatile
volatile unsigned long Thro_StartPeriod = 0; // set in the interrupt
bool ThroInit = false;
bool ThroState = false;


int Ail = 0;
const int Ailpin = 2;
volatile boolean Ail_NewSignal = false;
volatile int Ail_Value = 0;// volatile, we set this in the Interrupt and read it in loop so it must be declared volatile
volatile unsigned long Ail_StartPeriod = 0; // set in the interrupt
bool AilInit = false;
bool AilState = false;

int Ele = 0;
const int Elepin = 4;
volatile boolean Ele_NewSignal = false;
volatile int Ele_Value = 0;// volatile, we set this in the Interrupt and read it in loop so it must be declared volatile
volatile unsigned long Ele_StartPeriod = 0; // set in the interrupt
bool EleInit = false;
bool EleState = false;


int Rud = 0;
const int Rudpin = 5;
volatile boolean Rud_NewSignal = false;
volatile int Rud_Value = 0;// volatile, we set this in the Interrupt and read it in loop so it must be declared volatile
volatile unsigned long Rud_StartPeriod = 0; // set in the interrupt
bool RudInit = false;
bool RudState = false;



void setup()
{

 servo1r.attach(rServo1r);
 servo2l.attach(lServo2l);
 servo3r.attach(rServo3r);
 servo4l.attach(lServo4l);

 pinMode(xaxis, INPUT);
 pinMode(yaxis, INPUT);



 Serial.begin(9600);
}



void loop()
{


 PollThroSignalChangedProcessing();

 if (Thro_NewSignal)
 {

   Thro_NewSignal = false;
 }
 PollAilSignalChangedProcessing();

 if (Ail_NewSignal)
 {
   //Serial.println("power");

   //Serial.print("Ail "); Serial.println(Ail_Value);
   Ail_NewSignal = false;





   /*


     //int xstabil = 500;
     //xstabil = analogRead(xaxis);
     //int ystabil = 500;
     //ystabil = analogRead(yaxis);

     int tiltf = 0;
     int tiltb = 0;
     int tiltl = 0;
     int tiltr = 0;
     //Serial.println("power");
     int aile = 0;
     aile = map(Ail_Value, 990, 1950, -20, 20);

     int ele = 0;
     ele = map(Ele_Value, 990, 1950, -20, 20);

     //Serial.println("power");

     while
     (xstabil >= 513){
       tiltf = map(xstabil, 0, 1023, 0, 20) + ele;
     }
     while(xstabil <= 512){
     tiltb = map(xstabil, 0, 1023, 0, 20) + ele;

     }

     while(ystabil >= 513)
     {
     tiltl = map(ystabil, 0, 1023, 0, 20) + aile;
     }


     while(ystabil <= 512)

     {
     tiltr = map(ystabil, 0, 1023, 0, 20) + aile;
     }


     int angle1 = 0;
     int angle2 = 0;
     int angle3 = 0;
     int angle4 = 0;

     angle1 = tiltl + tiltf;

     angle2 = tiltf + tiltr;

     angle3 = tiltr + tiltb;

     angle4 = tiltb + tiltl;


     int power = map(Thro_Value, 990, 1950, 0, 180);
     servo1r.write(power - angle1);
     servo2l.write(power - angle2);
     servo3r.write(power - angle3);
     servo4l.write(power - angle4);
   */

   //Serial.println("aile");
   Serial.print("Ail "); Serial.println(Ail_Value);

   //Serial.print("thro"); Serial.println(Thro_Value);



 }
}

void PollThroSignalChangedProcessing()
{
 if (ThroInit == false)
 {
   ThroInit = true;
   ThroState = (digitalRead(Thropin) == HIGH);
   return;
 }

 bool newThroState = digitalRead(Thropin) == HIGH;

 if (newThroState == ThroState)
   return;

 ThroState = newThroState;

 // if the pin is high, its the start of an interrupt
 if (ThroState)
 {
   // get the time using micros - when our code gets really busy this will become inaccurate, but for the current application its
   // easy to understand and works very well
   Thro_StartPeriod = micros();
 }
 else
 {
   // if the pin is low, its the falling edge of the pulse so now we can calculate the pulse duration by subtracting the
   // start time Thro_StartPeriod from the current time returned by micros()
   if (Thro_StartPeriod && (Thro_NewSignal == false))
   {
     Thro_Value = (int)(micros() - Thro_StartPeriod);
     Thro_StartPeriod = 0;

     // tell loop we have a new signal on the throttle channel
     // we will not update Thro_Value until loop sets
     // Thro_NewSignal back to false
     Thro_NewSignal = true;
   }
 }
}

void PollAilSignalChangedProcessing()
{
 if (AilInit == false)
 {
   AilInit = true;
   AilState = (digitalRead(Ailpin) == HIGH);
   return;
 }

 bool newAilState = digitalRead(Ailpin) == HIGH;

 if (newAilState == AilState)
   return;

 AilState = newAilState;

 // if the pin is high, its the start of an interrupt
 if (AilState)
 {
   // get the time using micros - when our code gets really busy this will become inaccurate, but for the current application its
   // easy to understand and works very well
   Ail_StartPeriod = micros();
 }
 else
 {
   // if the pin is low, its the falling edge of the pulse so now we can calculate the pulse duration by subtracting the
   // start time Thro_StartPeriod from the current time returned by micros()
   if (Ail_StartPeriod && (Ail_NewSignal == false))
   {
     Ail_Value = (int)(micros() - Ail_StartPeriod);
     Ail_StartPeriod = 0;

     // tell loop we have a new signal on the throttle channel
     // we will not update Thro_Value until loop sets
     // Thro_NewSignal back to false
     Ail_NewSignal = true;
   }
 }
}


void PollEleSignalChangedProcessing()
{
 if (EleInit == false)
 {
   EleInit = true;
   EleState = (digitalRead(Elepin) == HIGH);
   return;
 }

 bool newEleState = digitalRead(Elepin) == HIGH;

 if (newEleState == EleState)
   return;

 EleState = newEleState;

 // if the pin is high, its the start of an interrupt
 if (EleState)
 {
   // get the time using micros - when our code gets really busy this will become inaccurate, but for the current application its
   // easy to understand and works very well
   Ele_StartPeriod = micros();
 }
 else
 {
   // if the pin is low, its the falling edge of the pulse so now we can calculate the pulse duration by subtracting the
   // start time Thro_StartPeriod from the current time returned by micros()
   if (Ele_StartPeriod && (Ele_NewSignal == false))
   {
     Ele_Value = (int)(micros() - Ele_StartPeriod);
     Ele_StartPeriod = 0;

     // tell loop we have a new signal on the throttle channel
     // we will not update Thro_Value until loop sets
     // Thro_NewSignal back to false
     Ele_NewSignal = true;
   }
 }
}


void PollRudSignalChangedProcessing()
{
 if (RudInit == false)
 {
   RudInit = true;
   RudState = (digitalRead(Rudpin) == HIGH);
   return;
 }

 bool newRudState = digitalRead(Rudpin) == HIGH;

 if (newRudState == RudState)
   return;

 RudState = newRudState;

 // if the pin is high, its the start of an interrupt
 if (RudState)
 {
   // get the time using micros - when our code gets really busy this will become inaccurate, but for the current application its
   // easy to understand and works very well
   Rud_StartPeriod = micros();
 }
 else
 {
   // if the pin is low, its the falling edge of the pulse so now we can calculate the pulse duration by subtracting the
   // start time Thro_StartPeriod from the current time returned by micros()
   if (Rud_StartPeriod && (Rud_NewSignal == false))
   {
     Rud_Value = (int)(micros() - Rud_StartPeriod);
     Rud_StartPeriod = 0;

     // tell loop we have a new signal on the throttle channel
     // we will not update Thro_Value until loop sets
     // Thro_NewSignal back to false
     Rud_NewSignal = true;
   }
 }
}

Thats not a code tag. (please edit your post)

that better?

    //Serial.print("thro"); Serial.println(Thro_Value);

Have you deliberately commented this line ?

that comment and the like where when i was debugging, and i can reuse them to test where the code gets to

vroomZOOM: that comment and the like where when i was debugging, and i can reuse them to test where the code gets to

I thought that your problem was that the throttle value was not printing. If that is not the problem then please explain what is wrong.

my question is how to make BOTH the aile and thro print in serial at the same time. they work independently if i change the braces, quite well, but if i put the brackets to make them both print in serial, I get gibberish.

vroomZOOM: my question is how to make BOTH the aile and thro print in serial at the same time. they work independently if i change the braces, quite well, but if i put the brackets to make them both print in serial, I get gibberish.

I can't follow what you're trying to say here. Can you just show the Serial.print() that you want to work, or what you've tried that didn't work. Also, why all the volatile type qualifier for so many variables? I don't see any interrupt routines, which is where that is often used.

this configuration works for aile

I have just put in the loop section of the code, the rest is unchanged

void loop()
{


  PollThroSignalChangedProcessing();

  if (Thro_NewSignal)
  {

    Thro_NewSignal = false;
  }
  PollAilSignalChangedProcessing();

  if (Ail_NewSignal)
  {
    //Serial.println("power");

    //Serial.print("Ail "); Serial.println(Ail_Value);
    Ail_NewSignal = false;





    /*


      //int xstabil = 500;
      //xstabil = analogRead(xaxis);
      //int ystabil = 500;
      //ystabil = analogRead(yaxis);

      int tiltf = 0;
      int tiltb = 0;
      int tiltl = 0;
      int tiltr = 0;
      //Serial.println("power");
      int aile = 0;
      aile = map(Ail_Value, 990, 1950, -20, 20);

      int ele = 0;
      ele = map(Ele_Value, 990, 1950, -20, 20);

      //Serial.println("power");

      while
      (xstabil >= 513){
        tiltf = map(xstabil, 0, 1023, 0, 20) + ele;
      }
      while(xstabil <= 512){
      tiltb = map(xstabil, 0, 1023, 0, 20) + ele;

      }

      while(ystabil >= 513)
      {
      tiltl = map(ystabil, 0, 1023, 0, 20) + aile;
      }


      while(ystabil <= 512)

      {
      tiltr = map(ystabil, 0, 1023, 0, 20) + aile;
      }


      int angle1 = 0;
      int angle2 = 0;
      int angle3 = 0;
      int angle4 = 0;

      angle1 = tiltl + tiltf;

      angle2 = tiltf + tiltr;

      angle3 = tiltr + tiltb;

      angle4 = tiltb + tiltl;


      int power = map(Thro_Value, 990, 1950, 0, 180);
      servo1r.write(power - angle1);
      servo2l.write(power - angle2);
      servo3r.write(power - angle3);
      servo4l.write(power - angle4);
    */

    //Serial.println("aile");
    Serial.print("Ail "); Serial.println(Ail_Value);

    //Serial.print("thro"); Serial.println(Thro_Value);



  }
}

now this works for thro

void loop()
{


  PollThroSignalChangedProcessing();

  if (Thro_NewSignal)
  {

    Thro_NewSignal = false;
  }
  PollAilSignalChangedProcessing();

  if (Ail_NewSignal)
  {
    //Serial.println("power");

    //Serial.print("Ail "); Serial.println(Ail_Value);
    Ail_NewSignal = false;





    /*


      //int xstabil = 500;
      //xstabil = analogRead(xaxis);
      //int ystabil = 500;
      //ystabil = analogRead(yaxis);

      int tiltf = 0;
      int tiltb = 0;
      int tiltl = 0;
      int tiltr = 0;
      //Serial.println("power");
      int aile = 0;
      aile = map(Ail_Value, 990, 1950, -20, 20);

      int ele = 0;
      ele = map(Ele_Value, 990, 1950, -20, 20);

      //Serial.println("power");

      while
      (xstabil >= 513){
        tiltf = map(xstabil, 0, 1023, 0, 20) + ele;
      }
      while(xstabil <= 512){
      tiltb = map(xstabil, 0, 1023, 0, 20) + ele;

      }

      while(ystabil >= 513)
      {
      tiltl = map(ystabil, 0, 1023, 0, 20) + aile;
      }


      while(ystabil <= 512)

      {
      tiltr = map(ystabil, 0, 1023, 0, 20) + aile;
      }


      int angle1 = 0;
      int angle2 = 0;
      int angle3 = 0;
      int angle4 = 0;

      angle1 = tiltl + tiltf;

      angle2 = tiltf + tiltr;

      angle3 = tiltr + tiltb;

      angle4 = tiltb + tiltl;


      int power = map(Thro_Value, 990, 1950, 0, 180);
      servo1r.write(power - angle1);
      servo2l.write(power - angle2);
      servo3r.write(power - angle3);
      servo4l.write(power - angle4);
    */

    //Serial.println("aile");
    //Serial.print("Ail "); Serial.println(Ail_Value);

    Serial.print("thro"); Serial.println(Thro_Value);



  }
}

but if i do this it does not work

void loop()
{


  PollThroSignalChangedProcessing();

  if (Thro_NewSignal)
  {

    Thro_NewSignal = false;
  }
  PollAilSignalChangedProcessing();

  if (Ail_NewSignal)
  {
    //Serial.println("power");

    //Serial.print("Ail "); Serial.println(Ail_Value);
    Ail_NewSignal = false;





    /*


      //int xstabil = 500;
      //xstabil = analogRead(xaxis);
      //int ystabil = 500;
      //ystabil = analogRead(yaxis);

      int tiltf = 0;
      int tiltb = 0;
      int tiltl = 0;
      int tiltr = 0;
      //Serial.println("power");
      int aile = 0;
      aile = map(Ail_Value, 990, 1950, -20, 20);

      int ele = 0;
      ele = map(Ele_Value, 990, 1950, -20, 20);

      //Serial.println("power");

      while
      (xstabil >= 513){
        tiltf = map(xstabil, 0, 1023, 0, 20) + ele;
      }
      while(xstabil <= 512){
      tiltb = map(xstabil, 0, 1023, 0, 20) + ele;

      }

      while(ystabil >= 513)
      {
      tiltl = map(ystabil, 0, 1023, 0, 20) + aile;
      }


      while(ystabil <= 512)

      {
      tiltr = map(ystabil, 0, 1023, 0, 20) + aile;
      }


      int angle1 = 0;
      int angle2 = 0;
      int angle3 = 0;
      int angle4 = 0;

      angle1 = tiltl + tiltf;

      angle2 = tiltf + tiltr;

      angle3 = tiltr + tiltb;

      angle4 = tiltb + tiltl;


      int power = map(Thro_Value, 990, 1950, 0, 180);
      servo1r.write(power - angle1);
      servo2l.write(power - angle2);
      servo3r.write(power - angle3);
      servo4l.write(power - angle4);
    */

    //Serial.println("aile");
    Serial.print("Ail "); Serial.println(Ail_Value);

    Serial.print("thro"); Serial.println(Thro_Value);



  }
}

if i do this it does not work

Most of the lines in that code are commented out,

The only active code is

void loop()
{
  PollThroSignalChangedProcessing();
  if (Thro_NewSignal)
  {
    Thro_NewSignal = false;
  }
  PollAilSignalChangedProcessing();

  if (Ail_NewSignal)
  {
    Ail_NewSignal = false;
    Serial.print("Ail "); Serial.println(Ail_Value);
    Serial.print("thro"); Serial.println(Thro_Value);
  }
}

Is that intentional ?

vroomZOOM:
but if i do this it does not work

void loop()

{

PollThroSignalChangedProcessing();

if (Thro_NewSignal)
  {

Thro_NewSignal = false;
  }
  PollAilSignalChangedProcessing();

if (Ail_NewSignal)
  {
    Ail_NewSignal = false;

Serial.print("Ail "); Serial.println(Ail_Value);

Serial.print(“thro”); Serial.println(Thro_Value);

}
}

What do you expect it to do? I expect that it will send out both values together, but only when the aileron changes, not when the throttle changes.

I did not quite follow your code (too complicated early in the morning ;) )

But maybe this helps you on the way.

void loop()
{
  PollThroSignalChangedProcessing();
  PollAilSignalChangedProcessing();

  if(Thro_NewSignal == true || Ail_NewSignal == true)
  {
    // print both
  }
  
  if (Thro_NewSignal)
  {
    ...
    ...
    Thro_NewSignal = false;
  }

  if (Ail_NewSignal)
  {
    ...
    ...
    Ail_NewSignal = false;
  }
}

here I am trying to read all 4 channels on an rc receiver and print out all 4 onto serial. I can get one to work, but not two or more, that is my objective. here what i want is to print the aile value which changes when i make change on my radio and then the throttle value when i change it on the radio. i am so far getting constant numbers that do not change when i tell serial to print out both channels. it works when i comment one out.

  if (Ail_NewSignal)
  {
    Ail_NewSignal = false;
    Serial.print("Ail "); Serial.println(Ail_Value);
    Serial.print("thro"); Serial.println(Thro_Value);
  }

As has been pointed out, in your code above the throttle value is printed when there is a new aileron signal. That is not what you say you want.

Surely you want to print the throttle value when it has changed.