-1

I have an analog light sensor working on my Micro Pro Leonardo board. I want to reach a part of my code which include keyboard presses where it monitors to see when an LED flashes and then continues the code from there.

Could someone give me a code example of how I can do that?

Currently I'd have something like....

  Keyboard.write(KEY_RETURN);
  delay(800);
  Keyboard.write(KEY_TAB);
  delay(150);
  Keyboard.write(KEY_TAB);
  delay(150);
  Keyboard.write(' ');
    }

//Code here to wait until LED flashes, registers as 30 or less on the light sensor. Then carry on with code below once this happens

  BootKeyboard.press(KEY_LEFT_GUI);
  BootKeyboard.press('r');
  delay(40000);
  BootKeyboard.releaseAll();

etc

I know the light sensor is AnalogValue = analogRead(A0); and I assume it's some kind of "If" statement which loops until the value because less than what is defined but have no idea how this would be written.

  • 1
    Generally you want to avoid delay, and avoid waiting until some external event happens. Both of those things completely lock up the controller until the condition is satisfied. Instead you usually want to check state flags in your loop function, and/or use millis() to see if enough time has transpired. – Duncan C Mar 5 at 16:08
  • I know but for a beginner with a script which has about 150 button presses that all have specific and different wait times between each key press, it seems the easiest way to do it. At some point I will revisit the code so I can add a pause button if required, but at the moment it's not the question in hand. – Danny Shepherd Mar 6 at 14:08
1

One method:

while (analogRead(A0) < setting){ // sit in this loop taking readings until get one that is >= to setting }

  • Thanks @CrossRoads i'll give this I shot, I read it should be a while setting and that < would be used with my number but didn't know how to actually structure this into a line. – Danny Shepherd Mar 5 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.