Loading...
Pages: [1]   Go Down
Author Topic: Please explain why Pin13 and Serial.write misbehaving  (Read 233 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 107
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
Edison Member
*
Karma: 47
Posts: 1383
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

Offline Offline
Full Member
***
Karma: 0
Posts: 107
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

Milton Keynes UK
Offline Offline
Tesla Member
***
Karma: 88
Posts: 6287
-
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

Queens, New York
Offline Offline
Edison Member
*
Karma: 27
Posts: 1539
"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

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Offline Offline
Full Member
***
Karma: 0
Posts: 107
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: 21
Posts: 697
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: 107
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: 21
Posts: 697
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: 107
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: 21
Posts: 697
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
Print
 
Jump to: