Question about table

Hello. Its possible to make a below table in Arduino? In "C" all works ok, but in Arduino I have problem with compiling. Its says that "zrodla" does not name a type

enum {opt1, opt2, opt3, coax, usb, bt};

byte zrodla[8][8]={

//OPT1//
zrodla[opt1][0]=0; //IPS 0
zrodla[opt1][1]=0; //IPS 1
zrodla[opt1][2]=0; //izolator usb
zrodla[opt1][3]=0; //mux bt
zrodla[opt1][4]=1; //mux ak

//OPT2//
zrodla[opt2][0]=1; //IPS 0
zrodla[opt2][1]=0; //IPS 1
zrodla[opt2][2]=0; //izolator usb
zrodla[opt2][3]=0; //mux bt
zrodla[opt2][4]=1; //mux ak

//OPT3//
zrodla[opt3][0]=0; //IPS 0
zrodla[opt3][1]=1; //IPS 1
zrodla[opt3][2]=0; //izolator usb
zrodla[opt3][3]=0; //mux bt
zrodla[opt3][4]=1; //mux ak

//COAX//
zrodla[coax][0]=1; //IPS 0
zrodla[coax][1]=1; //IPS 1
zrodla[coax][2]=0; //izolator usb
zrodla[coax][3]=0; //mux bt
zrodla[coax][4]=1; //mux ak

//USB//
zrodla[usb][0]=0; //IPS 0
zrodla[usb][1]=0; //IPS 1
zrodla[usb][2]=1; //izolator usb
zrodla[usb][3]=0; //mux bt
zrodla[usb][4]=0; //mux ak

//BT//
zrodla[bt][0]=0; //IPS 0
zrodla[bt][1]=0; //IPS 1
zrodla[bt][2]=0; //izolator usb
zrodla[bt][3]=1; //mux bt
zrodla[bt][4]=0; //mux ak
}

What you have is a 2-dimension array and you are not initializing it correctly. All you need to do is something like this

byte zrodla[4][4]={
                           {0,0,0,1},
                           {0,0,1,0},
                           {0,1,0,0},
                           {1,0,0,0}
                    }

To do it the way you are doing it you need to put the code in setup()

...R

Robin2:
What you have is a 2-dimension array and you are not initializing it correctly. All you need to do is ....

.... post a complete program so we can see where this snippet fits in.

enum in C++, the language of Arduino, requires a type declaration.

Perehama:
enum in C++, the language of Arduino, requires a type declaration.

Do you mean data type or ID for the enum definition?

gfvalvo:
.... post a complete program so we can see where this snippet fits in.

enum {opt1, opt2, opt3, coax, usb, bt};

byte zrodla[25][25]
{
//OPT1//
zrodla[opt1][0]=0; //IPS 0
zrodla[opt1][1]=0; //IPS 1
zrodla[opt1][2]=0; //izolator usb
zrodla[opt1][3]=0; //mux bt
zrodla[opt1][4]=1; //mux ak

//OPT2//
zrodla[opt2][0]=1; //IPS 0
zrodla[opt2][1]=0; //IPS 1
zrodla[opt2][2]=0; //izolator usb
zrodla[opt2][3]=0; //mux bt
zrodla[opt2][4]=1; //mux ak

//OPT3//
zrodla[opt3][0]=0; //IPS 0
zrodla[opt3][1]=1; //IPS 1
zrodla[opt3][2]=0; //izolator usb
zrodla[opt3][3]=0; //mux bt
zrodla[opt3][4]=1; //mux ak

//COAX//
zrodla[coax][0]=1; //IPS 0
zrodla[coax][1]=1; //IPS 1
zrodla[coax][2]=0; //izolator usb
zrodla[coax][3]=0; //mux bt
zrodla[coax][4]=1; //mux ak

//USB//
zrodla[usb][0]=0; //IPS 0
zrodla[usb][1]=0; //IPS 1
zrodla[usb][2]=1; //izolator usb
zrodla[usb][3]=0; //mux bt
zrodla[usb][4]=0; //mux ak

//BT//
zrodla[bt][0]=0; //IPS 0
zrodla[bt][1]=0; //IPS 1
zrodla[bt][2]=0; //izolator usb
zrodla[bt][3]=1; //mux bt
zrodla[bt][4]=0; //mux ak
}



void test(byte zrodlo)
{
   digitalWrite(IPS0, zrodla[zrodlo][0]);
   digitalWrite(IPS1, zrodla[zrodlo][1]);
   digitalWrite(ISOusb, zrodla[zrodlo][2]);
   digitalWrite(MUXbt, zrodla[zrodlo][3]);   
   digitalWrite(MUXak, zrodla[zrodlo][4]);
}

