Pointer Problem

I'm getting garbage data, so I know there is a problem here.

const int numOfChans = 8;
int chanCurr[numOfChans] =    {0, 0, 0, 0, 0, 0, 0, 0};
int chanTarg[numOfChans] =    {0, 0, 0, 0, 0, 0, 0, 0};
int chanSmooth[numOfChans] = {0, 0, 0, 0, 0, 0, 0, 0};

const int q1[numOfChans] =    {36, 100, 255, 0, 0, 0, 0, 0};
const int q2[numOfChans] =    {0, 18, 255, 76, 4, 0, 0, 0};

const int numOfQs = 2;

//I believe this is making an array of int array pointers
int *qLookup[numOfQs];

//I don't understand why these need to be in a function to work
void assign() {
  qLookup[1] = (const int *) &q1;
  qLookup[2] = (const int *) &q2;
}

void setup() {
  Serial.begin(9600);
  assign();
}

void loop() {
  if (Serial.available() > 0) {
    int qNumber = Serial.parseInt();
    Serial.println(qNumber);
    updateChannels(&qLookup[qNumber]); //call the updateChannels() function with the target of item of the array
  }
  //update all the channels (0 will be reserved for speed of cross-fade)
  for (int i = 1; i < numOfChans; i++) {
    chanSmooth[i] = ((chanCurr[i] * 9) + chanTarg[i])/10;
  }

  //Update the lights with Smooth
  //use first channel for delay
  
  for (int i = 1; i < numOfChans; i++) {
    chanCurr[i] = chanSmooth[i];
  }
  
}

void updateChannels(int *q[]) {
  //function should take in a pointer to an int array
  for (int i = 0; i < numOfChans; i++) {
    //print out the values as they are updated (I'm getting garbage here) 
    int currI = *q[i];
    Serial.println(currI);
    chanCurr[i] = *q[i];
  }
}

You have a two element array.
That element does not exist

Because assignments must be inside a function.

Got it, that is an easy fix, I switched those to 0 and 1 instead of 1 and 2.

Still getting garbage.

Can't see your code.

const int numOfChans = 8;
int chanCurr[numOfChans] =    {0, 0, 0, 0, 0, 0, 0, 0};
int chanTarg[numOfChans] =    {0, 0, 0, 0, 0, 0, 0, 0};
int chanSmooth[numOfChans] = {0, 0, 0, 0, 0, 0, 0, 0};

const int q0[numOfChans] =    {0, 0, 0, 0, 0, 0, 0, 0};
const int q1[numOfChans] =    {36, 100, 255, 0, 0, 0, 0, 0};
const int q2[numOfChans] =    {0, 18, 255, 76, 4, 0, 0, 0};

const int numOfQs = 3;

//I believe this is making an array of int array pointers
int *qLookup[numOfQs];

//I don't understand why these need to be in a function to work
void assign() {
  qLookup[0] = (const int *) &q0;
  qLookup[1] = (const int *) &q1;
  qLookup[2] = (const int *) &q2;
}

void setup() {
  Serial.begin(9600);
  assign();
}

void loop() {
  if (Serial.available() > 0) {
    int qNumber = Serial.parseInt();
    Serial.println(qNumber);
    updateChannels(&qLookup[qNumber]); //call the updateChannels() function with the target of item of the array
  }
  //update all the channels (0 will be reserved for speed of cross-fade)
  for (int i = 1; i < numOfChans; i++) {
    chanSmooth[i] = ((chanCurr[i] * 9) + chanTarg[i])/10;
  }

  //Update the lights with Smooth
  //use first channel for delay
  
  for (int i = 1; i < numOfChans; i++) {
    chanCurr[i] = chanSmooth[i];
  }
  
}

void updateChannels(int *q[]) {
  //function should take in a pointer to an int array
  for (int i = 0; i < numOfChans; i++) {
    //print out the values as they are updated (I'm getting garbage here) 
    int currI = *q[i];
    Serial.println(currI);
    chanCurr[i] = *q[i];
  }
}

Could that be part of the problem?

I'm not sure why this isn't a 2D array.

2D Array got me there, and simplified everything.

const int numOfChans = 8;
const int numOfQs = 3;

int chanCurr[numOfChans] =    {0, 0, 0, 0, 0, 0, 0, 0};
int chanTarg[numOfChans] =    {0, 0, 0, 0, 0, 0, 0, 0};
int chanSmooth[numOfChans] = {0, 0, 0, 0, 0, 0, 0, 0};

const int q[numOfQs][numOfChans] = {{0, 0, 0, 0, 0, 0, 0, 0},
                                    {36, 100, 255, 0, 0, 0, 0, 0},
                                    {0, 18, 255, 76, 4, 0, 0, 0}};


void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    int qNumber = Serial.parseInt();
    Serial.println(qNumber);
}

void updateChannels(int qNumber) {
timeDelay = chanCurr[0];  
for (int i = 1; i < numOfChans; i++) {
    //print out the values as they are updated
    chanCurr[i] = q[qNumber][i];
    Serial.println(chanCurr[i]);
  }
}

Again?

I'm not really sure why this isn't a 2D array.
It sure looks like it should be

I'm using the first member of the array to hold the time delay, so I'm ignoring it to keep the numbers consistent.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.