First steps towards using arrays and loops
This commit is contained in:
1 parent
d8dbfd78d3
commit
7947507853
1 file changed
+26
-36
+26
-36
@@ -21,17 +21,18 @@ int sensorOffsetY;
|
||||
// Position of the joystick when in use
|
||||
int sensorValueX;
|
||||
int sensorValueY;
|
||||
// Variables to set when one of the keys is currently pressed to later on release
|
||||
bool pressedKeyLeftArrow = false;
|
||||
bool pressedKeyRightArrow = false;
|
||||
bool pressedKeyUpArrow = false;
|
||||
bool pressedKeyDownArrow = false;
|
||||
// Variables to set when one of the keys is currently pressed to later on release again
|
||||
bool pressedKey[4] = {false, false, false, false};
|
||||
// Actual keys to press
|
||||
byte pressKey[4] = {0xD8, 0xD7, 0xDA, 0xD9}; // Left, Right, Up, Down
|
||||
// Cycle count to wait before next key press
|
||||
int waitValueX = 0;
|
||||
int waitValueY = 0;
|
||||
// Variable to save the new cycle count value to evaluate against the current one
|
||||
// Variables to save the new cycle count value to evaluate against the current one
|
||||
int waitValueXNew = 0;
|
||||
int waitValueYNew = 0;
|
||||
// Value to use for while loops
|
||||
int i = 0;
|
||||
|
||||
// the setup function runs once when you press reset or power the board
|
||||
void setup()
|
||||
@@ -62,9 +63,9 @@ void loop()
|
||||
if (waitValueX == 0)
|
||||
{
|
||||
// Press the left arrow key
|
||||
Keyboard.press(0xD8);
|
||||
Keyboard.press(pressKey[0]);
|
||||
// Set a variable that the left arrow key is currently pressed
|
||||
pressedKeyLeftArrow = true;
|
||||
pressedKey[0] = true;
|
||||
// Update the cycls wait count with the new value
|
||||
waitValueX = waitValueXNew;
|
||||
}
|
||||
@@ -85,9 +86,9 @@ void loop()
|
||||
if (waitValueX == 0)
|
||||
{
|
||||
// Press the right arrow key
|
||||
Keyboard.press(0xD7);
|
||||
Keyboard.press(pressKey[1]);
|
||||
// Set a variable that the right arrow key is currently pressed
|
||||
pressedKeyRightArrow = true;
|
||||
pressedKey[1] = true;
|
||||
// Update the cycls wait count with the new value
|
||||
waitValueX = waitValueXNew;
|
||||
}
|
||||
@@ -115,9 +116,9 @@ void loop()
|
||||
if (waitValueY == 0)
|
||||
{
|
||||
// Press the up arrow key
|
||||
Keyboard.press(0xDA);
|
||||
Keyboard.press(pressKey[2]);
|
||||
// Set a variable that the up arrow key is currently pressed
|
||||
pressedKeyUpArrow = true;
|
||||
pressedKey[2] = true;
|
||||
// Update the cycls wait count with the new value
|
||||
waitValueY = waitValueYNew;
|
||||
}
|
||||
@@ -138,9 +139,9 @@ void loop()
|
||||
if (waitValueY == 0)
|
||||
{
|
||||
// Press the down arrow key
|
||||
Keyboard.press(0xD9);
|
||||
Keyboard.press(pressKey[3]);
|
||||
// Set a variable that the down arrow key is currently pressed
|
||||
pressedKeyDownArrow = true;
|
||||
pressedKey[3] = true;
|
||||
// Update the cycls wait count with the new value
|
||||
waitValueY = waitValueYNew;
|
||||
}
|
||||
@@ -160,29 +161,18 @@ void loop()
|
||||
// Time to wait before keys will be released (if needed)
|
||||
delay(DELAY_BETWEEN_KEYS);
|
||||
|
||||
// Release left arrow key if pressed
|
||||
if (pressedKeyLeftArrow == true)
|
||||
// Release keys if pressed
|
||||
i = 0;
|
||||
while (i < 4)
|
||||
{
|
||||
Keyboard.release(0xD8);
|
||||
pressedKeyLeftArrow = false;
|
||||
}
|
||||
// Release right arrow key if pressed
|
||||
if (pressedKeyRightArrow == true)
|
||||
{
|
||||
Keyboard.release(0xD7);
|
||||
pressedKeyRightArrow = false;
|
||||
}
|
||||
// Release up arrow key if pressed
|
||||
if (pressedKeyUpArrow == true)
|
||||
{
|
||||
Keyboard.release(0xDA);
|
||||
pressedKeyUpArrow = false;
|
||||
}
|
||||
// Release down arrow key if pressed
|
||||
if (pressedKeyDownArrow == true)
|
||||
{
|
||||
Keyboard.release(0xD9);
|
||||
pressedKeyDownArrow = false;
|
||||
// Check if key is currently pressed
|
||||
if (pressedKey[i] == true)
|
||||
{
|
||||
// Release the key
|
||||
Keyboard.release(pressKey[i]);
|
||||
pressedKey[i] = false;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
// Lower the wait cycles for the X axis if not lower than 0
|
||||
|
||||
Reference in new issue
Block a user