and with this I would like to for example write "test(opt1);" , which will drive some outputs.

gavron04:

enum {opt1, opt2, opt3, coax, usb, bt};

byte zrodla[25][25];

enum sources : byte {opt1, opt2, opt3, coax, usb, bt};
sources mySources[25][25]

econjack:
Do you mean data type or ID for the enum definition?

"Enums:

In C, one of the following two forms is required:
enum color {
Red, Yellow, Green
};
enum color myColor;
or:
typedef enum {
Red, Yellow, Green
} color;
color yourColor;
In C++, an instance of the enum color can be defined and declared as
follows without a typedef:
enum color {
Red, Yellow, Green
};
color someColor;
In C, an enum is basically a way to alias names to integers. This does
not lead to improved type checking. In C++, an enum defines an actual
type, which results in strong type checking. So:
someColor = 1;
will generate a warning in C++ but not in C." - Some Differences Between C and C++ by Steve Jacobson

Perehama:

enum sources : byte {opt1, opt2, opt3, coax, usb, bt};

sources mySources[25][25]

So now I need to change:

//OPT1//
zrodla[opt1][0]=0; //IPS 0
zrodla[opt1][1]=0; //IPS 1
zrodla[opt1][2]=0; //izolator usb
zrodla[opt1][3]=0; //mux bt
zrodla[opt1][4]=1; //mux ak

to:

//OPT1//
mySources[opt1][0]=0; //IPS 0
mySources[opt1][1]=0; //IPS 1
mySources[opt1][2]=0; //izolator usb
mySources[opt1][3]=0; //mux bt
mySources[opt1][4]=1; //mux ak

right?

gavron04:
So now I need to change:

//OPT1//

zrodla[opt1][0]=0; //IPS 0
zrodla[opt1][1]=0; //IPS 1
zrodla[opt1][2]=0; //izolator usb
zrodla[opt1][3]=0; //mux bt
zrodla[opt1][4]=1; //mux ak




to:



//OPT1//
mySources[opt1][0]=0; //IPS 0
mySources[opt1][1]=0; //IPS 1
mySources[opt1][2]=0; //izolator usb
mySources[opt1][3]=0; //mux bt
mySources[opt1][4]=1; //mux ak




right?

Did you do it? Did it work for you?

You can initialize the array at run time but you have to move the assignment operations into a function:

enum {opt1, opt2, opt3, coax, usb, bt, enumCount};


boolean zrodla[enumCount][5];


void initializeArray()
{
  //OPT1//
  zrodla[opt1][0] = 0; //IPS 0
  zrodla[opt1][1] = 0; //IPS 1
  zrodla[opt1][2] = 0; //izolator usb
  zrodla[opt1][3] = 0; //mux bt
  zrodla[opt1][4] = 1; //mux ak


  //OPT2//
  zrodla[opt2][0] = 1; //IPS 0
  zrodla[opt2][1] = 0; //IPS 1
  zrodla[opt2][2] = 0; //izolator usb
  zrodla[opt2][3] = 0; //mux bt
  zrodla[opt2][4] = 1; //mux ak


  //OPT3//
  zrodla[opt3][0] = 0; //IPS 0
  zrodla[opt3][1] = 1; //IPS 1
  zrodla[opt3][2] = 0; //izolator usb
  zrodla[opt3][3] = 0; //mux bt
  zrodla[opt3][4] = 1; //mux ak


  //COAX//
  zrodla[coax][0] = 1; //IPS 0
  zrodla[coax][1] = 1; //IPS 1
  zrodla[coax][2] = 0; //izolator usb
  zrodla[coax][3] = 0; //mux bt
  zrodla[coax][4] = 1; //mux ak


  //USB//
  zrodla[usb][0] = 0; //IPS 0
  zrodla[usb][1] = 0; //IPS 1
  zrodla[usb][2] = 1; //izolator usb
  zrodla[usb][3] = 0; //mux bt
  zrodla[usb][4] = 0; //mux ak


  //BT//
  zrodla[bt][0] = 0; //IPS 0
  zrodla[bt][1] = 0; //IPS 1
  zrodla[bt][2] = 0; //izolator usb
  zrodla[bt][3] = 1; //mux bt
  zrodla[bt][4] = 0; //mux ak
}


void setup()
{
  initializeArray();
}


void loop() {}

Perehama:
Did you do it? Did it work for you?

No, it didnt.

"invalid conversion from 'int' to 'sources' [-fpermissive]

gavron04:
No, it didnt.

Post your latest attempt. Make sure it's a complete Arduino program including setup() and loop().

