Go Down

Topic: Please explain why Pin13 and Serial.write misbehaving (Read 569 times) previous topic - next topic

memotick

I stripped my code to the minimum and I just don't understand why I am getting pin 13 toggling and Serial.write not working in one case but not in the other.

I have three files: Debug the arduino sketch, Debug.h (the library header file) and Debug.cpp (the C++ library file)

With Debug.cpp as is shown below, I get pin 13 toggling on an UNO board and nothing is printed to serial out.
If I replace the "array[halfarraySize];" with "float array[halfarraySize];" in Debug.cpp, pin13 and the serial print behave properly.  Can you please explain what I am doing wrong and/or what is going on?

Thanks in advance!

Debug sketch:
Code: [Select]
#include <Debug.h>

const int arraySize=118;

debugClass debug_object(arraySize);

void setup() {

 Serial.begin(115200);  

 Serial.write("test");

}

void loop() {
}


Debug.h:
Code: [Select]
#ifndef Debug_h
#define Debug_h

#include "Arduino.h"
class debugClass{

public:
debugClass(const int arraySize);

   // Keep as many variables private as possible
private:
   int halfarraySize;
   float array[];
};

#endif


Debug.cpp:
Code: [Select]
#include "Debug.h"

debugClass::debugClass(const int arraySize){
const int halfarraySize = arraySize/2;
   // This needs to be declared or prints have issues
   array[halfarraySize];
   
for(int i = 0; i < halfarraySize; i++){
array[i] = 0;
}
}

UKHeliBob

Just as a matter of interest, what is the code supposed to do apart from filling up half the array ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

memotick


Just as a matter of interest, what is the code supposed to do apart from filling up half the array ?


I'm trying to implement a circular buffer for some experiments

PeterH

Would you expect to be able to define an array member field of unspecified size just by putting 'array[size];' in the body of the constructor? I would have thought you'd need to use a template to enable the size to be defined at instantiation time, or allocate the array dynamically. Perhaps what you're doing is legitimate, but it doesn't look that way to me; I suspect what you're actually doing is trampling over initialised memory that will be needed by the Arduino runtime later one.
I only provide help via the forum - please do not contact me for private consultancy.

HazardsMind

I believe Peter is correct, look at this link, http://www.cplusplus.com/doc/tutorial/templates/ under Non-type parameters for templates
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

memotick


Would you expect to be able to define an array member field of unspecified size just by putting 'array[size];' in the body of the constructor? I would have thought you'd need to use a template to enable the size to be defined at instantiation time, or allocate the array dynamically. Perhaps what you're doing is legitimate, but it doesn't look that way to me; I suspect what you're actually doing is trampling over initialised memory that will be needed by the Arduino runtime later one.


I think you are referring to the Debug.h private declaration "float array[];" no?

You make a good point, and suspect your theory is correct.  However, shouldn't the variable be declared in the header file?  Perhaps my assumptions are incorrect (completely possible as I'm learning this as I go).

When you say the array size should be enabled at instantiation time, where would this be located?

Thanks for the help

lloyddean

Code: [Select]

template<int N>
class debugClass
{
private:
    const int   array_length;
    float       array[N];

public:
    debugClass() : array_length(N)
    {
        for ( int i = array_length; i--; )
        {
            array[i] = 0;
        }
    }
};

debugClass <12>    debug;

memotick


Code: [Select]

template<int N>
class debugClass
{
private:
    const int   array_length;
    float       array[N];

public:
    debugClass() : array_length(N)
    {
        for ( int i = array_length; i--; )
        {
            array[i] = 0;
        }
    }
};

debugClass <12>    debug;



First of all, thank you for kindly writing this.  Would this code be placed in Debug.h?
I'm reading up on Templates, I was not aware of them.

lloyddean

Except for that last line yes as the last line instantiates and instance with an array length of 12 named 'debug' - place that in an implementation file where used.  Adjust array length as necessary.

memotick

Sorry but I can't wrap my head around this yet.  I'm still studying templates and dynamic memory to understand what is needed.I tried using templates but am getting errors

Debug sketch:
Code: [Select]
#include <Debug.h>

const int arraySize=118;

//debugClass debug_object(arraySize);
debugClass <arraySize> debug_object;
void setup() {

  Serial.begin(115200); 

  Serial.write("test");

}

void loop() {
}


Debug.h:
Code: [Select]
#ifndef Debug_h
#define Debug_h

#include "Arduino.h"

template<int N>
class debugClass
{
private:
    const int   array_length;
    float       array[N];
   
public:
    debugClass() : array_length(N)
    {
        for ( int i < array_length/2; i--; )
        {
            array[i] = 0;
        }
    }
};

#endif


Debug.cpp:
Code: [Select]
#include "Debug.h"

debugClass::debugClass(){

}


Errors:
Code: [Select]
In file included from Debug.ino:1:
/Applications/Arduino.app/Contents/Resources/Java/libraries/Debug/Debug.h: In constructor 'debugClass<N>::debugClass()':
/Applications/Arduino.app/Contents/Resources/Java/libraries/Debug/Debug.h:16: error: expected initializer before '<' token
/Applications/Arduino.app/Contents/Resources/Java/libraries/Debug/Debug.h:16: error: 'i' was not declared in this scope

lloyddean

#10
Feb 05, 2013, 02:14 am Last Edit: Feb 05, 2013, 02:18 am by lloyddean Reason: 1
Sorry I hadn't seen your question earlier -

Code: [Select]

template<int N>
class debugClass
{
   const int   halfarraySize;
   float       array[N / 2];

public:
   debugClass() : halfarraySize(N / 2)
   {
       for ( int i = halfarraySize; i--; )
       {
           array[i] = 0;
       }
   }
};

const int arraySize = 118;

debugClass<arraySize> debug_object;

void setup()
{
   Serial.begin(115200);  
   Serial.write("test");
}

void loop()
{}


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy