I'm away from home (no arduino around) yet a simple problem troubles me all day: how to match/compare variables stored in an array with analog read values? I have a guess, I just want to know if I'm on the right track:
int storedValues[]={10,20,30,40,50};
for(int i=0; i<4; i++){
if(storedValues*==analogRead(1)){*
println("match"+i);
}
}
What's most important is that to return the index of the matching value of the array. For instance, if analogRead is 30, it should say "the third element of the array matches analogRead!" . Hope it makes sense! Thanks for all the help in advance!
int what_we_have[]={10,20,30,40,50};
int what_we_get = analogRead(1);
for(int i=0; i<5; i++){
if( what_we_get == what_we_have[i])
{
Serial.print("\n Hi, we've got a value: " );
Serial.print( what_we_get );
Serial.print("\t , that equals to N-th element of what_we_have : ");
Serial.print( i );
}
}
The values in the array would be analog non varying voltage values. As far as I've experienced the values tend to shift on the analog inputs and hardly ever the same. Hence I need to define the values like this
char a; // variable to store approximate value
if (analogRead <230 && analogRead > 226 ){ // let's say the value is 228 and tends to shift a bit
analogRead=char a};
array[]={char a,char b....};
Is that correct? Can I store values in chars? What should I do if the voltage values are not accurate numeric values?
I know, there is a trick to "read" several buttons using only one analog input and resistors.
Probably, should be some library for this already exist .
What I will do, is modify "constants" array in "double constants", where elements:
0 and 1 define low and high of the first range,
2 and 3 define low and high of the second range,
4 and 5 define low and high of the third range, etc.
Than :
int what_we_have[]={ 100, 120, 225, 230, 440, 505 ...........};
int what_we_get = analogRead(1);
for(int i=0; i<5; i + 2){
if( (what_we_get > what_we_have[i]) && (what_we_get < what_we_have[i+1]))
{
Serial.print("\n Hi, we've got a value: " );
Serial.print( what_we_get );
Serial.print("\t , that BELONGS to N-th range of what_we_have : ");
Serial.print( i / 2 );
}
}