Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Trouble incrementing an array... attempting to make tapped patterns.

  1. #1

    Trouble incrementing an array... attempting to make tapped patterns.

    Hi all,
    My goal with this sketch is to have two buttons. Holding one button while tapping on the other creates an array full of millisecond intervals. I can't for the life of me figure out why the array 'slot' starts on 2 instead of 1 and seems to switch between 2 and 3 instead of incrementing all the way to 40. I'm pretty new to arrays so I'm hoping its just something simple I'm overlooking. Any help would be much appreciated.
    Code:
    #include <Bounce.h>
    
    const int tapTEMPOPin = 49;
    const int rndmBUTTONpin = 8;
    bool rndmBUTTONheld;
    unsigned long currentMILLIS;
    unsigned long fallingEDGEtime;
    unsigned long risingEDGEtime;
    int currentPATTERNarraySLOT;
    
    
    Bounce tapTEMPObutton = Bounce(tapTEMPOPin, 40);  // 40 ms debounce
    Bounce rndmBUTTON = Bounce(rndmBUTTONpin, 40);  // 40 ms debounce
    
    float tappedPATTERNarray[40];
    
    int LFOled = 10;
    
    void setup() {
      // put your setup code here, to run once:
    
    Serial.begin(9600);
    pinMode(LFOled, OUTPUT); 
    pinMode(tapTEMPOPin, INPUT_PULLUP);
    pinMode(rndmBUTTONpin, INPUT_PULLUP);
    rndmBUTTONheld = false;
    currentPATTERNarraySLOT = 1;
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    currentMILLIS = millis();
    if (tapTEMPObutton.update()) {
      if (tapTEMPObutton.fallingEdge()==true && rndmBUTTONheld == true) {
        fallingEDGEtime = millis();
        if (currentPATTERNarraySLOT > 1){
          float MILLIStoPLACEinARRAYslot = fallingEDGEtime - risingEDGEtime;
          tappedPATTERNarray[currentPATTERNarraySLOT] = MILLIStoPLACEinARRAYslot;
          currentPATTERNarraySLOT = (currentPATTERNarraySLOT+1);
          Serial.print(MILLIStoPLACEinARRAYslot);
          Serial.print(" MILLIS ADDED TO SLOT ");
          Serial.print(currentPATTERNarraySLOT);
          Serial.println();
        }
      }
      
      if (tapTEMPObutton.risingEdge()==true && rndmBUTTONheld == true) {
        risingEDGEtime = millis();
        if (currentPATTERNarraySLOT = 1 || currentPATTERNarraySLOT <= 39) {
          float MILLIStoPLACEinARRAYslot = risingEDGEtime - fallingEDGEtime;
          tappedPATTERNarray[currentPATTERNarraySLOT] = MILLIStoPLACEinARRAYslot;
          currentPATTERNarraySLOT = (currentPATTERNarraySLOT+1);
          Serial.print(MILLIStoPLACEinARRAYslot);
          Serial.print(" MILLIS ADDED TO SLOT ");
          Serial.print(currentPATTERNarraySLOT);
          Serial.println();
        }    
      }  
     }
    
    //if array is more the 40 go back to slot 1 
    if (currentPATTERNarraySLOT > 40){
     currentPATTERNarraySLOT = 1;
    }
    
    if (rndmBUTTON.update()) {
      if (rndmBUTTON.fallingEdge()) {
        rndmBUTTONheld = true;
        currentPATTERNarraySLOT = 1;
        
       }
      if (rndmBUTTON.risingEdge()) {
        rndmBUTTONheld = false;
        //fill rest of array with zeros
        for (int i= currentPATTERNarraySLOT; i < 40; i ++) {
          tappedPATTERNarray[currentPATTERNarraySLOT] = 0;
          currentPATTERNarraySLOT = currentPATTERNarraySLOT + 1;
        }
       }
     } 
    }
    Pic of what the serial monitor looks like while holding rndmButton and pressing tapTEMPObutton.
    https://imgur.com/HCMp6C7

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,842
    First quick note: Arrays in c start with index 0.

    So valid indexes for :: float tappedPATTERNarray[40];

    would be [0] to [39] and this would be right:
    Code:
    if (currentPATTERNarraySLOT > 39){
     currentPATTERNarraySLOT = 0;
    }
    And this is not right. It is the only increment of currentPATTERNarraySLOT. normally in this case 'i' would be used where currentPATTERNarraySLOT is?
    Code:
        for (int i= currentPATTERNarraySLOT; i < 40; i ++) {
          tappedPATTERNarray[currentPATTERNarraySLOT] = 0;
          currentPATTERNarraySLOT = currentPATTERNarraySLOT + 1;
        }
    And somewhere when desired the variable currentPATTERNarraySLOT should receive an expected regular update. Usually before the test where it would be >39 in this case.

  3. #3
    Thanks for the clarification. I kinda assumed at first that it should be 0-39 but was confused by the readings. i fixed the code and added a troubleshootINT just to see if I was messing up the currentPATTERNarraySLOT somewhere. Here's the updated code:
    Code:
    #include <Bounce.h>
    
    const int tapTEMPOPin = 49;
    const int rndmBUTTONpin = 8;
    bool rndmBUTTONheld;
    unsigned long currentMILLIS;
    unsigned long fallingEDGEtime;
    unsigned long risingEDGEtime;
    int currentPATTERNarraySLOT;
    int troubleshootINT;
    
    
    Bounce tapTEMPObutton = Bounce(tapTEMPOPin, 40);  // 40 ms debounce
    Bounce rndmBUTTON = Bounce(rndmBUTTONpin, 40);  // 40 ms debounce
    
    float tappedPATTERNarray[40];
    
    int LFOled = 10;
    
    void setup() {
      // put your setup code here, to run once:
    
    Serial.begin(9600);
    pinMode(LFOled, OUTPUT); 
    pinMode(tapTEMPOPin, INPUT_PULLUP);
    pinMode(rndmBUTTONpin, INPUT_PULLUP);
    rndmBUTTONheld = false;
    currentPATTERNarraySLOT = 0;
    troubleshootINT = 0;
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    currentMILLIS = millis();
    if (tapTEMPObutton.update()) {
      if (tapTEMPObutton.fallingEdge()==true && rndmBUTTONheld == true) {
        fallingEDGEtime = millis();
        if (currentPATTERNarraySLOT > 0){
          float MILLIStoPLACEinARRAYslot = fallingEDGEtime - risingEDGEtime;
          tappedPATTERNarray[currentPATTERNarraySLOT] = MILLIStoPLACEinARRAYslot;
          currentPATTERNarraySLOT = currentPATTERNarraySLOT+1;
          troubleshootINT = troubleshootINT + 1;
          Serial.print(MILLIStoPLACEinARRAYslot);
          Serial.print(" MILLIS ADDED TO SLOT ");
          Serial.print(currentPATTERNarraySLOT);
          Serial.println();
          Serial.print(troubleshootINT);
          Serial.println();
        }
      }
      
      if (tapTEMPObutton.risingEdge()==true && rndmBUTTONheld == true) {
        risingEDGEtime = millis();
        if (currentPATTERNarraySLOT = 0 || currentPATTERNarraySLOT <= 39) {
          float MILLIStoPLACEinARRAYslot = risingEDGEtime - fallingEDGEtime;
          tappedPATTERNarray[currentPATTERNarraySLOT] = MILLIStoPLACEinARRAYslot;
          currentPATTERNarraySLOT = currentPATTERNarraySLOT+1;
          troubleshootINT = troubleshootINT + 1;
          Serial.print(MILLIStoPLACEinARRAYslot);
          Serial.print(" MILLIS ADDED TO SLOT ");
          Serial.print(currentPATTERNarraySLOT);
          Serial.println();
          Serial.print("NUMBER OF BUTTON TRANSITIONS ");
          Serial.print(troubleshootINT);
          Serial.println();
        }    
      }  
     }
    
    //if array is more the 40 go back to slot 1 
    if (currentPATTERNarraySLOT > 39 || troubleshootINT > 39){
     currentPATTERNarraySLOT = 0;
     troubleshootINT = 0;
    }
    
    if (rndmBUTTON.update()) {
      if (rndmBUTTON.fallingEdge()) {
        rndmBUTTONheld = true;
        currentPATTERNarraySLOT = 0;
        troubleshootINT = 0;
        
       }
      if (rndmBUTTON.risingEdge()) {
        rndmBUTTONheld = false;
        //fill rest of array with zeros
        for (int i = (currentPATTERNarraySLOT+1); i <= 39; i ++) {
          tappedPATTERNarray[i] = 0;
          Serial.println(i);
        }
       }
     } 
    }
    Could you clarify what you mean by..
    And somewhere when desired the variable currentPATTERNarraySLOT should receive an expected regular update. Usually before the test where it would be >39 in this case.
    Thanks for the help @defragster

    oh and *edit here's what the serial moniter gets....
    https://imgur.com/mhgJMjR
    Last edited by cfredisded; 10-23-2018 at 04:15 AM.

  4. #4
    Okay so I'm still having issues with this but I think I've narrowed it down.. but still have no idea why its acting so strange. The variable that should be incrementing from 0-39 gets stuck switching between 2 and 3 depending on which variable i use for the if statement. Whichever one I use for the if statements gets stuck between 2 and 3 and the other variable acts normal rising to 39 and reseting to 0.

    code snippet
    Code:
      if (tapTEMPObutton.risingEdge()==true && rndmBUTTONheld == true) {
        risingEDGEtime = millis();
        if (troubleshootINT = 0 || troubleshootINT <= 39) { // <----Switching the variable troubleshootINT to arraySLOT here makes array slot get stuck between 2 and 3
          float MILLIStoPLACEinARRAYslot = risingEDGEtime - fallingEDGEtime;
          tappedPATTERNarray[arraySLOT] = MILLIStoPLACEinARRAYslot;
          arraySLOT = arraySLOT+1;
          troubleshootINT = troubleshootINT + 1;
          Serial.print(MILLIStoPLACEinARRAYslot);
          Serial.print(" MILLIS ADDED TO SLOT ");
          Serial.print(arraySLOT);
          Serial.println();
          Serial.print("NUMBER OF BUTTON TRANSITIONS ");
          Serial.print(troubleshootINT);
          Serial.println();
        }    
      }

    Full code
    Code:
    #include <Bounce.h>
    
    const int tapTEMPOPin = 49;
    const int rndmBUTTONpin = 8;
    bool rndmBUTTONheld;
    unsigned long currentMILLIS;
    unsigned long fallingEDGEtime;
    unsigned long risingEDGEtime;
    int arraySLOT;
    int troubleshootINT;
    
    
    Bounce tapTEMPObutton = Bounce(tapTEMPOPin, 40);  // 40 ms debounce
    Bounce rndmBUTTON = Bounce(rndmBUTTONpin, 40);  // 40 ms debounce
    
    float tappedPATTERNarray[40];
    
    int LFOled = 10;
    
    void setup() {
      // put your setup code here, to run once:
    
    Serial.begin(9600);
    pinMode(LFOled, OUTPUT); 
    pinMode(tapTEMPOPin, INPUT_PULLUP);
    pinMode(rndmBUTTONpin, INPUT_PULLUP);
    rndmBUTTONheld = false;
    arraySLOT = 0;
    troubleshootINT = 0;
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    currentMILLIS = millis();
    if (tapTEMPObutton.update()) {
      if (tapTEMPObutton.fallingEdge()==true && rndmBUTTONheld == true) {
        fallingEDGEtime = millis();
        if (arraySLOT > 0){
          float MILLIStoPLACEinARRAYslot = fallingEDGEtime - risingEDGEtime;
          tappedPATTERNarray[arraySLOT] = MILLIStoPLACEinARRAYslot;
          arraySLOT = arraySLOT+1;
          troubleshootINT = troubleshootINT + 1;
          Serial.print(MILLIStoPLACEinARRAYslot);
          Serial.print(" MILLIS ADDED TO SLOT ");
          Serial.print(arraySLOT);
          Serial.println();
          Serial.print("NUMBER OF BUTTON TRANSITIONS ");
          Serial.print(troubleshootINT);
          Serial.println();
        }
      }
      
      if (tapTEMPObutton.risingEdge()==true && rndmBUTTONheld == true) {
        risingEDGEtime = millis();
        if (troubleshootINT = 0 || troubleshootINT <= 39) { // <----Switching the variable troubleshootINT to arraySLOT here makes array slot get stuck between 2 and 3
          float MILLIStoPLACEinARRAYslot = risingEDGEtime - fallingEDGEtime;
          tappedPATTERNarray[arraySLOT] = MILLIStoPLACEinARRAYslot;
          arraySLOT = arraySLOT+1;
          troubleshootINT = troubleshootINT + 1;
          Serial.print(MILLIStoPLACEinARRAYslot);
          Serial.print(" MILLIS ADDED TO SLOT ");
          Serial.print(arraySLOT);
          Serial.println();
          Serial.print("NUMBER OF BUTTON TRANSITIONS ");
          Serial.print(troubleshootINT);
          Serial.println();
        }    
      }  
     }
    
    //if array is more the 39 go back to slot 0 
    if (arraySLOT > 39 || troubleshootINT > 39){
     arraySLOT = 0;
     troubleshootINT = 0;
    }
    
    if (rndmBUTTON.update()) {
      if (rndmBUTTON.fallingEdge()) {
        rndmBUTTONheld = true;
        arraySLOT = 0;
        troubleshootINT = 0;
        
       }
      if (rndmBUTTON.risingEdge()) {
        rndmBUTTONheld = false;
        //fill rest of array with zeros
        for (int i = (troubleshootINT+1); i <= 39; i ++) {
          tappedPATTERNarray[i] = 0;
          Serial.println(i);
        }
       }
     } 
    }
    EDIT* it seems like getting rid of the 'troubleshootINT = 0 ||' in that if statement fixes it and it works properly... no idea why. but the code should work now.

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,842
    Just seeing this again - glad you got it working!

    RE : "And somewhere when desired the variable currentPATTERNarraySLOT should receive an expected regular update. Usually before the test where it would be >39 in this case."

    It seems you solved that. The reason it was stuck at 2 or 3 I assume was because there was not a clear condition where currentPATTERNarraySLOT would get updated based on the desired user/system input.

    That is - in general terms I didn't see a clear way the 'new element' index was incremented, and then checked before it could be used if it went > 39:
    Code:
    If ( "time for a new element" ) {
      currentPATTERNarraySLOT += 1
      if ( currentPATTERNarraySLOT > 39 ) 
        currentPATTERNarraySLOT = 0
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •