I am new to coding and I am trying to understand how to cycle through an array, one element at a time. I thought that I needed a for loop at first, but this simply cycles through the entire array. How can I manually step through an array?
Background: My sketch drives a touchscreen TFT controlled Si4703 FM Stereo Module. I have no trouble tuning using the touchscreen. Instead of tuning using the seek functions, I wanted to tune through a list of preset stations - hence the array:
i = n+1;* the tuning advances to the next station in the array and is stuck there. How do I set up an incrementing counter n to keep advancing to the next value in the array and then wrap around when it reaches the last value in the array?
Thanks for the quick response. I am attaching the code for your reference. The functionality I am working on is in the controlRadio() function. The initial station (referred to as "channel" from the original code I was copying) is set on line 133. I am not clear on how to use a variable as a counter to increment through the array. A counter variable makes more sense to me seeing it in a for loop.
rzelko:
I am new to coding and I am trying to understand how to cycle through an array, one element at a time. I thought that I needed a for loop at first, but this simply cycles through the entire array. How can I manually step through an array?
int myStations[5] = {911, 985, 1011, 1051};
int stationIndex = 0;
void setup
{
}
void loop
{
if (cycleCondition == true)
{
stationIndex++;
}
}
Thanks for the all the input. I see how it's done now. I have it cycling through the array, but I'm a bit confused as to how to add the bounds-checking code that was suggested. I understand the theory of "sizeof" but I have never attempted it.
Would it be done as an embedded if statement or as an else to the my if statement?
Doing it per the above code, I am successful resetting the index to recycle, however it still sends me to a null station. Perhaps the "sizeof" approach would yield different results??
rzelko:
I'm a bit confused as to how to add the bounds-checking code that was suggested.
if (cycleCondition == true)
{
if (stationIndex < 5) // for an array of 5 stations array size is 6 (for the null)
{
stationIndex++;
}
else
{
stationIndex = 0;
}
}
Got it. Thank you so much for the input. You’ve been most helpful.
Before I steer off in another direction that may prove to be a dead end, is there a way to write to an array from within a user interface? I am building a menu driven version to additionally control an MP3 player and incorporate a RTC module. I would like to be able to add stations to the array from user input. Is that possible?
rzelko:
is there a way to write to an array from within a user interface?
int myArray[6];
int myArrayIndex;
/*... more code
*/
myArrayIndex = whereIWantToStoreInterfaceInput;
myArray[myArrayIndex] = InterfaceInput;
/*... more code
*/
rzelko:
I would like to be able to add stations to the array from user input. Is that possible?
You will have to limit the number of stations that can be input to the size of your array -1 (for the null). You can populate the array with default values (0) and perform a check on the station value (!= 0) before changing the channel to the station. maybe if the value is 0 it indexes and performs the check again, cycling through the unused (default 0) stations.