Arduino Leonardo keyboard behavior - keyboard

I want to use the Arduino Leonardo as a keyboard input with the inbuilt library.
boolean on;
void setup() {
pinMode(13, OUTPUT);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
Keyboard.begin();
on = true;
}
void loop() {
if(digitalRead(2) == LOW) {
Keyboard.end();
on = false;
}
digitalWrite(13, on);
if(digitalRead(3) == LOW) {
Keyboard.press('w');
}
else {
Keyboard.release('w');
}
}
Is it normal that the "Keyboard.press()" function still works after I ran "Keyboard.end()"?
I just don't want the Arduino to break.

You have used the begin() function in:
void setup()
{
pinMode(13, OUTPUT);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
Keyboard.begin();
on = true;
}
And you have ended the communication in
if(digitalRead(2) == LOW) {
Keyboard.end();
on = false;
}
Once the digitalRead(2) value goes low, the communication protocol is stopped. You have to use another Keyboard.begin() before:
if(digitalRead(3) == LOW)
{
Keyboard.press('w');
}
The best practice is to use Keyboard.begin(); within void loop().

By the looks of what you're trying to do, you should do this:
#include <Keyboard.h>
boolean on;
boolean disable;
void setup() {
pinMode(13, OUTPUT);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
disable = false;
on = true;
}
void loop() {
if(digitalRead(2) == LOW) {
disable = true;
on = false;
}
digitalWrite(13, on);
if(disable == false) {
if(digitalRead(3) == LOW) {
Keyboard.press('w');
}
else {
Keyboard.release('w');
}
}
}
That #include at the start I'm using is because I'm using the latest version of Arduino IDE.
Anyway, no matter if you are on your version or the latest version of Arduino IDE, Keyboard.begin() and Keyboard.end() do nothing, as I just tested it on my Arduino Leonardo-like board (I'm using Leostick instead of the genuine Arduino).
I'm not sure if I'm entirely right. It could depend on different operating systems (even though nothing went wrong when I tried both Windows and Mac). In all honesty, I'd leave them there just to be on the safe side, also to make your code easier to understand.
By the way, you won't break your Arduino.

Keyboard begin and end do not currently do anything and omitting them should not affect your code. If you go to the Keyboard Library Github repo and check the begin and end functions, you'll find them empty
void Keyboard_::begin(void)
{
}
void Keyboard_::end(void)
{
}

Related

scanning the barcode and control led

I have a serial barcode scanner and Arduino Uno.
I need to configuration..one Correct barcode scanning led on.
my code is
void setup ()
{
Serial.begin (9600);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop ()
{
if(Serial.available())
{
String command =Serial.readStringUntil('\n');
Serial.println(command);
if(command =="1010007")
{
digitalWrite(3, HIGH);
digitalWrite(4,LOW);}
else
if(command !="1010007")
{digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(3,LOW);}
}}
im scanning barcode 1010007 it showing serial monitor g⸮⸮⸮⸮ like this
how to fix it
plz help
and i try another code it showing correctly barcode,,,,,i scan 1010007 serial monitor also same 1010007
but led not on...code is below,,,
#include <SoftwareSerial.h>
SoftwareSerial barcode = SoftwareSerial(2, 3, true); // RX, TX
void setup()
{
Serial.begin(9600);
pinMode(3, OUTPUT);
barcode.begin(9600);
delay(1000);
}
void loop()
{
if(barcode.available())
{
char data = barcode.read();
Serial.write(data);
if(data=="101007")
{
digitalWrite(3, HIGH);
}
}}

Bluetooth Programming with the MSP-EXP432P401R

I am trying to connect my MSP432 to my HC-05 Bluetooth module however when I connect to the module using a Bluetooth terminal, I receive no feedback. I've attached my code as well. Is the RX pin P3.2 and TX P3.3?
void setup() {
Serial.begin(9600); /* Define baud rate for serial communication */
pinMode(12, OUTPUT);
}
void loop() {
if (Serial.available()) /* If data is available on serial port */
{
char data_received;
data_received = Serial.read(); /* Data received from bluetooth */
if (data_received == '1')
{
digitalWrite(12, HIGH);
Serial.write("LED turned ON");
}
else if (data_received == '2')
{
digitalWrite(12, LOW);
Serial.write("LED turned OFF");
}
else
{
Serial.write("Select either 1 or 2");
}
}
}

PySerial Arduino receives data incorrectly

The code that I've written is supposed to send the string "1" to the Arduino. Instead, from what I can see it receives "49" and for that reason both of the IF statements do not work. What am I doing wrong? I am using a Macbook(Mojave).
Here's the Arduino code:
String data;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
Serial.println(1);
digitalWrite(13, LOW);
}
void loop() {
while (Serial.available()) {
data = Serial.read();
Serial.println(data);
if (data == "1") {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("ON");
} else if (data == "2") {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("OFF");
}
}
}
And here's the python3 code:
import serial
import time
ArduinoSerial = serial.Serial('/dev/tty.usbserial-AH06F2WM', 9600)
time.sleep(2)
ArdData = ArduinoSerial.readline().decode('ascii')
print(ArdData)
ArduinoSerial.write("1".encode())
ArdData = ArduinoSerial.readline().decode('ascii')
print(ArdData)
Thank You!
In the ASCII table, a decimal value of 49 represents char '1'.
String and char are different. Serial.read() does not return a String. It returns an int.
You can use Serial.read() and treat a returned value as a char. Change String data to char data and compare against a char in the if statements.
char data; // char
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
Serial.println(1);
digitalWrite(13, LOW);
}
void loop() {
while (Serial.available()) {
data = Serial.read();
Serial.println(data);
if (data == '1') { // <- '1', not "1"
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("ON");
} else if (data == '2') { // <- '2', not "2"
digitalWrite(LED_BUILTIN, LOW);
Serial.println("OFF");
}
}
}
Or, if you really want to use String, you should use Serial.readString() instead.
String data;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
Serial.println(1);
digitalWrite(13, LOW);
}
void loop() {
while (Serial.available()) {
data = Serial.readString(); // <- readString() not read()
Serial.println(data);
if (data == "1") {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("ON");
} else if (data == "2") {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("OFF");
}
}
}

