I am making a program to scroll text across a set of 8x8 led matrices. After scrolling across one matrix I am adding a second one. I am having trouble adding several instances of my xShift variable to the pointer. What I am trying to do is have xShift be [2][8] the pointer would read it as 0-15 0-7 being xShift[0][0-7] and 8-15 be xShift[1][0-7]. I have searched through various forum threads and can still not find anything about multi dimensional arrays with pointers. Any help would be greatly appreciated.
Please, post a Code Snippet.
We can start with what you have already coded and advance from there ...
alright here is the code
byte charToPrint[95][5]
11 {
12 {0, 0, 0, 0, 0}, //SPACE
13 {95, 0, 0, 0, 0}, //!
14 {7, 7, 0, 0, 0}, //"
15 {20, 62, 20, 62, 20}, //#
16 {36, 42, 107, 42, 18}, //$
17 {76, 44, 16, 104, 100}, //%
18 {52, 74, 84, 32, 80}, //&
19 {7, 0, 0, 0, 0}, //'
20 {62, 65, 0, 0, 0}, //(
21 {65, 62, 0, 0, 0}, //)
22 {21, 14, 14, 21, 0}, //*
23 {8, 8, 62, 8, 8}, //+
24 {128, 96, 0, 0, 0}, //,
25 {8, 8, 8, 8, 8}, //-
26 {64, 0, 0, 0, 0}, //.
27 {32, 16, 8, 4, 2}, ///
28 {62, 81, 73, 69, 62}, //0
29 {68, 66, 127, 64, 64}, //1
30 {66, 97, 81, 73, 70}, //2
31 {65, 73, 73, 73, 54}, //3
32 {8, 12, 10, 127, 8}, //4
33 {71, 69, 69, 69, 57}, //5
34 {62, 73, 73, 73, 48}, //6
35 {97, 17, 9, 5, 3}, //7
36 {54, 73, 73, 73, 54}, //8
37 {6, 73, 73, 73, 62}, //9
38 {34, 0, 0, 0, 0}, //:
39 {64, 34, 0, 0, 0}, //;
40 {8, 20, 34, 0, 0}, //<
41 {20, 20, 20, 20, 20}, //=
42 {34, 20, 8, 0, 0}, //>
43 {2, 1, 81, 9, 6}, //?
44 {62, 73, 85, 93, 94}, //@
45 {126, 9, 9, 9, 126}, //A
46 {127, 73, 73, 73, 54}, //B
47 {62, 65, 65, 65, 34}, //C
48 {127, 65, 65, 65, 62}, //D
49 {127, 73, 73, 73, 73}, //E
50 {127, 9, 9, 9, 9}, //F
51 {62, 65, 65, 81, 50}, //G
52 {127, 8, 8, 8, 127}, //H
53 {65, 65, 127, 65, 65}, //I
54 {33, 65, 65, 63, 1}, //J
55 {127, 8, 20, 34, 65}, //K
56 {127, 64, 64, 64, 64}, //L
57 {127, 2, 4, 2, 127}, //M
58 {127, 2, 4, 8, 127}, //N
59 {62, 65, 65, 65, 62}, //O
60 {127, 9, 9, 9, 6}, //P
61 {62, 65, 81, 62, 64}, //Q
62 {127, 9, 9, 25, 102}, //R
63 {38, 73, 73, 73, 50}, //S
64 {1, 1, 127, 1, 1}, //T
65 {63, 64, 64, 64, 63}, //U
66 {31, 32, 64, 32, 31}, //V
67 {127, 32, 16, 32, 127}, //W
68 {99, 20, 8, 20, 99}, //X
69 {3, 4, 120, 4, 3}, //Y
70 {97, 81, 73, 69, 67}, //Z
71 {127, 65, 0, 0, 0}, //[
72 {2, 4, 8, 16, 32}, //\
73 {65, 127, 0, 0, 0}, //]
74 {4, 2, 1, 2, 4}, //^
75 {64, 64, 64, 64, 64}, //_
76 {1, 2, 0, 0, 0}, //`
77 {32, 84, 84, 84, 120}, //a
78 {127, 72, 72, 72, 48}, //b
79 {56, 68, 68, 68, 68}, //c
80 {48, 72, 72, 72, 127}, //d
81 {56, 84, 84, 84, 24}, //e
82 {68, 126, 69, 1, 2}, //f
83 {24, 164, 164, 164, 124}, //g
84 {127, 8, 8, 8, 112}, //h
85 {64, 68, 125, 64, 64}, //i
86 {64, 128, 132, 125, 4}, //j
87 {127, 8, 20, 34, 64}, //k
88 {64, 65, 127, 64, 64}, //l
89 {120, 8, 48, 8, 112}, //m
90 {120, 8, 8, 8, 112}, //n
91 {56, 68, 68, 68, 56}, //o
92 {252, 20, 20, 20, 8}, //p
93 {8, 20, 20, 20, 252}, //q
94 {120, 8, 8, 8, 16}, //r
95 {72, 84, 84, 84, 36}, //s
96 {8, 8, 126, 72, 8}, //t
97 {56, 64, 64, 64, 56}, //u
98 {24, 32, 64, 32, 24}, //v
99 {56, 64, 32, 64, 56}, //w
100 {68, 40, 16, 40, 68}, //x
101 {156, 160, 64, 32, 28}, //y
102 {68, 100, 84, 76, 68}, //z
103 {8, 54, 65, 0, 0}, //{
104 {127, 0, 0, 0, 0}, //|
105 {65, 54, 8, 0, 0}, //}
106 {8, 8, 16, 16, 0} //~
107 };
108
109
110
111
112 int dataPin = 9;
113 int clockPin = 10;
114 int latchPin = 8;
115 int numberOfDisplays = 1;
116
117 byte yShift[8];
118 byte xShift[1][8]; //here is where I want to put 2 or more and the pointer will redirect to this
119 boolean l = true;
120
121 void setup() {
122
123 cli(); //TO TURN OFF INTERRUPTS
124
125 TCCR1A = 0; //SET REGISTER TO 0
126 TCCR1B = 0; // SET REGISTER TO 0
127 TCNT1 = 0; //INITIALIZE COUNTER TO 0 VALUE
128
129 OCR1A = 259; //SET THE COMPARE MATCH REGISTER TO 259 VALUE
130
131 TCCR1B |= (1 << WGM12); //TURN ON CTC MODE
132
133 TCCR1B |= (1 << CS12) | (1 << CS10); // MAKE THE 1024 BIT PRESCALER
134
135 TIMSK1 |= (1 << OCIE1A); //ENABLE THE COMPARE INTERRUPT
136
137
138 sei(); //TURN ON INTERRUPTS
139 // x = new byte[8];
140 // y = new byte[8];
141
142 pinMode(dataPin, OUTPUT);
143 pinMode(clockPin, OUTPUT);
144 pinMode(latchPin, OUTPUT);
145 for (int i = 0; i < numberOfDisplays; i++)
146 {
147 for (int j = 0; j < 8; j++)
148 {
149 yShift[j] = 255;
150 bitWrite(yShift[j], j, 0);
151
152 }
153 }
154 // thread(displayToMatrix);
155
156 //Serial.begin(9600);
157
158
159
160 }
161
162 void loop() {
163
164
165 //writePixel(7,7);
166 //writePixel(6,6);
167 //writePixel(5,5);
168 //writePixel(4,4);
169 //writePixel(3,3);
170 //writePixel(2,2);
171 //writePixel(1,1);
172 //writePixel(0,0);
173 //writePixel(0,7);
174 //writePixel(1,6);
175 //writePixel(2,5);
176 //writePixel(3,4);
177 //writePixel(4,3);
178 //writePixel(5,2);
179 //writePixel(6,1);
180 //writePixel(7,0);
181
182
183 scrollText("HELLO WORLD!", 100);
184
185 //writePixel(0,0);
186
187 //writePixel(1,3);
188
189 }
190
191
192
193
194
195 void writePixel(int xDraw, int yDraw)
196 {
197
198 bitWrite(xShift[0][yDraw], xDraw, 1);
199
200
201 }
202 void erasePixel(int xErase, int yErase)
203 {
204
205 bitWrite(xShift[0][yErase], xErase, 0);
206
207 }
208
209
210 ISR(TIMER1_COMPA_vect)
211 {
212 for (int i = 0; i < 8; i++)
213 {
214
215 digitalWrite(latchPin, LOW);
216
217
218
219
220 shiftOut(dataPin, clockPin, LSBFIRST, xShift[0][i]);
221 shiftOut(dataPin, clockPin, LSBFIRST, yShift[i]);
222
223
224 digitalWrite(latchPin, HIGH);
225
226 // delay(500);
227
228
229 }
230 }
231
232 void scrollText(String stringToPrint, int scrollSpeed)
233 {
234
235 byte stringBitmap[stringToPrint.length() * 6];
236 for (int i = 0; i < stringToPrint.length(); i++)
237 {
238 for (int j = 0; j < 6; j++)
239 {
240 if (j == 5)
241 {
242 stringBitmap[i * 6 + j] = 0;
243 } else
244 {
245 stringBitmap[i * 6 + j] = charToPrint[stringToPrint.charAt(i) - 32][j];
246 }
247 }
248 }
249
250
251 for (int i = 0; i < stringToPrint.length(); i++)
252 {
253 for(int l = 0; l < 6; l++)
254 {
255 for (int k = 0; k < 8; k++)
256 {
257 if (k == 7)
258 {
259
260 for (int j = 0; j < 8; j++)
261 {
262
263 bitWrite(xShift[0][j], k, bitRead(stringBitmap[i*6+l], 7-j));
264
265 }
266 } else{
267 for (int j = 0; j < 8; j++)
268 {
269 bitWrite(xShift[0][j], k, bitRead(xShift[0][j], k+1));
270 }
271 }
272 }
273 delay(scrollSpeed);
274 }
275
276 }
277
278 }
Which pointer?
There is not a single pointer in the program.
So I imagine that you want to have a pointer to 8 consecutive bytes inside that 2d array?
byte many[4][8] = {
{1, 2, 3, 4, 5, 6, 7, 8},
{11, 12, 13, 14, 15, 16, 17, 18},
{21, 22, 23, 24, 25, 26, 27, 28},
{31, 32, 33, 34, 35, 36, 37, 38}
};
void setup() {
Serial.begin(115200);
byte* slice = &many[2][0];
pSlice(2, slice);
pSlice(3, &many[3][0]);
slice -= 8;
pSlice(1, slice);
}
void loop() {}
void pSlice(byte num, byte* ptr) {
Serial.print("slice ");
Serial.print(num);
Serial.print(" -> {");
for (byte idx = 0; idx < 8; idx++) {
Serial.print(ptr[idx]);
if (idx != 7) Serial.print(",");
}
Serial.println("}");
}
slice 2 -> {21,22,23,24,25,26,27,28}
slice 3 -> {31,32,33,34,35,36,37,38}
slice 1 -> {11,12,13,14,15,16,17,18}
Whandall:
Which pointer?There is not a single pointer in the program.
So I imagine that you want to have a pointer to 8 consecutive bytes inside that 2d array?
There is no pointer in the program because I was having trouble getting them to work. I do not want a pointer to 8 consecutive bytes in the array I want to pointer to point to 0-7 for xShift[0][0-7] and 8-15 for xShift[1][0-7]. Thanks for the help. Sorry if I am not explaining myself well.
odawg7:
I want to pointer to point to 0-7 for xShift[0][0-7] and 8-15 for xShift[1][0-7]
I can not understand what you are talking about.
Pointers always point exactly to one element (or nothing),
they can point to the first (or last, or ..) of some consecutive elements.
They can even point to memory before or after existing elements.
There is no xShift[0][0-7], there are xShift[0][0], xShift[0][1], ..., xShift[0][7].
But you do not want a pointer to these 8 bytes.
You do not show what you try to do,
you want a pointer to point at something non-existant (0-7) for something non-existant (xShift[0][0-7]).
That is a concept that I can not express in C++.
Whandall:
I can not understand what you are talking about.Pointers always point exactly to one element (or nothing),
they can point to the first (or last, or ..) of some consecutive elements.
They can even point to memory before or after existing elements.There is no xShift[0][0-7], there are xShift[0][0], xShift[0][1], ..., xShift[0][7].
So what I am trying to do is put all of xShift to the pointer. If I call pointer[0] it would give me xShift[0][0] if I call pointer[8] it would call xShift[1][0] pointer[15] is xShift[1][7]. I tried implementing it in the code here.
byte charToPrint[95][5]
{
{0, 0, 0, 0, 0}, //SPACE
{95, 0, 0, 0, 0}, //!
{7, 7, 0, 0, 0}, //"
{20, 62, 20, 62, 20}, //#
{36, 42, 107, 42, 18}, //$
{76, 44, 16, 104, 100}, //%
{52, 74, 84, 32, 80}, //&
{7, 0, 0, 0, 0}, //'
{62, 65, 0, 0, 0}, //(
{65, 62, 0, 0, 0}, //)
{21, 14, 14, 21, 0}, //*
{8, 8, 62, 8, 8}, //+
{128, 96, 0, 0, 0}, //,
{8, 8, 8, 8, 8}, //-
{64, 0, 0, 0, 0}, //.
{32, 16, 8, 4, 2}, ///
{62, 81, 73, 69, 62}, //0
{68, 66, 127, 64, 64}, //1
{66, 97, 81, 73, 70}, //2
{65, 73, 73, 73, 54}, //3
{8, 12, 10, 127, 8}, //4
{71, 69, 69, 69, 57}, //5
{62, 73, 73, 73, 48}, //6
{97, 17, 9, 5, 3}, //7
{54, 73, 73, 73, 54}, //8
{6, 73, 73, 73, 62}, //9
{34, 0, 0, 0, 0}, //:
{64, 34, 0, 0, 0}, //;
{8, 20, 34, 0, 0}, //<
{20, 20, 20, 20, 20}, //=
{34, 20, 8, 0, 0}, //>
{2, 1, 81, 9, 6}, //?
{62, 73, 85, 93, 94}, //@
{126, 9, 9, 9, 126}, //A
{127, 73, 73, 73, 54}, //B
{62, 65, 65, 65, 34}, //C
{127, 65, 65, 65, 62}, //D
{127, 73, 73, 73, 73}, //E
{127, 9, 9, 9, 9}, //F
{62, 65, 65, 81, 50}, //G
{127, 8, 8, 8, 127}, //H
{65, 65, 127, 65, 65}, //I
{33, 65, 65, 63, 1}, //J
{127, 8, 20, 34, 65}, //K
{127, 64, 64, 64, 64}, //L
{127, 2, 4, 2, 127}, //M
{127, 2, 4, 8, 127}, //N
{62, 65, 65, 65, 62}, //O
{127, 9, 9, 9, 6}, //P
{62, 65, 81, 62, 64}, //Q
{127, 9, 9, 25, 102}, //R
{38, 73, 73, 73, 50}, //S
{1, 1, 127, 1, 1}, //T
{63, 64, 64, 64, 63}, //U
{31, 32, 64, 32, 31}, //V
{127, 32, 16, 32, 127}, //W
{99, 20, 8, 20, 99}, //X
{3, 4, 120, 4, 3}, //Y
{97, 81, 73, 69, 67}, //Z
{127, 65, 0, 0, 0}, //[
{2, 4, 8, 16, 32}, //\
{65, 127, 0, 0, 0}, //]
{4, 2, 1, 2, 4}, //^
{64, 64, 64, 64, 64}, //_
{1, 2, 0, 0, 0}, //`
{32, 84, 84, 84, 120}, //a
{127, 72, 72, 72, 48}, //b
{56, 68, 68, 68, 68}, //c
{48, 72, 72, 72, 127}, //d
{56, 84, 84, 84, 24}, //e
{68, 126, 69, 1, 2}, //f
{24, 164, 164, 164, 124}, //g
{127, 8, 8, 8, 112}, //h
{64, 68, 125, 64, 64}, //i
{64, 128, 132, 125, 4}, //j
{127, 8, 20, 34, 64}, //k
{64, 65, 127, 64, 64}, //l
{120, 8, 48, 8, 112}, //m
{120, 8, 8, 8, 112}, //n
{56, 68, 68, 68, 56}, //o
{252, 20, 20, 20, 8}, //p
{8, 20, 20, 20, 252}, //q
{120, 8, 8, 8, 16}, //r
{72, 84, 84, 84, 36}, //s
{8, 8, 126, 72, 8}, //t
{56, 64, 64, 64, 56}, //u
{24, 32, 64, 32, 24}, //v
{56, 64, 32, 64, 56}, //w
{68, 40, 16, 40, 68}, //x
{156, 160, 64, 32, 28}, //y
{68, 100, 84, 76, 68}, //z
{8, 54, 65, 0, 0}, //{
{127, 0, 0, 0, 0}, //|
{65, 54, 8, 0, 0}, //}
{8, 8, 16, 16, 0} //~
};
int dataPin = 9;
int clockPin = 10;
int latchPin = 8;
int numberOfDisplays = 2;
byte yShift[8];
byte xShift[2][8];
boolean l = true;
byte * xShiftPointer; //this is the pointer
void setup() {
//byte * xShiftPointer;
for(int i = 0; i < numberOfDisplays; i++)
{
xShiftPointer = &xShift[i][0]; //what I am trying to do here is make the pointer point to all of the values in xShift
}
cli(); //TO TURN OFF INTERRUPTS
TCCR1A = 0; //SET REGISTER TO 0
TCCR1B = 0; // SET REGISTER TO 0
TCNT1 = 0; //INITIALIZE COUNTER TO 0 VALUE
OCR1A = 259; //SET THE COMPARE MATCH REGISTER TO 259 VALUE
TCCR1B |= (1 << WGM12); //TURN ON CTC MODE
TCCR1B |= (1 << CS12) | (1 << CS10); // MAKE THE 1024 BIT PRESCALER
TIMSK1 |= (1 << OCIE1A); //ENABLE THE COMPARE INTERRUPT
sei(); //TURN ON INTERRUPTS
// x = new byte[8];
// y = new byte[8];
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
for (int i = 0; i < numberOfDisplays; i++)
{
for (int j = 0; j < 8; j++)
{
yShift[j] = 255;
bitWrite(yShift[j], j, 0);
}
}
// thread(displayToMatrix);
//Serial.begin(9600);
}
This is exactly what you turned down when I suggested it.
My sample code even showed the effect of pointer arithmetic.
This is quite a simple task. Array data (even ones with multiple dimensions) is laid out sequentially. Taking a pointer to the first element is all you need to do. Then you can use that pointer to move through the whole array as if it were a single dimension.
unsigned char array[3][8] = {
{0,1,2,3,4,5,6,7},
{8,9,10,11,12,13,14,15},
{16,17,18,19,20,21,22,23}
};
void setup() {
Serial.begin(9600);
unsigned char *ptr = array[0];
for( int i = 0 ; i < 24 ; ++i){
Serial.println( ptr[i] );
}
}
void loop() {}