Binary strings and int arrays in cplusplus

Hi,
Am doing my first cplusplus varient of an arduino program (mostly necessitated by the complexity of the project), and am running into a kind of stupid problem. My knowledge of C++ is mostly derived from openframeworks and is fairly rudimentary, so apologies if this is overly simple.

Basically the problem is that the binary output of the seg array is working in the arduino native version, but outputting random strings in the c++ version, and I have no idea why. I’m fairly certain it’s either a type or pointing error, but am uncertain how to solve. Would hugely appreciate some advice.


So the following is the test program that DOES work to drive a 7 segment LED through an HEF4794 chip. It counts up from 0 to 9 and works perfectly (derived from some earlier examples).

int seg[10] = {
 0b11111100,
 0b01100000,
 0b11011010,
 0b11110010,
 0b01100110,
 0b10110110,
 0b10111110,
 0b11100000,
 0b11111110,
 0b11100110 
};

int data = 3;
int strob = 4;
int clock = 2;
int oe = 5;
int count = 0;
int dato = 0;
int ack = 5;

void setup()
{
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(strob, OUTPUT);
pinMode(oe, OUTPUT);
pinMode(ack, OUTPUT);
digitalWrite(oe, LOW);
}

void loop()
{
digitalWrite(oe, HIGH);
dato = 1;
for (int i = 0 ; i <= 9 ; i++){ 


  dato = seg[i];
shiftOut(data, clock, LSBFIRST, dato);
digitalWrite(oe, LOW); 
digitalWrite(strob, HIGH);
digitalWrite(oe, HIGH); 
digitalWrite(strob, LOW); 
delay(550); // waits for a moment
digitalWrite(oe, LOW);
}

The following is the class I’m basing on it, but am getting gibberish (output to follow)

(display.h)

//http://www.arduino.cc/en/Tutorial/LEDDriver

//Using HEF4794

#ifndef Display_h
#define Display_h

#include "WProgram.h"
#include "WConstants.h"


class Display
{
    int *seg;
  public:
    Display(int _data, int _clock, int _oe, int _strob); /* {
        seg = new int[11];
      };
    ~Display() { delete [] seg; } */
    int data;
    int clock;
    int oe;
    int count;
    int dato;
    int strob;
    
    void drawNum(int n);
    void inTunnel();
    void update( int _count);
};

#endif

(display.cpp)

#include "WProgram.h"
#include "Display.h"

static int seg[11] = {
    0b11111100, //0
    0b01100000, //1
    0b11011010, //2
    0b11110010, //3
    0b01100110, //4
    0b10110110, //5
    0b10111110, //6
    0b11100000, //7
    0b11111110, //8
    0b11100110,  //9
    0b10011110 // E for Error
  };

Display::Display(int _data, int _clock, int _oe, int _strob) {
 
  
  data = _data;
  clock = _clock;
  oe = _oe;
  strob = _strob;

  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(strob, OUTPUT);
  pinMode(oe, OUTPUT);
  digitalWrite(oe, LOW);

  /*
     //FROM TEST
   data = 3;
   strob = 4;
   clock = 2;
   oe = 5;
   */

  count = 0;
  dato = 0;

}

void Display::drawNum(int n) {
  
  digitalWrite(oe, HIGH);
  dato = seg[n];
  Serial.print( n );
  Serial.print( " to bin " );
  Serial.println( dato );
  shiftOut(data, clock, LSBFIRST, dato);
  //shiftOut(data, clock, LSBFIRST, dato);
  digitalWrite(oe, LOW); 
  digitalWrite(strob, HIGH);
  digitalWrite(oe, HIGH); 
  digitalWrite(strob, LOW); 
 // delay(550); // waits for a moment
  digitalWrite(oe, LOW);
  
}

void Display::inTunnel() {
  for (int i = 16 ; i > 0 ; i--){
    shiftOut(data, clock, LSBFIRST, dato>>8);
    shiftOut(data, clock, LSBFIRST, dato);
    dato = unsigned (dato)>>1;
    digitalWrite(oe, LOW);
    digitalWrite(strob, HIGH);
    digitalWrite(oe, HIGH);
    digitalWrite(strob, LOW);
    delay(50); 
    digitalWrite(oe, LOW);
  }
}
  
  
void Display::update( int _count ) {
  int count = _count;
  drawNum(count);
}

and the output when manipulated by the buttons that change the internal count.

8 to bin 318
Count: 9 (Going up)
9 to bin 8194
Count: 10 (Going up)
10 to bin 139
Count: 11 (Going up)
11 to bin 1
Count: 12 (Going up)
12 to bin 24
Count: 13 (Going up)
13 to bin 318
dato = seg[n];
class Display
{
    int *seg;

What AWOL is asking, a bit cryptically, is where does seg get assigned a value? It is defined as a pointer, but never pointed to anything.

Hey, cryptic is good - it makes you think.

Sorry, that's the output from trying a couple of things. The broader question is:

Why does an array of integers in the arduino program behave differently in the C++ variant? Is the arduino interpretor overriding the native types?

Why does an array of integers in the arduino program behave differently in the C++ variant?

The behaviour is different because the arrays are different. In drawNum, the "seg" referred to is the one declared in your class, which you haven't initialised. (the Arduino doesn't have an interpreter; the code is compiled)