Hi Everyone,
I followed the instructions here (https://learn.adafruit.com/usb-next-keyboard-with-arduino-micro?view=all) and got my NeXT keyboard to work with my Mac via the Arduino Micro and its Keyboard HID emulation. However, I have a strange issue. When I plug in the Arduino Micro to USB, the blue light on the bottom comes on but it doesn't attempt to boot until I push the reset button on the board. I tried writing a simple program that only sets LED 13 to High. When I uploaded that to the board, it boots normally. It does not require the reset and LED 13 lights up solid as the code requires. There must be something in the code provided by that project that causes it not to boot until its reset. I was wondering if anyone here could spot what might be causing my problem. I'll post the code before void loop() below. The rest of the code can be seen at this github project. (https://github.com/adafruit/USB-NeXT-Keyboard)
Thanks in advance for your help,
-Jeff
// NeXT non-ADB Keyboard to USB converter
// This will take an older NeXT keyboard, talk to it, and turn the keycodes into a USB keyboard
// Requires an Arduino Micro for the USB portion - but could be ported to another micro fairly easily
// Written by Limor Fried / Adafruit Industries
// Released under BSD license - thanks NetBSD! :)
//
// Timing reference thanks to http://m0115.web.fc2.com/
// Pinouts thanks to http://www.68k.org/~degs/nextkeyboard.html
// Keycodes from http://ftp.netbsd.org/pub/NetBSD/NetBSD-release-6/src/sys/arch/next68k/dev/
#include "wsksymdef.h"
#include "nextkeyboard.h"
// the timing per bit, 50microseconds
// I changed this to 51 as it seemed to make this more reliable. However, 51 broke the modifier keys.
#define TIMING 50
// pick which pins you want to use
#define KEYBOARDOUT 3
#define KEYBOARDIN 2
// comment to speed things up, uncomment for help!
//#define DEBUG
// speed up reads by caching the 'raw' pin ports
volatile uint8_t *misoportreg;
uint8_t misopin;
// our little macro
#define readkbd() ((*misoportreg) & misopin)
// debugging/activity LED
#define LED 13
#define NEXT_KMBUS_IDLE 0x200600
// NeXT Keyboard Defines
// modifiers
#define NEXT_KB_CONTROL 0x1000
#define NEXT_KB_ALTERNATE_LEFT 0x20000
#define NEXT_KB_ALTERNATE_RIGHT 0x40000
#define NEXT_KB_COMMAND_LEFT 0x8000
#define NEXT_KB_COMMAND_RIGHT 0x10000
#define NEXT_KB_SHIFT_LEFT 0x2000
#define NEXT_KB_SHIFT_RIGHT 0x4000
// special command for setting LEDs
void setLEDs(bool leftLED, bool rightLED) {
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING *9);
digitalWrite(KEYBOARDOUT, HIGH);
delayMicroseconds(TIMING *3);
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING);
if (leftLED)
digitalWrite(KEYBOARDOUT, HIGH);
else
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING);
if (rightLED)
digitalWrite(KEYBOARDOUT, HIGH);
else
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING);
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING *7);
digitalWrite(KEYBOARDOUT, HIGH);
}
void query() {
// query the keyboard for data
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING *5);
digitalWrite(KEYBOARDOUT, HIGH);
delayMicroseconds(TIMING );
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING *3);
digitalWrite(KEYBOARDOUT, HIGH);
}
void nextreset() {
// reset the keyboard
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING);
digitalWrite(KEYBOARDOUT, HIGH);
delayMicroseconds(TIMING*4);
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING);
digitalWrite(KEYBOARDOUT, HIGH);
delayMicroseconds(TIMING*6);
digitalWrite(KEYBOARDOUT, LOW);
delayMicroseconds(TIMING*10);
digitalWrite(KEYBOARDOUT, HIGH);
}
void setup() {
// set up pin directions
pinMode(KEYBOARDOUT, OUTPUT);
pinMode(KEYBOARDIN, INPUT);
pinMode(LED, OUTPUT);
misoportreg = portInputRegister(digitalPinToPort(KEYBOARDIN));
misopin = digitalPinToBitMask(KEYBOARDIN);
// according to http://cfile7.uf.tistory.com/image/14448E464F410BF22380BB
query();
delay(5);
nextreset();
delay(8);
query();
delay(5);
nextreset();
delay(8);
Keyboard.begin();
#ifdef DEBUG
while (!Serial)
Serial.begin(57600);
Serial.println("NeXT");
#endif
}
uint32_t getresponse() {
// bitbang timing, read 22 bits 50 microseconds apart
cli();
while ( readkbd() );
delayMicroseconds(TIMING/2);
uint32_t data = 0;
for (uint8_t i=0; i < 22; i++) {
if (readkbd())
data |= ((uint32_t)1 << i);
delayMicroseconds(TIMING);
}
sei();
return data;
}