Keyboard not found even though I'm including Keyboard.h - c++

I want to make a simple program to put on my Arduino/Genuino (Sunfounder) Uno board that when plugged in, it invokes the Keyboard.print() function. When I compile my program to do that, it says that I'm not including Keyboard.h even though I actually am at the beginning of my program.
My code:
#include <Keyboard.h>
void setup() {
Keyboard.begin();
Keyboard.print("Hello, world!");
Keyboard.end();
}
void loop() {
}
When I compile the code, I get this error:
KeyboardMessage:4:3: error: 'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?
I checked my libraries file in the Arduino sketch editor file and Keyboard.h is there.
Any help is appreciated.

Arduino/Genuino Uno does not support Keyboard. You can see it in hardware/ardunino/avr/libraries/HID/HID.h. This file is included from Keyboard.h and contains #if defined(USBCON). But USBCON is not defined for Arduino/Genuino Uno. You need to use another board for Keyboard.h
Following controllers support it:
ATmega32U6
ATmega8U2
ATmega16U2
ATmega16U4
ATmega32U2
ATmega32U4
and some ATxxUSBxx
Arduino/Genuino Uno uses ATmega32U8.

You know what you don't require the 32u4 now, I got a solution to use keyboard and mouse through any Arduino.
All you have to do is something like this:
Serial.println("pressA")
On the Arduino
Head to a python script and do this:
import serial
import pyautogui
Arduino_Serial = serial.Serial('COM5', 9600)
while 1:
incoming_data = str(Arduino_Serial.readline())
print(incoming_data)
if 'pressA' in incoming_data:
pyautogui.press('a')
incoming_data = ""

Related

Arduino Serial library cannot find "availableForWrite()" function

I'm trying to get Arduino to send some chars back to my raspberry pi via the serial USB port.
However, when I try to use the function "Serial.availableForWrite()", the compiler failed with:
error: ‘class HardwareSerial’ has no member named ‘availableForWrite’
the rest of the code piece works fine though.
Here is the entire code:
void setup() {
// connect to serial
Serial.begin(9600);
}
void loop() {
// write value
if (Serial.availableForWrite() > 0) {
Serial.write("0.587");
}
Serial.write("Error");
delay(1000);
}
I am pretty sure my Arduino IDE is up to date, and also I don't think I have spelling mistakes. What could be causing this problem?
i dont have any problems with this code neither when compiling and i can even upload this sketch to my arduino uno this problem might be ocuring due to the usage of an raspbery pei maybe try and use an arduino board

exit status 1 expected primary-expression before '.' token

I'm trying to get the temperature and humidity every 60 seconds, but my code won't work. Whenever I compile it, there's this error "expected primary-expression before '.' token"
This line gets highlighted.
Serial.print(DHT.humidity,0);//prints humidity in serial
Here's my entire code:
#include <DHT_U.h>
#define DHT12PIN 7// sets pin 7 for DHT11 signal connection
void setup(){
Serial.begin(9600);//opens serial
}
void loop()
{
int chk = DHT.read12(DHT12PIN);//reads DHT12
Serial.print(DHT.temperature,0);//prints temp in serial
Serial.print(",");//pints comma in serial
Serial.print(DHT.humidity,0);//prints humidity in serial
Serial.println(); //carraiage return
delay(2000);//wait 2 seconsds
}
Wow there was a lot to unpack in this question.
I have used the DHT library by Adafruit a lot of times and thought that you just ended up using the class name instead of instantiating a variable and that's why you were facing the issue.
But then I realized that you are using the following function which is not a part of the Adafruit library:
int chk = dht.read12(DHT12PIN);//reads DHT12
So, I did a bit of digging on the internet and realized that someone has made a library for DHT with that specific function. (A lot of libraries to be specific)
Based on my analysis of this library, and the example code that was given, you have 3 issues.
You have included the wrong header file. The header file dht.h from RobTillaart's library should replace the header file DHT_U.h of Adafruit's library, in your code.
You have to instantiate the variable named DHT of the dht class. This can be done as follows, above your setup function
dht DHT;
Given that you had the code and the header files mixed up from TWO DIFFERENT LIBRARIES, I am guessing that you have installed the Adafruit library, in the place of RobTillaart's library. To fix this, you will have to remove the DHT-sensor-library-1.3.4 from your Documents/Arduino/libraries, create a new folder named DHTStable in it's place, and place all of the files listed here in the new folder.
Some words of experience-based wisdom, don't use RobTillaart's library, I can tell from a glance of it's folder structure that you will face more problems then you can solve if you use it. Instead use Adafruit's library, with their example. You will also have to install Adafruit's sensor library, but it is definitely worth it.

