Go Down

Topic: Invalid conversion from byte to byte* (Read 296 times) previous topic - next topic

bobby3605

Hello, I am trying to multiplex a 3x3 led cube. To control it I'm trying to pass an array into a function as an argument.

Here is the code:

Code: [Select]

#include <MemoryFree.h>

byte levelPins[] = {11,12,13};
byte columnPins[] = {2,3,4,5,6,7,8,9,10};
byte maxLevel = 2;
byte maxColumn = 8;

int DELAY = 20; // Delay between off and on states in microseconds

void light(byte level[], byte column[]) {
 
  int sL = sizeof(level);
  int sC = sizeof(column);
 
  int l;
  int c;

  for(l=0;l<=sL;){
    for(c=0;c<=sC;) {
      Serial.println("Level = " + level[l]);
      Serial.println("Column = " + column[c]);
      digitalWrite(levelPins[(level[l])], HIGH);
      digitalWrite(columnPins[(column[c])], HIGH);
      delayMicroseconds(DELAY);
      digitalWrite(levelPins[(level[l])], LOW);
      digitalWrite(columnPins[(column[c])], LOW);
      l++;
      c++;
    }
  }
}
void setup ()
{
  Serial.begin(9600);
}

void loop ()
{
Serial.println(freeMemory());

byte level[0];
byte column[0];

light(level[0,1,0,1,1,2], column[6,6,8,8,7,7]);
}


Here is the error:

Code: [Select]

LedCube.ino: In function 'void loop()':
LedCube:44: error: invalid conversion from 'byte' to 'byte*'
LedCube:44: error: initializing argument 1 of 'void light(byte*, byte*)'
LedCube:44: error: invalid conversion from 'byte' to 'byte*'
LedCube:44: error: initializing argument 2 of 'void light(byte*, byte*)'


If I change

byte level[0]; and byte column[0];

to

byte* level[0]; and byte* column[0];

The program runs, but the leds randomly turn on and off. Then, the leds "save a pattern" and start blinking in a specific pattern.

The serial monitor just outputs random symbols when the leds are blinking randomly. Then the same symbol when they blink in a specific pattern.




bobby3605

#1
May 15, 2017, 05:25 am Last Edit: May 15, 2017, 05:31 am by bobby3605
I was just messing with it a bit more to make the leds brighter and found that if I add this to setup the arduino freezes after about a second. Then in order to upload anything to it, I have to unplug it and plug it back in while the compiler is running, but before the upload starts in order to change the program back to something that doesn't freeze. This also caused the leds to start lighting up in a certain pattern each time.

Code: [Select]

void setup ()
{
  int sL = sizeof(levelPins);
  int sC = sizeof(columnPins);
 
  int p;
  
  for(p=0;p<=sL;p++){
  pinMode(p, OUTPUT);
  }
  for(p=0;p<=sC;p++){
  pinMode(p, OUTPUT);
  }
  Serial.begin(9600); <--- That was already there before.
}

sterretje

In the opening post, 'level' is an array with a size 0.

You can
Code: [Select]
byte level[] = {0,1,0,1,1,2};
// same for column
...

light(level, column);

Just as example, not tested.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

bobby3605

That got rid of the random pattern, but only 2 leds are on and they are visibly flashing. There should be 6 leds on. The ones that are on are 0,6 and 1,6. Also, if I decrease the delay, they get dimmer until it gets to 0 microseconds, then it is very bright. If I completely remove the delayMicroseconds lines, they get very dim.

sterretje

That got rid of the random pattern, but only 2 leds are on and they are visibly flashing. There should be 6 leds on. The ones that are on are 0,6 and 1,6. Also, if I decrease the delay, they get dimmer until it gets to 0 microseconds, then it is very bright. If I completely remove the delayMicroseconds lines, they get very dim.
Post your updated code.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

bobby3605

Code: [Select]
#include <MemoryFree.h>

byte levelPins[] = {11,12,13};
byte columnPins[] = {2,3,4,5,6,7,8,9,10};
byte maxLevel = 2;
byte maxColumn = 8;

int DELAY = 20; // Delay between off and on states in microseconds

void light(byte level[], byte column[]) {
 
  int sL = sizeof(level);
  int sC = sizeof(column);
 
  int l;
  int c;

  for(l=0;l<=sL;){
    for(c=0;c<=sC;) {
      Serial.println("Level = " + level[l]);
      Serial.println("Column = " + column[c]);
      digitalWrite(levelPins[(level[l])], HIGH);
      digitalWrite(columnPins[(column[c])], HIGH);
      delayMicroseconds(DELAY);
      digitalWrite(levelPins[(level[l])], LOW);
      digitalWrite(columnPins[(column[c])], LOW);
      delayMicroseconds(DELAY);
      l++;
      c++;
    }
  }
}
void setup ()
{
  int sL = sizeof(levelPins);
  int sC = sizeof(columnPins);
 
  int p;
 
  for(p=0;p<=sL;p++){
  pinMode(p, OUTPUT);
  }
  for(p=0;p<=sC;p++){
  pinMode(p, OUTPUT);
  }
  Serial.begin(9600);
}

