Is there enough memory to use: float myArray[8][8] on a Arduino? I ask this because I all get strange results of the array
Tell us what board.
Post your code.
Post the results shown when you compile your code.
Yes, on any standard Arduino.
Is there enough memory to use: float myArray[8][8] on a Arduino?
Yes. It should occupy 256 bytes (884) of memory (out of about 2000 bytes that should be available.)
Depending on what other memory your program uses, of course.
it will give result 256 bytes (884) of memory (out of about 2000 bytes that should be available.)
here c++ tutorial Welookups คาสิโนออนไลน์ เล่นง่าย ผ่านเว็บตรง ใหม่ล่าสุด
Hm, then I doing something wrong. I have the following code to read the voltage of the analog inputs of an mega 2560 and put these values into a array.
float voltage[8][8];
int aRow = 0;
int aCol = 0;
for (int i=dOutput1; i <= dOutput8; i++) {
digitalWrite(i, HIGH);
delay(1);
for (int x=aInput1; x <= aInput8; x++)
{
voltage[aRow][aCol] = (analogRead(x)*5)/1024.0;
aCol++;
}
aRow++;
aCol = 0;
digitalWrite(i, LOW);
delay(1);
}
//Debug check the values
for (int u=0; u <= 7; u++) {
for (int v=0; v <= 7; v++) {
Serial.print(voltage[u][v]);
Serial.print(" ");
}
Serial.println(" ");
}
float voltage[8][8];
The voltage array has 8 columns numbered 0 to 7
As a guess, you are writing beyond the bounds of the array but we don't know how all of your variables are declared and the code will not compile anyway
Please post a complete program
The complete test program:
int aInput1 = A1;
int aInput2 = A2;
int aInput3 = A3;
int aInput4 = A4;
int aInput5 = A5;
int aInput6 = A6;
int aInput7 = A7;
int aInput8 = A8;
#define pinMode(aInput1, INPUT);
#define pinMode(aInput2, INPUT);
#define pinMode(aInput3, INPUT);
#define pinMode(aInput4, INPUT);
#define pinMode(aInput5, INPUT);
#define pinMode(aInput6, INPUT);
#define pinMode(aInput7, INPUT);
#define pinMode(aInput8, INPUT);
int dOutput1 = 2;
int dOutput2 = 3;
int dOutput3 = 4;
int dOutput4 = 5;
int dOutput5 = 6;
int dOutput6 = 7;
int dOutput7 = 8;
int dOutput8 = 9;
pinMode(dOutput1, OUTPUT);
pinMode(dOutput2, OUTPUT);
pinMode(dOutput3, OUTPUT);
pinMode(dOutput4, OUTPUT);
pinMode(dOutput5, OUTPUT);
pinMode(dOutput6, OUTPUT);
pinMode(dOutput7, OUTPUT);
pinMode(dOutput8, OUTPUT);
void setup()
{
digitalWrite(dOutput1, LOW);
digitalWrite(dOutput2, LOW);
digitalWrite(dOutput3, LOW);
digitalWrite(dOutput4, LOW);
digitalWrite(dOutput5, LOW);
digitalWrite(dOutput6, LOW);
digitalWrite(dOutput7, LOW);
digitalWrite(dOutput8, LOW);
Serial.begin(9600);
}
void loop()
{
int aRow = 0;
int aCol = 0;
float voltage[8][8];
for (int i=dOutput1; i <= dOutput8; i++) {
digitalWrite(i, HIGH);
delay(1);
for (int x=aInput1; x <= aInput8; x++)
{
voltage[aRow][aCol] = (analogRead(x)*5)/1024.0;
aCol++;
}
aRow++;
digitalWrite(i, LOW);
delay(1);
}
// Check output
for (int u=0; u <= 7; u++) {
for (int v=0; v <= 7; v++) {
Serial.print(voltage[u][v]);
Serial.print(" ");
}
Serial.println(" ");
}
//run only once for test
int var = 0;
while(var < 200){
// endless loop
//var++;
delay(1000);
}
}
Colin699:
The complete test program:int aInput1 = A1;
int aInput2 = A2;
int aInput3 = A3;
int aInput4 = A4;
int aInput5 = A5;
int aInput6 = A6;
int aInput7 = A7;
int aInput8 = A8;
#define pinMode(aInput1, INPUT);
#define pinMode(aInput2, INPUT);
#define pinMode(aInput3, INPUT);
#define pinMode(aInput4, INPUT);
#define pinMode(aInput5, INPUT);
#define pinMode(aInput6, INPUT);
#define pinMode(aInput7, INPUT);
#define pinMode(aInput8, INPUT);
int dOutput1 = 2;
int dOutput2 = 3;
int dOutput3 = 4;
int dOutput4 = 5;
int dOutput5 = 6;
int dOutput6 = 7;
int dOutput7 = 8;
int dOutput8 = 9;
pinMode(dOutput1, OUTPUT);
pinMode(dOutput2, OUTPUT);
pinMode(dOutput3, OUTPUT);
pinMode(dOutput4, OUTPUT);
pinMode(dOutput5, OUTPUT);
pinMode(dOutput6, OUTPUT);
pinMode(dOutput7, OUTPUT);
pinMode(dOutput8, OUTPUT);
void setup()
{
digitalWrite(dOutput1, LOW);
digitalWrite(dOutput2, LOW);
digitalWrite(dOutput3, LOW);
digitalWrite(dOutput4, LOW);
digitalWrite(dOutput5, LOW);
digitalWrite(dOutput6, LOW);
digitalWrite(dOutput7, LOW);
digitalWrite(dOutput8, LOW);
Serial.begin(9600);
}
void loop()
{
int aRow = 0;
int aCol = 0;
float voltage[8][8];
for (int i=dOutput1; i <= dOutput8; i++) {
digitalWrite(i, HIGH);
delay(1);
for (int x=aInput1; x <= aInput8; x++)
{
voltage[aRow][aCol] = (analogRead(x)*5)/1024.0;
aCol++;
}
aRow++;
digitalWrite(i, LOW);
delay(1);
}
// Check output
for (int u=0; u <= 7; u++) {
for (int v=0; v <= 7; v++) {
Serial.print(voltage[u][v]);
Serial.print(" ");
}
Serial.println(" ");
}
//run only once for test
int var = 0;
while(var < 200){
// endless loop
//var++;
delay(1000);
}
}
its a bit messy this probably would be one of the better ways:
const uint8_t aInputs = 8;
const uint8_t analog_pins[aInputs] = {A1, A2, A3, A4, A5, A6, A7, A8};
const uint8_t dOutputs = 8;
const uint8_t digital_pins[dOutputs] = {2, 3, 4, 5, 6, 7, 8, 9};
float voltage[8][8];
void setup()
{
for (uint8_t i = 0; i <= dOutputs; i++) {
pinMode(digital_pins[i], OUTPUT);
}
Serial.begin(9600);
}
void loop()
{
uint8_t aRow = 0;
uint8_t aCol = 0;
for (uint8_t i = 0; i < dOutputs; i++) {
digitalWrite(digital_pins[i], HIGH);
delay(1);
for (uint8_t j = 0; j < aInputs; j++)
{
analogRead(analog_pins[j]);
voltage[aRow][aCol] = (analogRead(analog_pins[j]) * 5) / 1024.0;
++aCol;
}
++aRow;
digitalWrite(digital_pins[i], LOW);
delay(1);
}
// Check output
for (uint8_t u = 0; u <aRow; u++) {
for (uint8_t v = 0; v <aCol; v++) {
Serial.print(voltage[u][v]);
Serial.print(" ");
}
Serial.println(" ");
}
//run only once for test
uint8_t var = 0;
while (var < 200) {
// endless loop
//var++;
delay(1000);
}
}
what "strange results of the array" do you get and why to think they are odd?
Thanks. You code is indeed a lot clearer and better to read.
Below the output what is should be:
3.55 3.53 3.51 3.50 3.47 3.44 3.33 3.17
3.51 3.57 3.49 3.48 3.46 3.42 3.31 3.18
3.45 3.45 3.59 3.43 3.40 3.36 3.26 3.14
3.38 3.39 3.38 3.65 3.34 3.31 3.21 3.09
3.32 3.32 3.30 3.29 3.69 3.25 3.14 3.02
3.19 3.19 3.18 3.17 3.15 3.70 3.04 2.92
2.83 2.82 2.82 2.81 2.79 2.77 3.82 2.62
2.42 2.41 2.40 2.38 2.37 2.36 2.30 4.02
And this is the output what I get:
3.56 3.56 3.56 3.56 3.56 3.56 3.56 3.56
nan nan nan nan nan nan nan nan
3.52 3.52 3.51 3.52 3.52 3.52 3.52 3.52
nan nan nan nan nan nan nan nan
3.46 3.46 3.46 3.46 3.46 3.46 3.46 3.46
nan nan nan nan nan nan nan nan
3.40 3.39 3.39 3.40 3.40 3.39 3.39 3.39
0.00 0.00 0.00 0.00 nan 0.00 0.00 0.00
And with your code I unfortunately don’t get output at all
With regards to the code in reply #7, I suggest that you crank up the warning levels to ALL in File -> Preferences.
This is what I get
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:12:0: warning: "pinMode" redefined
#define pinMode(aInput2, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:11:0: note: this is the location of the previous definition
#define pinMode(aInput1, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:13:0: warning: "pinMode" redefined
#define pinMode(aInput3, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:12:0: note: this is the location of the previous definition
#define pinMode(aInput2, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:14:0: warning: "pinMode" redefined
#define pinMode(aInput4, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:13:0: note: this is the location of the previous definition
#define pinMode(aInput3, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:15:0: warning: "pinMode" redefined
#define pinMode(aInput5, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:14:0: note: this is the location of the previous definition
#define pinMode(aInput4, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:16:0: warning: "pinMode" redefined
#define pinMode(aInput6, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:15:0: note: this is the location of the previous definition
#define pinMode(aInput5, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:17:0: warning: "pinMode" redefined
#define pinMode(aInput7, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:16:0: note: this is the location of the previous definition
#define pinMode(aInput6, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:18:0: warning: "pinMode" redefined
#define pinMode(aInput8, INPUT);
^
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_506734\sketch_oct26a.ino:17:0: note: this is the location of the previous definition
#define pinMode(aInput7, INPUT);
^
You should not redefine those. Once I remove those #defines, the compiler will happily complain about
pinMode(dOutput1, OUTPUT);
pinMode(dOutput2, OUTPUT);
pinMode(dOutput3, OUTPUT);
pinMode(dOutput4, OUTPUT);
pinMode(dOutput5, OUTPUT);
pinMode(dOutput6, OUTPUT);
pinMode(dOutput7, OUTPUT);
pinMode(dOutput8, OUTPUT);
before the setup() function. Function calls don't belong outside functions.
You don't set aCol back to zero after each row
Try printing aCol before putting the value in the voltage array. What do you see ?
UKHeliBob:
You don't set aCol back to zero after each rowTry printing aCol before putting the value in the voltage array. What do you see ?
He's right.
tried the code I posted with the change UKHeliBob suggested and I do get the analog reads and the output DigitalWrites. no sure what went wrong on your side...
const uint8_t aInputs = 8;
const uint8_t analog_pins[aInputs] = {A1, A2, A3, A4, A5, A6, A7, A8};
const uint8_t dOutputs = 8;
const uint8_t digital_pins[dOutputs] = {2, 3, 4, 5, 6, 7, 8, 9};
float voltage[8][8];
void setup()
{
for (uint8_t i = 0; i <= dOutputs; i++) {
pinMode(digital_pins[i], OUTPUT);
digitalWrite(digital_pins[i], LOW);
}
Serial.begin(9600);
}
void loop()
{
uint8_t aRow = 0;
uint8_t aCol = 0;
for (uint8_t i = 0; i < dOutputs; i++) {
digitalWrite(digital_pins[i], HIGH);
delay(1);
for (uint8_t j = 0; j < aInputs; j++)
{
analogRead(analog_pins[j]);
voltage[aRow][aCol] = (analogRead(analog_pins[j]) * 5) / 1024.0;
++aCol;
}
aCol = 0;
++aRow;
digitalWrite(digital_pins[i], LOW);
delay(1);
}
// Check output
for (uint8_t u = 0; u < aRow; u++) {
for (uint8_t v = 0; v < aInputs; v++) {
Serial.print(voltage[u][v]);
Serial.print(" ");
}
Serial.println(" ");
}
//run only once for test
uint8_t var = 0;
while (var < 200) {
// endless loop
//var++;
delay(1000);
}
}
Strange, I do not get an error message when compiling.
Sketch uses 5,306 bytes (2%) of program storage space. Maximum is 253,952 bytes.
Global variables use 248 bytes (3%) of dynamic memory, leaving 7,944 bytes for local variables. Maximum is 8,192 bytes.
But with the last code of sherzaad it works now.
Thank you all for your help
The original mistake was not resetting 'aCol' to 0 at the end of each row. Instead of starting the second row at [1][0] you start at [1][8] which is actually [2][0]. Instead of starting the third row at [2][0] you start at [2][16] which is actually [4][0]. The last place you write in your array is [7][63], WAY outside your array.
Here is my take on how it could have been written:
const byte aInputPins[8] = {A1, A2, A3, A4, A5, A6, A7, A8}; // Arduino MEGA required
const byte dOutputPins[8] = {2, 3, 4, 5, 6, 7, 8, 9};
void setup()
{
Serial.begin(9600);
for (byte output = 0; output < 8; output++)
{
pinMode(dOutputPins[output], OUTPUT);
digitalWrite(dOutputPins[output], LOW);
}
float voltage[8][8];
for (byte row = 0; row < 8; row++)
{
digitalWrite(dOutputPins[row], HIGH);
delay(1);
for (byte col = 0; col < 8; col++)
{
voltage[row][col] = (analogRead(aInputPins[col]) * 5) / 1024.0;
}
digitalWrite(dOutputPins[row], LOW);
delay(1);
}
// Check output
for (byte row = 0; row < 8; row++)
{
for (byte col = 0; col < 8; col++)
{
Serial.print(voltage[row][col]);
Serial.print(" ");
}
Serial.println(" ");
}
}
void loop()
{
}
Strange, I do not get an error message when compiling
The compiler is generally quite happy if your program writes to memory all over the place. It is your responsibility to write the program properly or include checks within it to avoid writing outside the bounds of arrays, for example.
Colin699:
Strange, I do not get an error message when compiling.
I didn't get an error but I did get a warning message that I did not understand:
/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.21/cores/arduino/main.cpp: In function 'main':
/Users/john/Documents/Arduino/sketch_oct20a/sketch_oct20a.ino:60:57: warning: iteration 1 invokes undefined behavior [-Waggressive-loop-optimizations]
voltage[aRow][aCol] = (analogRead(x) * 5) / 1024.0;
^
/Users/john/Documents/Arduino/sketch_oct20a/sketch_oct20a.ino:54:3: note: containing loop
for (int i = dOutput1; i <= dOutput8; i++)
^
Sketch uses 6090 bytes (21%) of program storage space. Maximum is 28672 bytes.
Global variables use 165 bytes (6%) of dynamic memory, leaving 2395 bytes for local variables. Maximum is 2560 bytes.
Once I figured out the error I could see that what the message was trying to tell me was: "The second time through the 'i' loop you will be using index 8 in an array of size 8." Adding "aCol = 0;" after "aRow++;" fixed the mistake and the warning went away.