#define MUXak 2
#define MUXbt 1

#define ISOusb 0

#define IPS0 4
#define IPS1 3


//TABLICA//
enum sources : byte {opt1, opt2, opt3, coax, usb, bt};
sources mySources[25][25]



  //OPT1//
  mySources[opt1][0] = 0; //IPS 0
  mySources[opt1][1] = 0; //IPS 1
  mySources[opt1][2] = 0; //izolator usb
  mySources[opt1][3] = 0; //mux bt
  mySources[opt1][4] = 1; //mux ak


  //OPT2//
  mySources[opt2][0] = 1; //IPS 0
  mySources[opt2][1] = 0; //IPS 1
  mySources[opt2][2] = 0; //izolator usb
  mySources[opt2][3] = 0; //mux bt
  mySources[opt2][4] = 1; //mux ak


  //OPT3//
  mySources[opt3][0] = 0; //IPS 0
  mySources[opt3][1] = 1; //IPS 1
  mySources[opt3][2] = 0; //izolator usb
  mySources[opt3][3] = 0; //mux bt
  mySources[opt3][4] = 1; //mux ak


  //COAX//
  mySources[coax][0] = 1; //IPS 0
  mySources[coax][1] = 1; //IPS 1
  mySources[coax][2] = 0; //izolator usb
  mySources[coax][3] = 0; //mux bt
  mySources[coax][4] = 1; //mux ak


  //USB//
  mySources[usb][0] = 0; //IPS 0
  mySources[usb][1] = 0; //IPS 1
  mySources[usb][2] = 1; //izolator usb
  mySources[usb][3] = 0; //mux bt
  mySources[usb][4] = 0; //mux ak


  //BT//
  mySources[bt][0] = 0; //IPS 0
  mySources[bt][1] = 0; //IPS 1
  mySources[bt][2] = 0; //izolator usb
  mySources[bt][3] = 1; //mux bt
  mySources[bt][4] = 0; //mux ak




void setup() 

{
 pinMode(MUXak, OUTPUT);
 pinMode(MUXbt, OUTPUT);
  pinMode(ISOusb, OUTPUT);
  pinMode(IPS0, OUTPUT);
  pinMode(IPS1, OUTPUT);
}


void test(byte zrodlo)
{
   digitalWrite(IPS0, mySources[zrodlo][0]);
   digitalWrite(IPS1, mySources[zrodlo][1]);
   digitalWrite(ISOusb, mySources[zrodlo][2]);
   digitalWrite(MUXbt, mySources[zrodlo][3]);   
   digitalWrite(MUXak, mySources[zrodlo][4]);
}


void loop() 
{
}

Unless you need to calculate the array contents at run time it is MUCH cleaner to initialize them at compile time:

enum zrodloEnum {opt1, opt2, opt3, coax, usb, bt, zrodloCount};


enum outputEnum {IPS0, IPS1, ISOusb, MUXbt, MUXak, outputPinCount};


const byte IPS0Pin = 2;
const byte IPS1Pin = 3;
const byte ISOusbPin = 5;
const byte MUXbtPin = 7;
const byte MUXakPin = 8;


const byte OutputPins[outputPinCount] = {IPS0Pin, IPS1Pin, ISOusbPin, MUXbtPin, MUXakPin};


const boolean zrodla[zrodloCount][outputPinCount] =
{
  {/*opt1*/ 0, 0, 0, 0, 1},
  {/*opt2*/ 1, 0, 0, 0, 1},
  {/*opt3*/ 0, 1, 0, 0, 1},
  {/*coax*/ 1, 1, 0, 0, 1},
  {/*usb*/  0, 0, 1, 0, 0},
  {/*bt*/   0, 0, 0, 1, 0}
};




void setup()
{
}


void loop()
{
  for (byte i = 0; i < zrodloCount; i++)
    test(i);
}


void test(const byte zrodlo)
{
  for (byte i = 0; i < outputPinCount; i++)
    digitalWrite(OutputPins[i], zrodla[zrodlo][i]);
}

You’ve already been told several times that you can’t initialize an array in that manner outside of a function. Your initialization statements are not within any function.

Second, I don’t understand why mySources is an array of sources (which is an enum type). From the values assigned, it looks to me like mySources should be an array of bytes.

So, correcting those things produces something that compiles (see below). Whether or not it actually does what you think / want is another matter as it seems kind of odd.

#define MUXak 2
#define MUXbt 1

#define ISOusb 0

#define IPS0 4
#define IPS1 3

//TABLICA//
enum sources : byte {opt1 = 0, opt2, opt3, coax, usb, bt};
byte mySources[25][25];

