Help needed to convert a small code.

Hi
I am a complete novice to Arduino and purchased a board for a small project. The project is to connect a Morse key to the PC to be able to run some practice software. The script given is bellow. However with the amount of error messages given when trying verify before uploading I can only presume that this is not written for Ardunio. If this is a case I would very much appreciate if someone could help to convert it?
I have a Promicro and a Uno to use.
Thank you.

#include "DigiKeyboard.h"

#define DIT_PIN 2
#define DAH_PIN 0

void setup() {
 pinMode(DIT_PIN, INPUT_PULLUP);
 pinMode(DAH_PIN, INPUT_PULLUP);
 _delay_ms(10);
}

static uchar last_modifiers = 0xff;
static uchar reportBuffer[2]; // buffer for HID reports [ 1 modifier byte + (len-1) key strokes]

void loop() {
 uchar modifiers = 0;
 if (digitalRead(DIT_PIN) == 0) {
 modifiers |= MOD_CONTROL_LEFT;
 }
 if (digitalRead(DAH_PIN) == 0) {
 modifiers |= MOD_CONTROL_RIGHT;
 }
 
 if (modifiers != last_modifiers) {
 last_modifiers = modifiers;
 while (!usbInterruptIsReady()) {
 usbPoll();
 _delay_ms(1);
 }
 reportBuffer[0] = modifiers;
 reportBuffer[1] = 0;
 usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
 _delay_ms(1);
 }
}

I can only presume that this is not written for Ardunio

Some of it looks like it was written for Arduino.
Maybe if you told us how you found it…?

Sure the program I wish to use and the source for the code is given here:
Source Code - The Digispark source code is available for those that would like to build their own USB interface.
https://hamradio.solutions/vband/
Thanks

And where is the source of “DigiKeyboard.h” ?

I have no idea what that really means as I say I'm completely new at all this. Just borrowed an Arduino book and there is no mention of it. However I was just searching the web and found this DigisparkArduinoIntegration/DigiKeyboard.h at master · digistump/DigisparkArduinoIntegration · GitHub

DigiKeyboard.h is for the DigiSpark models of ATtiny85 boards from DigiStump: Digispark USB Development Board - Digistump

I would recommend switching to an Arduino Leonardo or Arduino Micro to emulate a keyboard using the built-in "Keyboard" library. The various "Teensy" models from PJRC.com (Teensy® 2.0) can also do keyboard emulation.

Thanks for that John. If I was to go down the route of the Arduino Micro and find a keyboard emulator, is it possible to know what key or keys to map looking at the script supplied?

rockeyes:
Thanks for that John. If I was to go down the route of the Arduino Micro and find a keyboard emulator, is it possible to know what key or keys to map looking at the script supplied?

Looks like it is using the left CTRL key for DIT and right CTRL key for DAH. I think the Keyboard equivalent would be:

  Keyboard.write(KEY_LEFT_CTRL); // DIT
  Keyboard.write(KEY_RIGHT_CTRL); // DAH

You may need to use separate .press() and .release() calls instead of .write() which does both.

Thank you John. That's very helpful. Guess I'm on my way to discover the magic arduino.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.