ProjectsWithRed / wireless_keyboard

Custom Arduino wireless keyboard.
Other
23 stars 8 forks source link

Making smaller version of this wireless keyboard by ProjectsWithRed #1

Open liamwheat opened 5 months ago

liamwheat commented 5 months ago

Hello So to summarize, I am attempting to make an identical keyboard to the one beautifully crafted by ProjectsWithRed, Mine however will feature only a single button and uses all the exact same components apart from the ATtiny84 as I am using the smaller ATtiny85 instead!

I have therefore used the same code and libraries and adjusted the wiring and code (pins and input commands) for my single button which I want to command an input of "LCTRL+e"repeated 3 times. when pressed once.

To my knowledge I followed as carefully as possible all steps involved however testing the button I had no luck. As mentioned I have used all the same parts, apart from using the smaller Attiny85 and single button.

Included in my post here I have attached my circuit diagrams and my code which I uploaded to the transmitter and receiver. also when uploading the code to the Attiny85 i used an Arduino uno as ISP, and followed online guides as to how to correctly upload code to the Attiny with this method. I believe I did this correctly.

Hopefully ProjectsWithRed can help me with this and anyone else looking to do similar can also see the fix.

*sidenote, my code for the buttons command is currently set as {{5, "128 101"}, (pin 5, LCTRL e) I am aware this currently does not include the repeat function i wish to add so that the single press of the button will cause it to loop/repeat 'LCTRL +e' 3 times. I am unsure of how to do that and so need help with this. But for now the problem stands, I cannot get it to show any command. It could be a wiring issue --perhaps I've made an error with the ATtiny pins I've used for the button and transmitter, or perhaps my code adjustments are incorrect.

Your help is greatly appreciated.

my transmitter attiny85

attiny85 transmittrer code.txt

Promiccro reciever code.txt

### Attiny transmitter code:

#include "SPI.h"
#include "RF24.h"

#include <Button.h>*

// Radio, choose any pins on your micro-controller to use as the CE and CSN pins.
#define CE_PIN 2
#define CSN_PIN 3

const uint8_t ADDRESS[6] = "CKeyB";

// Maximum number of keys a shortcut can be, or max number of keys to send through the NRF24L01 per button.
// This needs to match with the receiver side.
const int MAX_SHORTCUT_KEYS = 4;

const int BTN_SHORTCUT_SIZE = (3 * MAX_SHORTCUT_KEYS) + (MAX_SHORTCUT_KEYS - 1) + 1;

struct ButtonInfo {
    int btnPin;
    char btnShortcut[BTN_SHORTCUT_SIZE];
};

// All keys are represented using their ASCII decimal/int value. This can be found here: www.asciitable.com
// First specify the pin number, then seperate each key for that button in a string by a space.
const ButtonInfo BUTTONS_INFO[] = {{5, "128 101"},
                                   };

const int N_BUTTONS = sizeof(BUTTONS_INFO) / sizeof(BUTTONS_INFO[0]);

const uint16_t DEBOUNCE_MS = 10;

RF24 radio(CE_PIN, CSN_PIN);

Button *buttonObjs[N_BUTTONS];

void initRadio() {

    if (!radio.begin()) {
        while (1) {}
    }

    radio.setPALevel(RF24_PA_LOW);
    radio.setPayloadSize(BTN_SHORTCUT_SIZE);
    radio.openWritingPipe(ADDRESS);
    radio.setDataRate(RF24_1MBPS);

    radio.stopListening();
}

void initButtons() {
    for(int i = 0; i < N_BUTTONS; i++) {
        ButtonInfo btnInfo = BUTTONS_INFO[i];
        buttonObjs[i] -> begin();
    }
}

void setup() {

    initRadio();
    initButtons();

}

void loop() {

    // If any button pressed, then send the button keys through radio using the nRF24L01.
    for(int i = 0; i < N_BUTTONS; i++) {
        if(buttonObjs[i] -> pressed()) {
            ButtonInfo btnInfo = BUTTONS_INFO[i];

            radio.write(&btnInfo.btnShortcut, sizeof(btnInfo.btnShortcut));

            break;
        }
    }
}

Pro-micro receiver code:

#include "SPI.h"
#include "RF24.h"

#include <Keyboard.h>

#define CE_PIN 5
#define CSN_PIN 6

RF24 radio(CE_PIN, CSN_PIN);

uint8_t ADDRESS[6] = "CKeyB";

// Maximum number of keys a shortcut can be, or max number of keys to send through the NRF24L01 per button.
// This needs to match with the transmitter side.
const int MAX_SHORTCUT_KEYS = 4;

const int BTN_SHORTCUT_SIZE = (3 * MAX_SHORTCUT_KEYS) + (MAX_SHORTCUT_KEYS - 1) + 1;

char btnShortcut[BTN_SHORTCUT_SIZE];

char * key;
int keyInt;

void initRadio() {
    if (!radio.begin()) {
        while (1) {}
    }

    radio.setPALevel(RF24_PA_LOW);
    radio.setPayloadSize(BTN_SHORTCUT_SIZE);
    radio.openReadingPipe(1, ADDRESS);
    radio.setDataRate(RF24_1MBPS);

    radio.startListening();
}

void setup() {
    Keyboard.begin();

    initRadio();
}

void loop() {
    if (radio.available()) {
        radio.read(&btnShortcut, sizeof(btnShortcut));

        // Excract each key integer value from the incoming string and press each button.
        key = strtok(btnShortcut, " ");

        while(key != NULL) {
            keyInt = atoi(key);    
            Keyboard.press(keyInt);

            key = strtok(NULL, " ");
        }

        delay(10);
        Keyboard.releaseAll();
    }
}
ProjectsWithRed commented 5 months ago

After a quick look at your wiring diagram, I can see that on the Attiny85, the MISO and MOSI pins have been swapped around.

liamwheat commented 5 months ago

I used the below image, however I have found what you're saying on other attiny 85 images that perhaps the image I've used was incorrect. I will try swapping and let you know! tumblr_inline_nistwftvW81qzarq5 (1)

ProjectsWithRed commented 5 months ago

You need to check the datasheet for your specific Attiny to make sure.

liamwheat commented 5 months ago

Hi i have now confirmed the pins for my attiny85 and rewired accordingly. However still no luck for me i'm afraid. Does the code look all good to you? I've also attached a picture how im powering it, the same as your video 2, 3v coin cells. Just wanted to check it looks alright with the capacitors.

Also for my single button -- I want to command an input of "LCTRL+e"repeated 3 times, when pressed once. would you be able to help me with that? the normal LCTRL + e part i have used your method for and tweaked the index, but the repeat part im not sure how to do.

Best regards

IMG_20240126_185643

ProjectsWithRed commented 5 months ago

If none of these changes work, try point 1, which might just be the issue.

liamwheat commented 5 months ago

So, I got some Attiny84's to try out. I've now made the exact same circuit with 3 buttons, and code exactly as yours. Unfortunately no luck for me yet. I can't think im doing wrong

For the uploading of the code I'm using an ardiuno UNO as ISP and below is my method:

-First I connect my Arduino UNO on its own and do the following in the Arduino software: 1.File, examples, ArdiunoISP,

  1. Tools, board, Arduino UNO,
  2. Tools, Programmer, AVRISP mkII
  3. Upload code.

-Then I attach my micro-controller to the UNO,

  1. File, preferences and included.
  2. Boards manager , install Attiny by david a. mellis
  3. Tools, board, ensure: Attiny84, clock 8Mhz internal, correct port selected,
  4. Tools, programmer, Arduino as ISP.
  5. Tools, burn bootloader
  6. Finally upload transmitter code from your post. object

As for the function line 64, I had to delete it as it always created an error code and would not let me upload the code with that line.

With the ce and csn pins im now using same code unchanged with same button layout.

Not sure where im going wrong this time.

ProjectsWithRed commented 5 months ago

Removing line 64 will for sure make the code not work, so if this has been the case then that might very well be the core of the issue. Can you just upload anything you know that will work to the Attiny so you can confirm everything else is good to go, could be a simple blinking LED example. Once you have tested and confirmed this, upload the code with line 64 and lets see what error you are getting.

liamwheat commented 4 months ago

I uploaded the blink code which worked with my Attiny85 so i can confirm everryhting else seems good, However I have indeed had this line 64 of code removed on both my Attiny 85 and Attiny 84 tests. I will get a sceenshot of the error for you soon.
Thanks so much!

liamwheat commented 4 months ago

Hello, sooo.. here I have copied the error I am getting and a screenshot of the highlighted line 64. the code has not been edited it is copied directly from your project. Hopefully this could give us an idea as to whats going wrong for me? Kind regards.

error code ardruino

Below is the error messages:

Arduino: 1.8.19 (Windows 10), Board: "Arduino Uno" C:\Users\liam_\OneDrive\Documents\Arduino\Attiny_84_transmitter_code\Attiny_84_transmitter_code.ino: In function 'void initButtons()':

Attiny_84_transmitter_code:64:63: error: no matching function for call to 'Button::Button(int&, const uint16_t&)'

     buttonObjs[i] = new Button(btnInfo.btnPin, DEBOUNCE_MS);

                                                           ^

In file included from C:\Users\liam_\OneDrive\Documents\Arduino\Attiny_84_transmitter_code\Attiny_84_transmitter_code.ino:4:0:

C:\Users\liam_\OneDrive\Documents\Arduino\libraries\Button/Button.h:14:3: note: candidate: Button::Button(uint8_t)

Button(uint8_t pin);

^~

C:\Users\liam_\OneDrive\Documents\Arduino\libraries\Button/Button.h:14:3: note: candidate expects 1 argument, 2 provided

C:\Users\liam_\OneDrive\Documents\Arduino\libraries\Button/Button.h:11:7: note: candidate: constexpr Button::Button(const Button&)

class Button

   ^~~~~~

C:\Users\liam_\OneDrive\Documents\Arduino\libraries\Button/Button.h:11:7: note: candidate expects 1 argument, 2 provided

C:\Users\liam_\OneDrive\Documents\Arduino\libraries\Button/Button.h:11:7: note: candidate: constexpr Button::Button(Button&&)

C:\Users\liam_\OneDrive\Documents\Arduino\libraries\Button/Button.h:11:7: note: candidate expects 1 argument, 2 provided

exit status 1

no matching function for call to 'Button::Button(int&, const uint16_t&)'

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

ProjectsWithRed commented 4 months ago

Have you installed the Button library by Michael Adams?

liamwheat commented 4 months ago

Sorry for slow reply, I've been on leave.

Yes i've just double checked and i can confirm button library by Micheal adams is installed

liamwheat commented 4 months ago

I have noticed that in your video button library shows orange in colour, mine is however grey. Comparison below. Does this colour difference indicate mine is not linked/working properly? ardunio software comparison

liamwheat commented 4 months ago

Okay ive just managed to link the library and its now orange. Now when attempting to verify the code to upload i get alot of errors associated with SPI.

Small snipit of the error below:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src/SPI.h:310:55: error: 'SPIE' was not declared in this scope inline static void detachInterrupt() { SPCR &= ~_BV(SPIE); } C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src/SPI.h:310:55: note: suggested alternative: 'ACIE' exit status 1 Error compiling for board ATtiny24/44/84.

Could it be that the SPI library is not installed? where did you install/ find this library?. I do have the RF24 library installed as I followed your video, however SPI library installation was not covered.

As always kind regards and thankyou for checking

ProjectsWithRed commented 4 months ago

Are you changing the Tools>Programmer to USBasp (ATTinyCore) and selecting Attiny84 no bootloader as the microcontroller in the Arduino IDE? Are you using this ATTinyCore board manager?

liamwheat commented 4 months ago

My method for uploading the code is a bit earlier in our discussion, I used the arduino uno as ISP. Have a quick check back, as I've put step by step my actions by the book I believe. I do have an USBasp adapter, but it was requiring more and more things to upload the code, so I went with the arduino uno solution following this guide:

https://www.youtube.com/watch?v=dtPGMTBdDoA&ab_channel=AhmadLogs

ProjectsWithRed commented 4 months ago

The issue is with the micro-controller selection and upload, I have not uploaded code to the Attiny in that way so I can't help in that regard.

liamwheat commented 3 months ago

Would you be able to provide a guide on how you uploaded your code onto the attiny using the USBasp.? I do own one, but I've searched hard to find a concrete method but not sure how to do it. If you could give me a stepped guide including components i could give it a go. kind regards and as always thankyou for your support so far

ProjectsWithRed commented 3 months ago

The instructions for that is in the readme of this repository, found here