Search code examples
carduinomicrocontrollerarduino-unoatmega

How do I assign a value to a variable once?


I need to assign a one-time value to two variables. 'keypressed' when the button is pressed, 'keyrelesed' when it is released. The rest of the time they should both have the value 'nokey'.When it is output, it should look like this.

keypressed = nokey                           //button at rest
keyrelesed = nokey

keypressed = "The value of the pressed button"     //button is pressed
keyrelesed = nokey

keypressed = nokey                           //button continues to be pressed
keyrelesed = nokey

nokey = keypressed
keyrelesed = "The value of the depressed button"     //button is released

keypressed = nokey
keyrelesed = nokey

It turns out that "The value of pressed button" = "The value of depressed button".

The way of accepting the value of the button.

 nokey = 255;
 byte keyreleased = nokey;
 byte keypressed = nokey;
 byte lastKey = keypressed;
void loop() {
int analogVal = analogRead(A0);               //read analog voltage value from pin A0
  if (analogVal < 325) keypressed = instrkey; //Correspondence of value and variable
  if (analogVal < 300) keypressed  = keyB4;
  if (analogVal < 275) keypressed  = keyA4s;
  if (analogVal < 250) keypressed  = keyA4;
  if (analogVal < 225) keypressed  = keyG4s;
  if (analogVal < 200) keypressed  = keyG4;
  if (analogVal < 175) keypressed  = keyF4s;
  if (analogVal < 150) keypressed  = keyF4;
  if (analogVal < 125) keypressed  = keyE4;
  if (analogVal < 100) keypressed  = keyD4s;
  if (analogVal < 75) keypressed  = keyD4;
  if (analogVal < 50) keypressed  = keyC4s;
  if (analogVal < 25) keypressed  = keyC4;
  if (analogVal > 1000) keypressed  = nokey;

  if (lastKey != keypressed) {               //button release detection
    keyreleased=lastKey;
    lastKey=keypressed;
  }
}

The "lastkey" variable does not have to be used.

I tried to explain as best I could.


Solution

  • You could use an extra variable: currentKey:

    void loop() {
      int analogVal = analogRead(A0);               //read analog voltage value from pin A0
      byte currentKey = nokey;
    
      if (analogVal < 325) currentKey = instrkey; //Correspondence of value and variable
      if (analogVal < 300) currentKey  = keyB4;
      if (analogVal < 275) currentKey  = keyA4s;
      if (analogVal < 250) currentKey  = keyA4;
      if (analogVal < 225) currentKey  = keyG4s;
      if (analogVal < 200) currentKey  = keyG4;
      if (analogVal < 175) currentKey  = keyF4s;
      if (analogVal < 150) currentKey  = keyF4;
      if (analogVal < 125) currentKey  = keyE4;
      if (analogVal < 100) currentKey  = keyD4s;
      if (analogVal < 75) currentKey  = keyD4;
      if (analogVal < 50) currentKey  = keyC4s;
      if (analogVal < 25) currentKey  = keyC4;
      if (analogVal > 1000) currentKey  = nokey;
    
      if (lastKey != currentKey) {  // Change detected
        keyreleased=lastKey;
        keypressed=currentKey;
        lastKey=currentKey;
      } else {  // No change detected
        keyreleased=nokey;
        keypressed=nokey;
      }
    }
    

    (You should also do something for analogVal in the range 326 to 1000. EDIT: OP says there will be no such values.)

    The keypressed, keyreleased and lastKey variables are all initialized to nokey before the first update cycle.

    The function loop() is called during each update cycle. It sets local variable currentKey to the current key press state. If a key is currently pressed (determined by a single analog voltage level, so only one key at a time can be pressed), currentKey gets set to the code for that key, otherwise it gets set to nokey to indicate that no key is currently pressed. The lastKey variable is assumed to be set to the value of currentKey from the previous update cycle, but will be nokey for the first update cycle.

    The following conditions and actions can occur when currentKey is updated:

    1. lastKey == currentKey && currentKey == nokey — no change; no key is pressed (i.e. nokeynokey)
      • Set:
        keyreleased=nokey;
        keypressed=nokey;
        
        because there is no change.
    2. lastKey == currentKey && currentKey != nokey — no change; previous key is still pressed (e.g. keyXkeyX)
      • Set:
        keyreleased=nokey;
        keypressed=nokey;
        
        because there is no change.
    3. lastKey != currentKey && lastKey == nokey — change from no key pressed to some key pressed (e.g. nokeykeyX)
      • Set:
        keyreleased=lastKey;   /*=nokey*/
        keypressed=currentKey; /*=keyX*/
        
        because something has changed.
    4. lastKey != currentKey && currentKey == nokey — change from some key pressed to no key pressed (e.g. keyXnokey)
      • Set:
        keyreleased=lastKey;   /*=keyX*/
        keypressed=currentKey; /*=nokey*/
        
        because something has changed.
    5. lastKey != currentKey && lastKey != nokey && currentKey != nokey — change from some key pressed to some other key pressed (e.g. keyXkeyY)
      • Set:
        keyreleased=lastKey;   /*=keyX*/
        keypressed=currentKey; /*=keyY*/;
        
        because something has changed.

    As can be seen, the code for cases 1 and 2 (no change) can be merged together, and the code for cases 3, 4 and 5 (some change) can be merged together.

    lastKey is then set to the value of currentKey ready for the next update cycle.