Read PIR and output keystrokes in sequence

Hi all,

I have a project where I have a PIR motion sensor looking for motion. When it's triggered, a random letter between A - L is sent as a keystroke. Otherwise, it keeps looking for motion.

I've been told that rather than having a random letter generate now, I need to have it send a letter as a keystroke from a list of letters in a sequence.

Please see the code I have that works for sending a random letter in the array when motion is detected. How would I change this to continue to look for motion and move down the list of letters in order? I will ultimately be putting a much longer series of letters in the array.

int PIR = 9;
const byte randomLetters = 20;
char* letters[randomLetters] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"};

void setup()
{
  pinMode(PIR, INPUT);
  Keyboard.begin();
  randomSeed(analogRead(0));
  Serial.begin(9600);
}
void loop()
{
  if (digitalRead(PIR) == HIGH)
  {
    Keyboard.print(letters[random(0,randomLetters)]);
    delay(2000);
  }
  else
  {
    Serial.println("No Motion");
  }
  delay(500);
}

Your question is more related to coding basics than to sensors.

You can e.g. have a global index variable, used to pick a character from the list, then increment the index, and restart from zero when it reaches the end of the array. Another variable is required to track the state of the sensor, so that only one character is output when the sensor starts detecting a move, then waits for no move before waiting again for the next move to start.