void init() {
  //OPT1//
  mySources[opt1][0] = 0; //IPS 0
  mySources[opt1][1] = 0; //IPS 1
  mySources[opt1][2] = 0; //izolator usb
  mySources[opt1][3] = 0; //mux bt
  mySources[opt1][4] = 1; //mux ak

  //OPT2//
  mySources[opt2][0] = 1; //IPS 0
  mySources[opt2][1] = 0; //IPS 1
  mySources[opt2][2] = 0; //izolator usb
  mySources[opt2][3] = 0; //mux bt
  mySources[opt2][4] = 1; //mux ak

  //OPT3//
  mySources[opt3][0] = 0; //IPS 0
  mySources[opt3][1] = 1; //IPS 1
  mySources[opt3][2] = 0; //izolator usb
  mySources[opt3][3] = 0; //mux bt
  mySources[opt3][4] = 1; //mux ak

  //COAX//
  mySources[coax][0] = 1; //IPS 0
  mySources[coax][1] = 1; //IPS 1
  mySources[coax][2] = 0; //izolator usb
  mySources[coax][3] = 0; //mux bt
  mySources[coax][4] = 1; //mux ak

  //USB//
  mySources[usb][0] = 0; //IPS 0
  mySources[usb][1] = 0; //IPS 1
  mySources[usb][2] = 1; //izolator usb
  mySources[usb][3] = 0; //mux bt
  mySources[usb][4] = 0; //mux ak

  //BT//
  mySources[bt][0] = 0; //IPS 0
  mySources[bt][1] = 0; //IPS 1
  mySources[bt][2] = 0; //izolator usb
  mySources[bt][3] = 1; //mux bt
  mySources[bt][4] = 0; //mux ak
}

void setup()
{
  init();
  pinMode(MUXak, OUTPUT);
  pinMode(MUXbt, OUTPUT);
  pinMode(ISOusb, OUTPUT);
  pinMode(IPS0, OUTPUT);
  pinMode(IPS1, OUTPUT);
}

void test(byte zrodlo)
{
  digitalWrite(IPS0, mySources[zrodlo][0]);
  digitalWrite(IPS1, mySources[zrodlo][1]);
  digitalWrite(ISOusb, mySources[zrodlo][2]);
  digitalWrite(MUXbt, mySources[zrodlo][3]);
  digitalWrite(MUXak, mySources[zrodlo][4]);
}

void loop()
{
}

johnwasser

So now I dont need to declare in "void setup" all pins as OUTPUT?

And If I need to turn on for example USB I need to write in void loop:

if(analogRead(USBsw) == LOW)
{
zrodlo[usb]
}

(USBsw - tact switch)

gavron04:
johnwasser
So now I dont need to declare in "void setup" all pins as OUTPUT?

Yes, you do. And you should set pinMode() on your input pins, too.

gavron04:
And If I need to turn on for example USB I need to write in void loop:

if(analogRead(USBsw) == LOW)

{
zrodlo[usb]
}

You can use A0 through A5 as digital pins. Set pinMode() to INPUT_PULLUP. Just referenceing an array element does nothing. You should call a function that sets the output pins. You already have one, named test():

    if (digitalRead(USBsw) == LOW)
    {
      test(usb);
    }

Thanks :slight_smile: I will report if all works after tests.

Ok. So I have soldered my PCB. After tests buttons and changing inputs wont work. Here is code:

//IR//
#include <IRremote.h>
#define irPin 11  
IRrecv irrecv(irPin);
decode_results results;

//TABLICA LEDy I PRZYCISKI//
byte leds [8] = {9,10,8,31,14,30};
byte inputs [8] = {18,19,20,21,22,23};
byte i,j;
byte temp=0;


//PRZYCISKI//
#define OPT1sw 18
#define OPT2sw 19
#define OPT3sw 20
#define COAXsw 21
#define USBsw 22
#define BTsw 23

//LEDy//
#define OPT1led 9
#define OPT2led 10
#define OPT3led 8
#define COAXled 31
#define USBled 14
#define BTled 30



//RGB//
int redPin = 12;
int greenPin = 13;
int bluePin = 15;
//RGB//
int potPin_red = A7;
int potPin_green = A6;
int potPin_blue = A5;
//RGB//
int readValue_red;
int readValue_green;
int readValue_blue;
//RGB//
int writeValue_red;
int writeValue_green;
int writeValue_blue;



//TABLICA//
enum zrodloEnum {opt1, opt2, opt3, coax, usb, bt, zrodloCount};


enum outputEnum {IPS0, IPS1, ISOusb, MUXbt, MUXak, outputPinCount};


