Go Down

Topic: If statement doesn't work for me... (Read 711 times) previous topic - next topic

Y2K986

Jan 21, 2013, 02:25 am Last Edit: Jan 21, 2013, 02:36 am by Y2K986 Reason: 1
Hi. I'm confused so you can probably help me. I made a 32x8 LED board and started working with it.
Code: [Select]
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
//Pin connected to DS of 74HC595
int dataPin = 11;

int blok[8][40];
int counter=0;

int Aca[8][40] ={
{1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0,1,1,0,0,1,1,1,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0},
{0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,0}
};

int Je[8][40] ={
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0,0,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,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0},
{0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1,0,0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0}
};

void loop(){
{
 //count up routine
     if (counter>=0 && counter <80)
     {
       Print ( Aca );
     }
     if (counter>=80 && counter < 160)
     {
       Print ( Je );
     }
   /*  if (counter>=160 && counter < 240)
     {
       Print ( Car );
     }  */
     if (counter >= 240)   counter=0;
     counter++;
 }
}
 
void Print ( int blok[8][40]){
 for (int i = 0; i < 8; i++) {
       digitalWrite(latchPin, 0);
       for (int j = 0; j< 40; j++){
         
         shiftOut(dataPin, clockPin, blok[i][j]);

       }
       digitalWrite(latchPin, 1);
 }
}
 
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
 // This shifts 8 bits out MSB first,
 //on the rising edge of the clock,
 //clock idles low

//internal function setup
 int i=0;
 int pinState;
 pinMode(myClockPin, OUTPUT);
 pinMode(myDataPin, OUTPUT);

//clear everything out just in case to
//prepare shift register for bit shifting
 digitalWrite(myDataPin, 0);
 digitalWrite(myClockPin, 0);

 //for each bit in the byte myDataOut?
 //NOTICE THAT WE ARE COUNTING DOWN in our for loop
 //This means that %00000001 or "1" will go through such
 //that it will be pin Q0 that lights.
 pinState = myDataOut;

   //Sets the pin to HIGH or LOW depending on pinState
   digitalWrite(myDataPin, pinState);
   //register shifts bits on upstroke of clock pin  
   digitalWrite(myClockPin, 1);
   //zero the data pin after shift to prevent bleed through
   digitalWrite(myDataPin, 0);

 //stop shifting
 digitalWrite(myClockPin, 0);
}


Problem is when I want to print 2 matrices, one after another, it all works fine and I get " Aca Je" on my LED board.
Code: [Select]
void loop(){
{
 //count up routine
     if (counter>=0 && counter <80)
     {
       Print ( Aca );
     }
     if (counter>=80 && counter < 160)
     {
       Print ( Je );
     }
     if (counter >= 160)   counter=0;
     counter++;
 }
}


But when I want to print the 3. one nothing happens, not even a start. Can someone help?
Code: [Select]
void loop(){
{
 //count up routine
     if (counter>=0 && counter <80)
     {
       Print ( Aca );
     }
     if (counter>=80 && counter < 160)
     {
       Print ( Je );
     }
     if (counter>=160 && counter < 240)
     {
       Print ( Car );
     }  
     if (counter >= 240)   counter=0;
     counter++;
 }
}

HazardsMind

#1
Jan 21, 2013, 02:40 am Last Edit: Jan 21, 2013, 02:42 am by HazardsMind Reason: 1
No multi-posting!

What is the counter after the second print? Does it go past 160?
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Jack Christensen

#2
Jan 21, 2013, 02:46 am Last Edit: Jan 21, 2013, 02:55 am by Jack Christensen Reason: 1
Strong possibility you're out of SRAM. Three 8x40 integer arrays require 1920 bytes. If they're only going to hold values of 1 or 0, use the byte data type instead and cut the SRAM requirement in half. Better yet, pack 8 bits into a byte, then each array would only require 40 bytes.

