Min menu

Pages

Project 31 : Keyboard Message

 Console Message 


At the point when the catch is squeezed in this model, a content string is shipped off the PC as console input. The string reports the occasions the catch has been squeezed. When you have the Leonardo customized and wired up, open up your number one content tool to see the outcomes. 


NB: When you utilize the Keyboard.print() order, the Arduino assumes control over your PC's console! To guarantee you don't lose control of your PC while running a sketch with this capacity, try to set up a dependable control framework before you call Keyboard.print(). This sketch incorporates a pushbutton to flip the console, so it just pursues the catch is squeezed. 


Equipment Required 


Arduino Leonardo, Micro, or Due board 


transient pushbutton 


10k ohm resistor 


Programming Required 


Any content tool 


Circuit 


Join one pin of the pushbutton to nail 4 to the Arduino. Connect the other pin to 5V. Utilize the resistor as a draw down, giving a reference to ground, by appending it from pin 4 to ground. 


Whenever you've modified your board, unplug the USB link, open a content manager and put the content cursor at in the composing region. Associate the board to your PC through USB again and press the catch to write in the record.

Circuit & shematics :




Code

/* 
 Keyboard Message test 

 For the Arduino Leonardo and Micro. 

 Sends a text string when a button is pressed. 

 The circuit: 
 * pushbutton attached from pin 4 to +5V 
 * 10-kilohm resistor attached from pin 4 to ground 

 created 24 Oct 2011 
 modified 27 Mar 2012 
 by Tom Igoe 
 modified 11 Nov 2013 
 by Scott Fitzgerald 

 This example code is in the public domain. 

 http://www.arduino.cc/en/Tutorial/KeyboardMessage
 */ 


#include "Keyboard.h" 
#include "HID.h" 

const int buttonPin           // input pin for pushbutton 
int previousButtonState HIGH   // for checking the state of a pushButton 
int counter                   // button push counter 

void setup 
  // make the pushButton pin an input: 
  pinMode buttonPin INPUT 
  // initialize control over the keyboard: 
  Keyboard. begin 


void loop 
  // read the pushbutton: 
  int buttonState digitalRead buttonPin 
  // if the button state has changed, 
  if buttonState != previousButtonState 
      // and it's currently pressed: 
      && buttonState == HIGH 
    // increment the button counter 
    counter ++; 
    // type out a message 
    Keyboard. print "You pressed the button " )
    Keyboard. print counter 
    Keyboard. println " times." 
  
  // save the current button state for comparison next time: 
  previousButtonState buttonState 
}
reaction: