Go Down

Topic: How to require a key-up event before continuing (Read 1 time) previous topic - next topic

roggesound

Jul 08, 2011, 09:47 pm Last Edit: Jul 08, 2011, 09:49 pm by roggesound Reason: 1
So I've made a box with a "Go" button on it to fire a show control program on my computer.  Unfortunately, if the button is held down, the signal is repeated after the delay.  What I'd like to do is fire the signal once and have the program wait until it sees the button go open before allowing another signal to fire.  

Here's my current code.  The board is a Teensy 2.0.  Any help you can give will be much appreciated.  Thank you.

Code: [Select]
void loop() {
  if (digitalRead(10) == HIGH) {
    delay(10);
  } else {
    Keyboard.print(" "); // print a space (GO)
    delay(1000);
  }



flyboy

You could use something like this to determine if the go button is not released.  Once the button is released, it changes the value of go_button_pressed.  When the value goes from 1 to 0, the program starts on your PC.

Code: [Select]
              if((go == LOW) && (last_go == LOW))
                  {
                     go_button_pressed = 1;
                  }

              if((go == HIGH) && (last_go == LOW))
                  {
                     go_button_pressed = 0;
                  }

roggesound

I should probably have mentioned that I'm also relatively an idiot when it comes to programming.  I understand what my simple code does, but when it gets more complicated than that, i don't know what's going on...  And the code must ultimately print a space, but not print a space again until after the button comes up. 

robtillaart


Give the variables a meaningfull name makes the program more "English" readable. That makes programming easier.

Code: [Select]

int spacesAllowed = 1;

if (digitalRead(10) == LOW && spacesAllowed > 0)
{
  Keyboard.print(" ");
  spacesAllowed = 0;  // no  spaces allowed anymore
}

if (digitalRead(10) == HIGH)
{
  spacesAllowed = 1;  // butto is up again
}

 
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

roggesound

Thank you.  That's much easier for me to understand.  I even understand why it should work and the logic of the program...  but it doesn't work somehow.  I added a 1 second delay, which slows it down a bit, but when that's not in there, it just prints spaces very quickly when the button is held down.  With the delay, it prints one space per second.

Code: [Select]
void setup() {
  Serial.begin(9600);
  pinMode(10, INPUT_PULLUP);
  delay(4000);
}

void loop() {
  int spacesAllowed = 1;
  if (digitalRead(10) == LOW && spacesAllowed > 0)
  {
    Keyboard.print(" ");
    spacesAllowed = 0;  // no  spaces allowed anymore
    delay(1000);
  }
  if (digitalRead(10) == HIGH)
  {
    spacesAllowed = 1;  // button is up again
  }
}

Go Up