/*Hi, I have an error and don't know how to solve it.
description: Taking analog input storing it to array and then giving it to output led
*/
void setup(){
Serial.begin(9600);
}
//code for analog input piezo:
void loop(){
void piezo();
void led();
}
void piezo(){
int array [15] ;
int i=0;
int val1;
int value;
int val2;
val1=analogRead(A0); //reading analog input
if (val1>0)
{ value= val1*1000; //multiplying with 1000 to give to led
array[i]=value;
i++ ;
}
val2=analogRead(A1);
if (val2>0)
{ value=val2;
array[i]=value;
i++ ; }
}
// reason I have multiplied with 1000 is that I can use if statement and based on value I can give it to the led
//Code for led:
void led(){
int i; int value; int array[15];
for (i=0; i<14; i++)
{
value = array[i];
{ if (value>1000)
{ digitalWrite( 13, HIGH);
delay(2000);
digitalWrite(13, LOW);}
else if (value<1000)
{digitalWrite(12, HIGH);
delay(2000);
digitalWrite(12, LOW);}
}
delay(1000);
}
}
So this code i don't have errors but haven't tested as friend borrowed my arduino for a couple of days.
the code contains an array intialized to 15 elements
Anywho my question is how to intialize number of elements in an array if array size is not known?
Dynamic size is not allowed. (no 'new' command)
Size an array to biggest possible (RAM restrictions)
If u need thousends of elements -> store to SD-card (which is extremly slow)
summi11:
Anywho my question is how to intialize number of elements in an array if array size is not known?
The question doesn't make much sense - I don't see how the array size could be unknown. The whole of the code looks very strange and I am having a hard time understanding it. Perhaps if you told what you are trying to achieve then we could explain how to approach that easier than trying to fix the code you have so far.
void piezo(){
int array [15] ;
int i=0;
int val1;
int value;
int val2;
val1=analogRead(A0); //reading analog input
if (val1>0)
{
value= val1*1000; //multiplying with 1000 to give to led
array[i]=value;
i++ ;
}
val2=analogRead(A1);
if (val2>0)
{
value=val2;
array[i]=value;
i++ ;
}
}
Then look up the size of an int it cant hold 1023 (the maximum returned by analogread()) x 1000 value= val1*1000; //multiplying with 1000 to give to led
and the maximum value for analogWrite() is 255.
The size of your array is 15 which is silly if you intend to store 2 values in each time you call piezo().
You do of course understand that as soon as you get to the end of piezo() all that data is lost!. Look scope.
I only see two arrays and they are both 15 values in size, what's the problem?
Apart from that the code in peizo() is very strange, you only ever write into element 0 if val1>0 in array and possibly element 1 if both val1>0 and val2>0.
Then array is local so it's lost as soon as the functions return, which is lucky because you never reset "i" so eventually it will exceed the array size.
This code is chock full of bugs, I suspect this will not do anything useful.
Okay so what i am trying to do is take inputs from piezo-resistive element 1 and 2 and make an array of inputs and then i want to give this array as outputs to leds 1 and 2 respectively. And with array size 15.