void loop ()
{
Serial.println(freeMemory());

byte level[] = {0,1,0,1,1,2};
byte column[] = {6,6,8,8,7,7};

light(level, column);
}

sterretje

One problem (there might be others) is that you loop too much for both the colums and the levels.

sizeof() returns the size of the array; you use '<=' in the for-loops so the for-loops will iterate one time too many. Change '<=' to '<'

If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

bobby3605

That didn't change anything.
Code: [Select]
#include <MemoryFree.h>

byte levelPins[] = {11,12,13};
byte columnPins[] = {2,3,4,5,6,7,8,9,10};
byte maxLevel = 2;
byte maxColumn = 8;

int DELAY = 20; // Delay between off and on states in microseconds

void light(byte level[], byte column[]) {
 
  int sL = sizeof(level);
  int sC = sizeof(column);
  
  int l;
  int c;

  for(l=0;l<sL;){
    for(c=0;c<sC;) {
      Serial.println("Level = " + level[l]);
      Serial.println("Column = " + column[c]);
      digitalWrite(levelPins[(level[l])], HIGH);
      digitalWrite(columnPins[(column[c])], HIGH);
      delayMicroseconds(DELAY);
      digitalWrite(levelPins[(level[l])], LOW);
      digitalWrite(columnPins[(column[c])], LOW);
      delayMicroseconds(DELAY);
      l++;
      c++;
    }
  }
}
void setup ()
{
  int sL = sizeof(levelPins);
  int sC = sizeof(columnPins);
 
  int p;
  
  for(p=0;p<sL;p++){
  pinMode(p, OUTPUT);
  }
  for(p=0;p<sC;p++){
  pinMode(p, OUTPUT);
  }
  Serial.begin(9600);
}

void loop ()
{
Serial.println(freeMemory());

byte level[] = {0,1,0,1,1,2};
byte column[] = {6,6,8,8,7,7};

light(level, column);
}

larryd

#8
May 16, 2017, 07:03 pm Last Edit: May 16, 2017, 07:04 pm by larryd
Try
for(l=0;l<sL;l++){
and
for(c=0;c<sC;c++) {

remove:
l++;
c++;

Avoid letter variables.


.
No technical PMs.
The last thing you did is where you should start looking.

bobby3605

#9
May 16, 2017, 11:10 pm Last Edit: May 16, 2017, 11:43 pm by bobby3605
I got it working. I used the PWM library to control the flashing. Then, I moved the sizeof(level) and sizeof(column) to the other arrays. For some reason, after the arrays went through the function, they became pointers. sizeof doesn't work well with pointers, so it thought the size of the arrays was 1.

Here is the finished code for anyone interested:
Code: [Select]
#include <PWM.h>

byte levelPins[] = {
  11,12,13};
byte columnPins[] = {
  2,3,4,5,6,7,8,9,10};
byte maxLevel = 2;
byte maxColumn = 8;

int f = 255; // Frequency of PWM in Hz

void light(byte level[], byte column[], int sL, int sC) { 

  for(int l=0;l<sL;){
    for(int c=0;c<sC;) {
      pwmWrite(levelPins[(level[l])], f);
      pwmWrite(columnPins[(column[c])], f);
      pwmWrite(levelPins[(level[l])], 0);
      pwmWrite(columnPins[(column[c])], 0);
      l++;
      c++;
    }
  }
}

void allOn(){
  byte level[] = {
    0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2        };
  byte column[] = {
    0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,6,7,8        };

  int sL = sizeof(level);
  int sC = sizeof(column);

  for(int l=0;l<sL;){
    for(int c=0;c<sC;) {
      pwmWrite(levelPins[(level[l])], f);
      pwmWrite(columnPins[(column[c])], f);
      l++;
      c++;
    }
  }
}

void allOff() {
  byte level[] = {
    0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2        };
  byte column[] = {
    0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,6,7,8        };

  int sL = sizeof(level);
  int sC = sizeof(column);

  for(int l=0;l<sL;){
    for(int c=0;c<sC;) {
      pwmWrite(levelPins[(level[l])], 0);
      pwmWrite(columnPins[(column[c])], 0);
      l++;
      c++;
    }
  }
}

void randomLights(int Delay) {
  pwmWrite(levelPins[random(0, (sizeof(levelPins)))], f);
  pwmWrite(columnPins[random(0, (sizeof(columnPins)))], f);
  delay(random(1,Delay));
  pwmWrite(levelPins[random(0, (sizeof(levelPins)))], 0);
  pwmWrite(columnPins[random(0, (sizeof(columnPins)))], 0);
  delay(random(1,Delay));


}
void setup ()
{
  InitTimersSafe();

  int sL = sizeof(levelPins);
  int sC = sizeof(columnPins);

  for(int p=0;p<sL;p++){
    SetPinFrequencySafe(p, f);
  }
  for(int p=0;p<sC;p++){
    SetPinFrequencySafe(p, f);
  }
  Serial.begin(9600);

}

void loop ()
{
  randomLights(100);
}



}

sterretje

#10
May 17, 2017, 01:49 am Last Edit: May 17, 2017, 01:49 am by sterretje
Of course. Stupid from me not to pick that up.

It does not change to pointers, the parameters ARE pointers.

Congratulations.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up