0

I have a small project that I have in mind and I'm pretty new with Arduino.

I want to make a small device that if a key was pressed it would do something.

Now I know I could use a button with the device but I want to specifically use a Cherry MX Blue Switch.

So I wanted to know if the test code I wrote up would get me in the right stating place and I want to know where I would wire the key switch onto the board.

From how I have the code written I'm guessing I connect the positive pin from the switch to P0 on the board and the other pin to ground on the board, is this correct?

#include "DigiKeyboard.h"

void setup() {
    pinMode(0, INPUT);
}

void loop() {
    // put your main code here, to run repeatedly:
    if(digitalRead(0)==HIGH){
        DigiKeyboard.sendKeyStroke(MOD_GUI_LEFT,KEY_R);
        DigiKeyboard.print("test");
    }
}
3
  • 1
    Try Googling: "Arduino INPUT_PULLUP", "Arduino button edge detection" and "Arduino button debounce".
    – tttapa
    Dec 23, 2017 at 15:50
  • is there something special about cherry mx blue switch? ... i thought that it is just a SPST switch
    – jsotola
    Dec 23, 2017 at 20:02
  • I just really like cherry mix blue switches tbh
    – MintCollie
    Dec 24, 2017 at 0:36

2 Answers 2

1

I would suggest to connect one pin of the with to 5v and the other to ground. The Arduino will be able to detect 5v as logic HIGH.

The code looks correct except for better programming technique I would suggest to indent you code, this make debugging easier for longer programs:

#include "DigiKeyboard.h"

void setup() {

   pinMode(0, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:

  if(digitalRead(0)==HIGH){

    DigiKeyboard.sendKeyStroke(MOD_GUI_LEFT,KEY_R);
    DigiKeyboard.print("test");

  }
}
6
  • so should i connect it to pin 0 and 5v? since in the code its telling it to look at pin 0.
    – MintCollie
    Dec 23, 2017 at 17:55
  • The pin which you originally connected to gnd connect to 5v
    – Gorria24
    Dec 23, 2017 at 23:35
  • So so the switch would be connected to 5v & p0?
    – MintCollie
    Dec 24, 2017 at 0:39
  • Yes, this means when the switch is closed p0 will be able to detect 5v as login high
    – Gorria24
    Dec 24, 2017 at 10:48
  • 2
    This wont' work unless you add an external pulldown resistor to GND. Connecting the switch between the digital input and GND has the advantage that you don't need an external resistor, as you can use the internal pullup by setting the pin mode toINPUT_PULLUP. This was already suggested by @tttapa in his comment. Dec 24, 2017 at 11:22
0

Do you want the absolute lowest power consumption? If yes then instead of using a microcontroller, use a dual D type latch. The circuit below will take the first break of the optical beam, turn on the output for that latch, and will block the other latch from turning on.

When the clock input goes high (beam broken) the high on the D input is transferred to Q and -Q will go low. That low will force the output of the other latch to stay low.

To reset the light the push button switch is used to clear the Q output. It is then ready again.

I didn't show the power supply (bypass cap). The chip is a CD74HC74. 14 pins containing two latches.The only current drawn is the LED current and the current of the opto-transistors.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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