Incrementing array steps

Hello,

I am very new to Arduino, and I have a general question. If I have a square wave generator like this:

{ 
     // Squarewave gen 
     digitalWrite(A5, HIGH); 
     delay(val2); 
     digitalWrite(A5, LOW);
     delay(val2);
}

(where val2 is coming from a potentiometer), how would I get "digitalWrite(A5, HIGH); " to increment through steps this array?:

int seqArray[] = {0,1,2,3,4,5,6,7,8,9};

Obviously, I’d have to incorporate state change code in there, which I believe I know how to do. I just have not been able to figure out how to get the array to increment when A5 goes high.

Thank you in advance!

digitalWrite(A5, HIGH);can't increment, but a variable could. Initialise the variable to 0, then every time you execute the digitalWrite(); increment the variable.

What is it that you want to increment? You talk about incrementing an array, but an array doesn't increment. Neither does the output of digitalWrite. The output is either HIGH or LOW. Nothing in-between, and nothing higher than HIGH, and nothing lower than LOW.. You already told us that the delay value comes from a potentiometer, so what is left? I am confused, please explain.

to change the delays (i.e. increment them through your Array) I like these two ways.

untested

int j;
int seqArray[10] = {0,1,2,3,4,5,6,7,8,9};

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < 10 ; i++)// do it all at once
  { 
    digitalWrite(A5, HIGH); 
    delay(seqArray[i]); 
    digitalWrite(A5, LOW);
    delay(seqArray[i]);
    Serial.println(i);
  }
}
void loop()
{
  Serial.println(j);
  digitalWrite(A5, HIGH); // do it over and over, your square wave frequency dropping as you progress through the array.
  delay(seqArray[j]); 
  digitalWrite(A5, LOW);
  delay(seqArray[j]);
  j++;
  if (j > 9) j = 0;
}

I may not be properly explaining what I want todo. The potentiometer alone changes the frequency of the square wave. What I want to happen is each time A5 goes high, it calls the value stored in the first step of the array(0), the next time it goes high, it calls the value stored in the next step of the array (1), etc.

the next time it goes high, it calls the value stored in the next step of the array (1), etc.

What is "it" that goes high? What is "it" that gets (not calls) a value in the array? What are you going to do with the value?

Well, I think I need to look over my whole project and give some more thought to my question before I post again. As I try to answer your questions, I'm beginning to see flaws in my "design". Feeling a little dumb right about now! I've gotten quite far, getting the whole working one little chunk at a time, but I think I've gotten myself confused. Your replies have helped me realize where I might be going wrong, so I won't bug you until either I figure it out or at least know the right questions to ask!

Some things to help you get going:

If you're trying to make something happen, based on how many pulses have been received, a switch statement could be used. For example, you could have an array index that increments by 1 and mods (%) by however many functions you have, like:

int array_place+=1;
array_place=array_place%number_of_functions;

This will loop through the array. You could then use a switch statement to do different functions:

switch(array_place) {
case 1:
   //do function 1
   break
}

Why you'd want to do this over hardcoding function calls in loop() after each pulse and delay pair is beyond me, so maybe I'm not understanding.

JB8256: As I try to answer your questions, I'm beginning to see flaws in my "design". Feeling a little dumb right about now!

Happens to all of us. Don't feel bad about it.

...R

I finally figured out something that works. Using state change detection, a counter, and switch case, I am now able to get the values from elements in the array (seqArray) to create a 10 step sequencer:

{ 
     // Squarewave gen 
     digitalWrite(A5, HIGH); 
     delay(val2); 
     digitalWrite(A5, LOW);
     delay(val2);
}

  // read the pushbutton input pin:
  pulseGenState = digitalRead(A5 == HIGH);

  // compare the buttonState to its previous state
  if (pulseGenState != lastpulseGenState) {
    // if the state has changed, increment the counter
    if (pulseGenState == HIGH); 
      // if the current state is HIGH then the button
      // wend from off to on:
      button_count ++;
      int i;
      if(button_count == 10)button_count = 0;
      
      Serial.println(arraySelect);
      switch(button_count){
        case 0:
        val4 = seqArray[0];
        break;
        case 1:
        val4 = seqArray[1];
        break;
        case 2:
        val4 = seqArray[2];
        break;
        case 3:
        val4 = seqArray[3];
        break;
        case 4:
        val4 = seqArray[4];
        break;
        case 5:
        val4 = seqArray[5];
        break;
        case 6:
        val4 = seqArray[6];
        break;
        case 7:
        val4 = seqArray[7];
        break;
        case 8:
        val4 = seqArray[8];
        break;
        case 9:
        val4 = seqArray[9];
        break;

      }
    }

The seqArray values range from 2-27, and via "val4" they control two 4067 sixteen channel demultiplexers. I can input different values into each seqArray element. Thanks to SwingKing for pointing me in the right direction. I know all my code is severely bloated, so the next step is to go through and clean it up. For loops didn't work for the task at hand, but they'll sure eliminate a lot of unnecessary lines of code.

I was trying to do a lot of stuff with logic gates before, and found out that those ICs really add up fast. I thought that using microcontrollers would eliminate a lot of that, so I decided to start learning Arduino. A couple weeks later, I'm finding this to be true.