GPIO Pins RaspberryPi Using C++

I'm writing a program to control the GPIO pins on my raspberryPi with C++ and having difficulty I'm able to export with the following code:
char pathString[256];
sprintf(pathString, "%s/export", "/sys/class/gpio");
ofstream exporterFile(pathString);
exporterFile << pinNumber;
exporterFile.close()
This works an successfully exports the pin but this does not set the direction:
sprintf(pathString, "%sgpio%d/direction", "/sys/class/gpio", pinNumber);
ofstream directionFile(pathString);
directionFile << pinDirection;
directionFile.close();
For some reason I cannot write to the file, perhaps I do not have the right privileges. My question is, is that the problem and if so how do I solve it so I can write to the file.
Thanks in advance
You need to be root or run your program with sudo in order to use the GPIO pins.
However, I'd recommend using the wiringpi library http://wiringpi.com/ to access GPIO from c/c++. It is easy to use and raises the abstraction level a bit. It also lets you do things like PWM. A program using wiringpi also needs to be run with sudo.

MFRC522 PICC responded with NAK (Porting MFRC522 arduino library[C++] to [C])

First some introducing.
I am trying to make the MFRC522 library for Arduino work on an ATmega328 programmed in C(I am using a 'normal' controller first, to make it work on a raspberry pi in a later state).
I copied the .h and .cpp from the library to my own project and renamed the .c to .cpp. After removing the classes in the .h file, it was time for the .c file. I replaced all the 'byte' statements to 'uint8_t', replaced the 'Serial.print' with printf and did the changes for GPIO and SPI.
The problem.
After some small mistakes I finally got data from a keycard. However it looked like to work, I get an error with reading line 58 from the card. The error is:
MIFARE_READ() failed: A MIFARE PICC responded with NAK.
I added a print statement to the SPI write and read and found out the following difference(on the left the [C] version and on the right the Arduino version): (because of my reputation, the picture can be found in the BitBucket I mentioned at the code part)
Code
The code is pretty long, but i made it available on BitBucket
I hope someone can point me where to look(some [C++] >> [C] different interpretations), because I don't know anymore where to look.
Sander
You need to run the PCD_Authenticate function before reads and writes. There are a few pre programmed keys in the linked github library that will authenticate the cards. I was getting this when trying to write to the card because I was using KEY_B and not KEY_A. You can see this Authenticate used in the samples provided on that GitHub page. It should looks something like this.
status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
From what I can tell the NAK simply means that the wrong key was used or maybe no key.

Arduino ports registers in Eclipse are not working

I am building a Arduino application in C with Eclipse and the Arduino plugin. In my old code I used pinMode and digitalWrite. But as you all know, this uses more space. So I am re-building my code now with port manipulation. If you don't know what that is, you can see it here: http://www.arduino.cc/en/Reference/PortManipulation
I will explain what I did.
Where there stands pinMode, I changed it to something like this: DDRD = 0b11111111;
And where there stands digitalWrite, I changed it to PORTD = 0b10000000;
You can see it in my code below.
Eclipse is now giving me the error (highlighting the words DDRD and PORTD with a red line) of symbol not resolved for DDRD and PORTD, but the program is building and running normal. How do I solve this?
#include <avr/io.h>
#include <util/delay.h>
int main()
{
UCSR0B = 0; // disconnect pins 0 and 1 from USART (Serial)
DDRD = 0b11111111; // all pins of port D as output
for(;;)
{
PORTD = 0b10000000; // Pin 7 on
_delay_ms(500); // Wait
}
}
These are multi-level macros which encode direct volatile access to the SFR locations.
They are individually defined in one of an assortment of chip-specific header files which avr/io.h will include when it is informed of the specific CPU variant you are using.
Normally this is done with the -mmcu flag to avr-gcc, for example
-mmcu=atmega328p
However, if the Eclipse plugin does it's own pass through the project sources to try to give you advisory errors, it may not be smart enough to turn that into a define (you can get Eclipse claiming errors even when gcc is happy). To work around that, you may need to explicitly define the cpu type above the include in your code, or in some configuration for Eclipse. For example:
#ifndef __AVR_ATmega328P__
#define __AVR_ATmega328P__
#endif
#include <avr/io.h>
Note that this may cause problems if you later change the processor type! And even as is, it's a little iffy as there are two potential names for each processor.
I have faced the same problem. This is what solved for me in Eclipse.
From project explorer right click on the active project > Index > Rebuild