Teensy 3.2 Integers reading zero outside of void function.

Status
Not open for further replies.

cfredisded

Active member
Hi Guys,
I'm currently working on a project that will be a numbered keypad that sends out midi program changes. It will work like this:Hold a button down, then type in the program change number, then let go of held button, then each time button is short pressed send out the program change. Things were going smoothy until i ran into this issue that has been plaguing me for hours.
I'm using LiquidCrystal.h, keypad.h, and onebutton.h and I'm sure I'll be using the midi library once I get to that point. My issue is with globally declared integers returning to 0 after they are out of a void function. Here's my code:


Code:
// include the library code:
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <OneButton.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, DB4 = 10, DB5 = 9, DB6 = 8, DB7 = 7;
LiquidCrystal lcd(rs, en, DB4, DB5, DB6, DB7);


const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
byte rowPins[ROWS] = {16, 17, 18, 19}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {13, 14, 15}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// Setup a new OneButton on pin A6.  
OneButton button1(A6, true);
// Setup a new OneButton on pin A7.  
OneButton button2(A7, true);

  char prognum1;
  char prognum2;
  char prognum3;
  int midiprogrom1; 
  int midiprogrom2;
  int prognum11;
  int prognum22;
  int prognum33;


void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);
  // Print a message to the LCD.
  lcd.print("hell yeah, world!");

  // link the button 1 functions.
  button1.attachClick(click1);
  button1.attachDuringLongPress(longPress1);
  button1.attachLongPressStop(longPressStop1);

  // link the button 2 functions.
  button2.attachClick(click2);
  button2.attachDuringLongPress(longPress2);
  button2.attachLongPressStop(longPressStop2);

  Serial.begin(9600);

}

void loop() {
  // keep watching the push buttons:
  button1.tick();
  button2.tick();

  // You can implement other code in here or just wait a while 
  delay(10);
}

// This function will be called when the button1 was pressed 1 time (and no 2. button press followed).
void click1() {
  Serial.println("Button 1 click.");
} // click1

// This function will be called often, while the button1 is pressed for a long time.
void longPress1() {
  
Serial.println("Waiting for key1");
  char prognum1 = keypad.waitForKey();
  lcd.setCursor(1, 3);
  lcd.print(prognum1);
  Serial.println(prognum1);
  int prognum11=prognum1;

  
Serial.println("Waiting for key2");
  char prognum2 = keypad.waitForKey();
  lcd.setCursor(2, 3);
  lcd.print(prognum2);
  Serial.println(prognum2);

Serial.println("Waiting for key3");
  char prognum3 = keypad.waitForKey();
  lcd.setCursor(3, 3);
  lcd.print(prognum3);
  Serial.println(prognum3);
  static int midiprogrom2= 100*(prognum1- '0') + 10*(prognum2- '0') + prognum3 - '0';
  Serial.println(midiprogrom2);
} // longPress1


  
 // longPressStart1

// This function will be called once, when the button1 is released after beeing pressed for a long time.
void longPressStop1() {
  
  Serial.println("Button 1 longPress stop");

  Serial.println("midi program number =");

  Serial.println(midiprogrom2);
  
} // longPressStop1



// ... and the same for button 2:

void click2() {
  Serial.println("Button 2 click.");
} // click2


// This function will be called often, while the button1 is pressed for a long time.
void longPress2() {
  Serial.println("Button 1 longPress...");
} // longPress1


void longPressStop2() {
  Serial.println("Button 2 longPress stop");
} // longPressStop2

This is what shows in the serial monitor when I test it. You can see that the integer "midiprogrom2" becomes a 0 after it leaves the "void longPress1" function.

Code:
Waiting for key1
4
Waiting for key2
5
Waiting for key3
4
454
Button 1 longPress stop
midi program number =
0




I cant for the life of me figure out how to stop this. Am I going to need to write it to the eeprom to preserve the integer? Or is there something I've looked over?
I'm pretty new at this so thanks for any insight you all can give me.
 
The problem is that you have two variables called midiprogrom2. One is global, the other is declared static and local to the longPress1() function (it's irrelevant that it is declared be void).
Try changing this:
Code:
  static int midiprogrom2= 100*(prognum1- '0') + 10*(prognum2- '0') + prognum3 - '0';
to this
Code:
  midiprogrom2= 100*(prognum1- '0') + 10*(prognum2- '0') + prognum3 - '0';

You've done that with several other variables too - e.g. prognum1 etc.

Pete
 
Thanks a bunch Pete,
This did the trick. I only need to declare something an integers once. I was using int to refer to it didn't realize I was re-declaring it.
 
Status
Not open for further replies.
Back
Top