Looking for some guidance on a project i stumbled across on instructables that i am trying to build with very basic knowledge of arduino, more complex understanding of electronics.
The project basically involves using 2 nanos and a micro to provide Inputs. the inputs should translate to ASCII keyboard format, i have the correct libraries loaded.
Im 99.9% sure i have wired it all up correctly, i am managing to get data from the second slave all the way through to the master and printing keys. The issue is no matter what input button i press i am getting ]z]p as the ouput. i beleive the problem to be with how the master converts the input coming from the slaves into an ASCII number but it is beyong my level of understanding despite my best efforts. Has anyone on here built this or can provide some guidance on a solution?
I have managed to configure the micro and nano combo to provide inputs for alphabet a-z, the issue seems to be coming from the function buttons which are used to provide a secondary key assignment for the same push button , like pressing ctrl shift .
I have run a seril monitor on all boards and the pins are checkin out to the correct ones but at the master all it see's is :
Sending key command for data: 84
Show this, too... None of the source code has Serial.print() so it looks like you left some "debug" statements in for keycode and position... which means you probably mangled other code.
/*
System for assigning double key press for any key code over 26. There are about 90 possible instructions from this device
1-26 => a-z => [97-122]
27-52 => [a-[z => 91 + [97-122]
53-78 => \a-\z => 92 + [97-122]
79-104 =>]a-]z => 93 + [97-122]
*/
What we need is a photo of your hand drawn wiring diagram. Also ALL the code in code tags and any verbose error logs and serial output also in code tags.
As stated below, all i have connected is the power between the boards, the serial connections between the boards and one button i am moving round between the common ground bar on the perf board and the input pins to keep the board clear.
This is the code, apologies i forgot i added a read serial to the master i have not modified the salve code from what is on git HUB . I will produce a clear schematic diagram tomorrow. For simplicity at this stage i have just connected the power and grounds and the serial connections and im moving the momentary button between ground and and various input pins to keep my board clear.
Serial comms is as follows:
Micro Tx1/Rx1 to Slave 1 Nano A4/A5
Slave 1 Nano DI12/DI13 to Slave 2 Nano A4/A5
appreciate the help.
Modified master c ode
#include <Keyboard.h>
#include <SoftwareSerial.h>
/*
MASTER - ARDUINO MICRO - Uses Serial1 by default on the rX and tX pins
Controller recieves button press data from slave1 controller as well as debouncing and handling 14 two-way switches. This code implements an
key command system that references ASCII characters by their integer
values, then uses the Arduino Keyboard library to send the command.
*/
const int pins[] = {4,5,6,7,8,9,10,16,14,15,18,19,20,21};
int states[14] = {1};
int prevStates[14] = {1};
const int debounceDelay = 20;
const int asciiIntStartPoint = 97;
const int fnStartPoint = 91;
void setup(){
Keyboard.begin();
Serial1.begin(9600);
pinMode(2, INPUT);
pinMode(3, OUTPUT);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
pinMode(20, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);
}
void loop(){
/*
Read incoming data from slave 1
contains data from both slaves
*/
while(Serial1.available() > 0){
int data = Serial1.read();
keyboardCommand(data);
}
/*
Debounce and handle onboard switches
*/
for(int i = 0; i < 14; i++){
int state = digitalRead(pins[i]);
states[i] = state;
}
for(int i = 0; i < 14; i++){
if(states[i] != prevStates[i]){
if(states[i] == LOW){ // on button press
keyboardCommand(i + 74);
} else {
keyboardCommand(i + 90);
}
prevStates[i] = states[i];
delay(debounceDelay);
}
}
}
void keyboardCommand(int data){
/*
System for assigning double key press for any key code over 26. There are about 90 possible instructions from this device
1-26 => a-z => [97-122]
27-52 => [a-[z => 91 + [97-122]
53-78 => \a-\z => 92 + [97-122]
79-104 =>]a-]z => 93 + [97-122]
*/
int position = ((data + asciiIntStartPoint) % 26) + 1;
int keyCode = position + asciiIntStartPoint - 1;
if(data <= 26){
Keyboard.press(data + asciiIntStartPoint);
} else if(data > 26 && data <= 52){
Keyboard.press(fnStartPoint);
Keyboard.press(keyCode);
} else if(data > 52 && data < 78){
Keyboard.press(fnStartPoint + 1);
Keyboard.press(keyCode);
} else {
Keyboard.press(fnStartPoint + 2);
Keyboard.press(keyCode);
}
delay(200);
Keyboard.releaseAll();
}
My apologies, ive spent half the evening soldering and checking the wiring to rule that out and forgot what i had done with the code last night. I started a fresh today but still had the serial monitor log recorded.
You stated you had a problem with the key codes not being what you expected, is that right.
We now know the code is not your own, the wiring diagram is not your own so you are assembling a kit. So far so good?
May I suggest you strip out all the parts (both hardware and software) that do not have something to do with the sending of the key codes. Once you simplify the problem, it will be resolved much quicker.
Once that is working start adding in additional pieces, test, if ok add another piece etc.
I have not claimed the code was my own and yes im trying to follow someones instructable to build something.
The hardware is fucntioning, its passing data from slave 2 all the way to the master. I am seeing keystrokes recorded but the wrong keystrokes. The same thing happens when i have just one board connected (master Micro) so its a fair assumption that the issue is with how it is interpreting the input.
Until you draw a wiring diagram and post a picture of it we will take the wiring as good.
I took a quick look at the code and it is nothing like I have seen before.
If I tell you I press the numeric key 1, then the lowercase a, then the upper case A. Tell me in bit format or hex what you expect to see printed on the Serial monitor.
This. I would suggest coming to enough of an understanding of how the code works, or is supposed to work, that you could write some very simple sketches just to verify the wiring.
So a different angle of attack, as I remain of the opinion that your wiring must be the problem.
This is giving an Instructable more credit than they are usually worth. Nevertheless, something that could just serial print as buttons were pressed and released might show up an error. And you would have learned something just doing, even if all it does is prove you are a wiring genius.
Leave keyboard.h out of the picture in these experiments. Maybe use a button handling library (ezButton small e small zButton doesn't suck) and just confirm your wiring.
Another set of experiments might involve only the Arduino talking to the big rig over USB. Here I would suggest using a software UART connected to another USB port monitored by a terminal emulator like PuTTY or CoolTerm. You'd need a USB to serial adapter, or use an Arduino which would let you hijack the one that is onboard.
This looks like a ridiculously complicated project. There are crap-ton of button boxes using Arduino. Perhaps aiming for something with fewer than 4096 switches and buttons as a starter project would not be a waste of time.
@morebeanz99 Agreed, that code is bizarre and the so-called debounce is a joke. Without a clear understanding of what input creates what output the entire project is a waste of time that is why I asked him for a simple 1aA test.