const byte IPS0Pin = 2;
const byte IPS1Pin = 3;
const byte ISOusbPin = 5;
const byte MUXbtPin = 7;
const byte MUXakPin = 8;


const byte OutputPins[outputPinCount] = {IPS0Pin, IPS1Pin, ISOusbPin, MUXbtPin, MUXakPin};


const boolean zrodla[zrodloCount][outputPinCount] =
{
  {/*opt1*/ 0, 0, 0, 0, 1},
  {/*opt2*/ 1, 0, 0, 0, 1},
  {/*opt3*/ 0, 1, 0, 0, 1},
  {/*coax*/ 1, 1, 0, 0, 1},
  {/*usb*/  0, 0, 1, 0, 0},
  {/*bt*/   0, 0, 0, 1, 0}
};

//MUXy//
#define MUXak 2
#define MUXbt 1

//IZOLATOR//
#define ISOusb 0

//AK4118//
#define IPS0 4
#define IPS1 3

void setup() 

{



  //PRZYCISKI//
  pinMode(OPT1sw, INPUT);
  pinMode(OPT2sw, INPUT);
  pinMode(OPT3sw, INPUT);
  pinMode(COAXsw, INPUT);
  pinMode(USBsw, INPUT);
  pinMode(BTsw, INPUT);  

  //LEDy//
  pinMode(OPT1led, OUTPUT);
  pinMode(OPT2led, OUTPUT);
  pinMode(OPT3led, OUTPUT);
  pinMode(COAXled, OUTPUT);
  pinMode(USBled, OUTPUT);
  pinMode(BTled, OUTPUT);

  //MUXy//
  pinMode(MUXak, OUTPUT);
  pinMode(MUXbt, OUTPUT);

  //IZOLATOR//
  pinMode(ISOusb, OUTPUT);

  //AK4118//
  pinMode(IPS0, OUTPUT);
  pinMode(IPS1, OUTPUT);

  //PIERWSZE ZRODLO//
  digitalWrite(ISOusb, HIGH);
  digitalWrite(USBled, HIGH);


  //WYLACZENIE POZOSTALYCH ZRODEL//
  digitalWrite(MUXak, LOW);
  digitalWrite(MUXbt, LOW);

  
  //RGB//
  pinMode(potPin_red, INPUT); 
  pinMode(potPin_green, INPUT); 
  pinMode(potPin_blue, INPUT); 
  //RGB//
  pinMode(redPin,OUTPUT); 
  pinMode(bluePin,OUTPUT); 
  pinMode(greenPin, OUTPUT); 


}



void loop() 
{
  //TABELA//
  for (byte k = 0; k < zrodloCount; k++)
    test(k);


  //RGB//
  readValue_red = analogRead(potPin_red); 
  readValue_green = analogRead(potPin_green); 
  readValue_blue = analogRead(potPin_blue); 
  //RGB//
  writeValue_red = (255./1023.)*readValue_red; 
  writeValue_green = (255./1023.)*readValue_green; 
  writeValue_blue = (255./1023.)*readValue_blue;
  //RGB//
  analogWrite(redPin,writeValue_red); 
  analogWrite(greenPin,writeValue_green); 
  analogWrite(bluePin,writeValue_blue); 



  //PRZYCISKI//
  for(i=0;i<6;i++)
  {
  if(digitalRead(inputs[i]))
     {
     temp=0;
     bitSet(temp,i);
     }
  }

  for(j=0;j<6;j++)
  {
  if(bitSet(temp,j))
  digitalWrite(leds[j],HIGH);
  else
  digitalWrite(leds[j],LOW);
  }


  //ZRODLA//
  //OPT1//
  if(digitalRead(OPT1sw) == LOW)
  {
   test(opt1);
  }

  //OPT2//
  if(digitalRead(OPT2sw) == LOW)
  {
   test(opt2);
  }

  //OPT3//
  if(digitalRead(OPT3sw) == LOW)
  {
   test(opt3);
  }

  //COAX//
  if(digitalRead(COAXsw) == LOW)
  {
   test(coax);
  }

  //USB//
  if(digitalRead(USBsw) == LOW)
  {
   test(usb);
  } 


  //BT//
  if(digitalRead(BTsw) == LOW)
  { 
   test(bt);
  } 

  
}

void test(const byte zrodlo)
{
  for (byte k = 0; k < outputPinCount; k++)
    digitalWrite(OutputPins[k], zrodla[zrodlo][k]);
}

Maybe I should put switch and leds to enum with zrodlo/output? After power ON supply, all leds are on, and pressing keys nothing change.

Regards