Hey!
Firstly I'm beginner don't hate me pls.
And my problem is with arrays, i need to save analogRead data to an array and analogwrite it after some delay, can you help?
int analogPin = A0; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup()
{
Serial.begin(9600); // setup serial
}
void loop()
{
int what_we_have[]={};
int what_we_get = analogRead(analogPin);
int i = 0;
while(what_we_get)
{
what_we_have[i]= what_we_get;
i++;
}
val = analogRead(analogPin); // read the input pin
Serial.println(what_we_get); // debug value
}
int what_we_have[]={};
That creates an array with zero elements (not an empty array). See how to declare arrays in the Reference.
The while loop will run until what_we_get is zero which Wil never happen. You need to do analogRead inside the while loop.
like this?
int analogPin = A0; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
int commands[128]={};
int i =0;
void setup()
{
Serial.begin(9600); // setup serial
pinMode(analogPin, INPUT);
}
void loop()
{
int command = analogRead(analogPin);
if(command != 0)
{
while(command != 0)
{
commands[i] = command;
i++;
Serial.print(command);
Serial.print(" proc\n");
}
}
else
{
for(int m = 0; m<sizeof(commands); m++){
Serial.print(commands[m]);
Serial.print(" ok\n");
}
}
Serial.print(" fail\n");
}
Not quite
if (command != 0)
{
while (command != 0)
{
...
...
}
Once you enter the while loop, you will never get out of it because you don't update the command variable. You also don't limit the i variable to the size of the array so you will happily overwrite memory that is not part of the array.
Further
for (int m = 0; m < sizeof(commands); m++)
I suggest that you print out sizeof(commands). It will not give you what you think it will give you sizeof() returns the size of the array in bytes, not in elements. So 128 ints equals 256 bytes.
If you first want to read 128 analog values and next display them, you can use something like
void loop()
{
// index in array
unsigned int index = 0;
// do N readings and store in array
while (index < sizeof(commands) / sizeof(commands[0]))
{
commands[index++] = analogRead(analogPin);
}
// display the readings
for (unsigned int cnt = 0; cnt < sizeof(commands) / sizeof(commands[0]); cnt++)
{
Serial.print("Value at index "); Serial.print(cnt);
Serial.print(" = "); Serial.println(commands[cnt]);
}
}
Note how the number of elements of the array is determined. One usually defines a macro for that like this
#define NUMELEMENTS(x) sizeof(x)/sizeof(x[0])
This macro can be used on any type of array; some examples below
#define NUMELEMENTS(x) sizeof(x)/sizeof(x[0])
char charArray[5];
byte byteArray[6];
int intArray[7];
long longArray[8];
void setup()
{
Serial.begin(9600);
Serial.println("Number of elements in ");
Serial.print("charArray = "); Serial.println(NUMELEMENTS(charArray));
Serial.print("byteArray = "); Serial.println(NUMELEMENTS(byteArray));
Serial.print("intArray = "); Serial.println(NUMELEMENTS(intArray));
Serial.print("longArray = "); Serial.println(NUMELEMENTS(longArray));
}
void loop()
{
}