Pages: [1]   Go Down
Author Topic: Please explain why Pin13 and Serial.write misbehaving  (Read 452 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#include <Debug.h>

const int arraySize=118;

debugClass debug_object(arraySize);

void setup() {

  Serial.begin(115200);  

  Serial.write("test");

}

void loop() {
}

Debug.h:
Code:
#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:
#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;
}
}
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4242
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Queens, New York
Online Online
Faraday Member
**
Karma: 98
Posts: 3576
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe Peter is correct, look at this link, http://www.cplusplus.com/doc/tutorial/templates/ under Non-type parameters for templates
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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;
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
#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:
#include "Debug.h"

debugClass::debugClass(){

}

Errors:
Code:
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
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry I hadn't seen your question earlier -

Code:
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()
{}
« Last Edit: February 04, 2013, 08:18:33 pm by lloyddean » Logged

Pages: [1]   Go Up
Jump to: