Go Down

Topic: Question about table (Read 484 times) previous topic - next topic

gfvalvo

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.

Code: [Select]
#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()
{
}

gavron04

#16
Sep 12, 2018, 06:35 pm Last Edit: Sep 12, 2018, 06:37 pm by gavron04
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)

johnwasser

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.


And If I need to turn on for example USB I need to write in void loop:
Code: [Select]
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():

Code: [Select]
   if (digitalRead(USBsw) == LOW)
    {
      test(usb);
    }
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

gavron04

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

gavron04

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

Code: [Select]
//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

johnwasser

Code: [Select]
  pinMode(OPT1sw, INPUT);
  pinMode(OPT2sw, INPUT);
  pinMode(OPT3sw, INPUT);
  pinMode(COAXsw, INPUT);
  pinMode(USBsw, INPUT);
  pinMode(BTsw, INPUT);


You are not using INPUT_PULLUP so you need an external pull-up or pull-down resistor.  How are the switches wired?

All of this code:
Code: [Select]
 //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); 


Can be replaced by:
Code: [Select]
   //RGB//
  analogWrite(redPin, analogRead(potPin_red)/4); 
  analogWrite(greenPin, analogRead(potPin_green)/4); 
  analogWrite(bluePin, analogRead(potPin_blue)/4); 
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

gavron04

About switches wiring - only 100nF between signal and GND. So I will change in code from INPUT to INPUT_PULLUP.

gavron04

Can be replaced by:
Code: [Select]
   //RGB//
  analogWrite(redPin, analogRead(potPin_red)/4);
  analogWrite(greenPin, analogRead(potPin_green)/4);
  analogWrite(bluePin, analogRead(potPin_blue)/4);


After this all LEDS are flickering at low signal (I have 16mhz crystal, external).


About switches - after changing to INPUT_PULLUP situation is the same. All leds are on, and when I press some buttons - nothing changes (led or high/low usb)

sterretje

#23
Sep 19, 2018, 05:39 pm Last Edit: Sep 19, 2018, 05:40 pm by sterretje
You have a PCB; so you should also have a schematic. I suggest that you post it.
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.

gavron04

#24
Sep 19, 2018, 05:56 pm Last Edit: Sep 19, 2018, 07:29 pm by gavron04
Code: [Select]
//LEDy//
#define OPT1led 9
#define OPT2led 10
#define OPT3led 8
#define COAXled 31
#define USBled 14
#define BTled 30

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

#define ISOusb 0

//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;


void setup() {
  // put your setup code here, to run once:

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

    pinMode(ISOusb, OUTPUT);
    digitalWrite(ISOusb, LOW);



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

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

/*
  digitalWrite(OPT1led, HIGH);
    digitalWrite(OPT2led, HIGH);
  digitalWrite(OPT3led, HIGH);
  digitalWrite(COAXled, HIGH);
  digitalWrite(USBled, HIGH);
  digitalWrite(BTled, HIGH);

*/
}

void loop() {
  // put your main code here, to run repeatedly:


  //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);


    //ZRODLA//
  //OPT1//
  if(digitalRead(OPT1sw) == LOW)
  {
  digitalWrite(OPT1led, HIGH);
  digitalWrite(ISOusb, HIGH);
  }

  //OPT2//
  if(digitalRead(OPT2sw) == LOW)
  {
    digitalWrite(OPT2led, LOW);
  }

  //OPT3//
  if(digitalRead(OPT3sw) == LOW)
  {
   digitalWrite(OPT3led, LOW);
  }

  //COAX//
  if(digitalRead(COAXsw) == LOW)
  {
    digitalWrite(COAXled, LOW);
  }

  //USB//
  if(digitalRead(USBsw) == LOW)
  {
    digitalWrite(USBled, LOW);
  }


  //BT//
  if(digitalRead(BTsw) == LOW)
  {
    digitalWrite(BTled, LOW);
      digitalWrite(OPT1led, LOW);
    digitalWrite(ISOusb, LOW);
  }


}


I have tested this code, and works ok. After pressing OPT1 switch, led OPT1 is on, ISOusb is ON, after pressing BT switch, led OPT1 is turned off and ISOusb is turned off.

So I think that problem is in code.


After some fix:


Code: [Select]
//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 = 4;
const byte IPS1Pin = 3;
const byte ISOusbPin = 0;
const byte MUXbtPin = 1;
const byte MUXakPin = 2;


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()

{



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

  //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);


 
  //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 i = 0; i < zrodloCount; i++)
    test(i);


  //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);


  //ZRODLA//
  //OPT1//
  if(digitalRead(OPT1sw) == LOW)
  {
   test(opt1);
   ledsoff();
   digitalWrite(OPT1led, HIGH);
  }

  //OPT2//
  if(digitalRead(OPT2sw) == LOW)
  {
   test(opt2);
     ledsoff();
      digitalWrite(OPT2led, HIGH);
  }

  //OPT3//
  if(digitalRead(OPT3sw) == LOW)
  {
   test(opt3);
     ledsoff();
      digitalWrite(OPT3led, HIGH);
  }

  //COAX//
  if(digitalRead(COAXsw) == LOW)
  {
   test(coax);
     ledsoff();
      digitalWrite(COAXled, HIGH);
  }

  //USB// 
  if(digitalRead(USBsw) == LOW)
  {
   test(usb);
     ledsoff();
      digitalWrite(USBled, HIGH);
  }


  //BT//
  if(digitalRead(BTsw) == LOW)
  {
   test(bt);
   ledsoff();
      digitalWrite(BTled, HIGH);
  }

 
}


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


void ledsoff()
{

        digitalWrite(OPT1led, LOW);
        digitalWrite(OPT2led, LOW);
        digitalWrite(OPT3led, LOW);
        digitalWrite(COAXled, LOW);
        digitalWrite(USBled, LOW);
        digitalWrite(BTled, LOW);

}




buttons and leds works ok. but sources (zrodloEnum,outputEnum) wont work.

Go Up