cycling through an array

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:

int myStations[5] = {911, 985, 1011, 1051}; //array storing multiple stations

If I set it up like this,

for (int i = 0; i<5; i=i+1){ //trying to set up an array for scrolling through
channel = myStations*; //favorite stations*

  • tft.fillRoundRect(160, 40, 140, 25,6, HX8357_BLACK); //erase part of the screen for new station*

  • radio.setChannel(channel); //set new station*

  • delay(2000);*

  • }*
    the tuning scrolls through the array and stops at 0MHz (the null value of the array).
    When I tried without a for loop like this,

  • int i*

  • int n = i+1;*

  • channel = myStations[n]; //favorite stations*

  • tft.fillRoundRect(160, 40, 140, 25,6, HX8357_BLACK);*

  • radio.setChannel(channel);*

  • delay(2000);*

  • 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?

How do I set up an incrementing counter n

It looks like you are declaring i each time through loop() although it is impossible to be sure as you have not posted a complete program (HINT !)

Why mess around with 2 variables ? Just add 1 to the array index variable to move through 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.

Si4703_Radio_TFT_Amp_RTC_test1.ino (14.9 KB)

  controlRadio();                                                   //call controlRadio function

If you cut out all the dumb comments, you could've posted you code.

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++;
  }
}
const uint16_t myStations [] = {911, 985, 1011, 1051};
byte stationIndex;
void setup
{
}
void loop
{
  if (cycleCondition == true)
  {
    stationIndex++;
    // some bounds-checking code here based on "sizeof (myStations) / sizeof (myStations [0])"
  }
}

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?

if(stationIndex<5){
channel = myStations[stationIndex];
tft.fillRoundRect(160, 40, 140, 25,6, HX8357_BLACK);
radio.setChannel(channel);
delay(500);
stationIndex++;
} else stationIndex=0;

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;
  }
}

you need to change your check to <4 if you have 4 stations because your array is 0 indexed, so station 1 is index 0, station 4 is index 3, null is 4.

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.