Edit: I see there is a fourth 8x40 array as well. With an ATmega328-based board (Uno, etc.) that's definitely over the 2048 bytes of available SRAM, and all of it is not available to a sketch because the Arduino core uses some. Last I checked, with a minimal sketch, about 1800 bytes or so were available IIRC.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Y2K986


No multi-posting!

What is the counter after the second print? Does it go past 160?


It goes back to 0, so the whole process is repeated over and over. When trying to print third one, nothing happens.


Strong possibility you're out of SRAM. Three 8x40 integer arrays require 1920 bytes. If they're only going to hold values of 1 or 0, use the byte data type instead and cut the SRAM requirement in half. Better yet, pack 8 bits into a byte, then each array would only require 40 bytes.

Good idea. Will try!

Y2K986

Thank you for your help. I changed the code to this:
Code: [Select]
int Aca[8][5] ={
{0b10000000,0b00001111,0b10000111,0b11000011,0b11100000},
{0b01000000,0b00011111,0b11001111,0b11100111,0b11110000},
{0b00100000,0b00011000,0b11001100,0b00000110,0b00110000},
{0b00010000,0b00011000,0b11001100,0b00000110,0b00110000},
{0b00001000,0b00011111,0b11001100,0b00000111,0b11110000},
{0b00000100,0b00011111,0b11001100,0b00000111,0b11110000},
{0b00000010,0b00011000,0b11001111,0b11100110,0b00110000},
{0b00000001,0b00011000,0b11000111,0b11000110,0b00110000}
};

int Je[8][5] ={
{0b10000000,0b00000000,0b11111110,0b01111111,0b00000000},
{0b01000000,0b00000000,0b11111110,0b01111111,0b00000000},
{0b00100000,0b00000000,0b00000110,0b01100000,0b00000000},
{0b00010000,0b00000000,0b00000110,0b01111111,0b00000000},
{0b00001000,0b00000000,0b00000110,0b01111111,0b00000000},
{0b00000100,0b00000000,0b00000110,0b01100000,0b00000000},
{0b00000010,0b00000000,0b11111110,0b01111111,0b00000000},
{0b00000001,0b00000000,0b01111100,0b01111111,0b00000000}
};

int Car[8][5] ={
{0b10000000,0b00001111,0b10000111,0b11000111,0b11100000},
{0b01000000,0b00011111,0b11001111,0b11100111,0b11110000},
{0b00100000,0b00011000,0b00001100,0b01100110,0b00110000},
{0b00010000,0b00011000,0b00001100,0b01100110,0b00110000},
{0b00001000,0b00011000,0b00001111,0b11100111,0b11100000},
{0b00000100,0b00011000,0b00001111,0b11100111,0b11110000},
{0b00000010,0b00011111,0b11001100,0b01100110,0b00110000},
{0b00000001,0b00001111,0b10001100,0b01100110,0b00110000}
};


And it all worked fine. :)

Jack Christensen

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

westfw

Quote
int Je[8][5] ={
{0b10000000,0b00000000,0b11111110,0b01111111,0b00000000},

You still have 8bit quantities (apparently) in 16-bit variables...

Y2K986

Well...that's all what comes to my mind. I have no idea how to do that in any other way. Can you give me an example of how it's suppose to be done?  :smiley-red:

AWOL

Code: [Select]
byte Je[8][5] ={
{0b10000000,0b00000000,0b11111110,0b01111111,0b00000000},
would seem to do the trick
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Y2K986

OK. But it's the same like I already did. Is there a better ( optimized ) solution?

JimboZA

Code: [Select]
if (statement doesn't work for me...)
{
  find a different way of doing it;
}


Sorry, couldn't resist that....  8)
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

PaulS

Quote
OK. But it's the same like I already did. Is there a better ( optimized ) solution?

How is byte like int? A byte is half the size of an int.

Y2K986

I don't mind. :)
Well, I tried switch-case and I get the same problem. :)

Y2K986

Oh...sry..  :smiley-red: Did not see that ! Thank you :)

Go Up