Arduino code for password-activated output through bluetooth

I have this pretty simple task to light up an LED for 3 seconds when I send the right password from my Android phone to the Arduino via Bluetooth. Below's the code I am using:
Setting up variables:
int output = 9; //Pin 9 on arduino (LED)
char final[4];
char correct[4] = {'Q','W','E','R'}; //Password is QWER
int pass_true;
void setup() {
pinMode(output, OUTPUT);
Serial.begin(9600);
}
Here's the code in question:
void loop() {
while(Serial.available()){
for(int i=0; i<4; i++){
final[i] = Serial.read();
}
for(int i=0; i<4; i++){
if(final[i]==correct[i]){
pass_true = 1;
}
else{
pass_true = 0;
break;
}
}
}
if(pass_true==1){
digitalWrite(output, HIGH);
Serial.println("ON");
delay(3000);
pass_true = 0;
}
else{
digitalWrite(output, LOW);
}
}
As a newbie I can't see what's wrong with this code. I don't get any response from the arduino when sending the correct password. The LED doesn't turn on, the device doesn't return any Serial.println() string.
I see to it that the electrical part is all good so I'm pretty sure the problem is with the code. I am using Bluetooth spp pro Android app.

Arduino Multi Thread

I Am working with arduino mega 2560....
Here I want to toggle two led simultaneously using threading.....
but there is problem that it is running one by one...
here is the code...
it would b your pleasure...
#include <SPI.h>
#include <Thread.h>
#include <ThreadController.h>
Thread Led1=Thread();
Thread Led2=Thread();
ThreadController controll=ThreadController();
void Led1run()
{
Serial.println("Led1");
Led2.run();
for(int i=0;i<5;i++)
{
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}
}
void Led2run()
{
Serial.println("Led2");
for(int i=0;i<5;i++)
{
digitalWrite(53,HIGH);
delay(1000);
digitalWrite(53,LOW);
delay(1000);
}
}
void setup()
{
Serial.begin(9600);
Led1.onRun(Led1run);
Led2.onRun(Led2run);
controll.add(&Led1);
pinMode(13,OUTPUT);
pinMode(53,OUTPUT);
}
void loop()
{
if(Serial.available())
{
Serial.println("loop");
Led1.run();
}
}

Resources